1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.util.internal;
16
17 import java.util.Collection;
18 import java.util.Map;
19
20
21
22
23 public final class ObjectUtil {
24
25 private static final float FLOAT_ZERO = 0.0F;
26 private static final double DOUBLE_ZERO = 0.0D;
27 private static final long LONG_ZERO = 0L;
28 private static final int INT_ZERO = 0;
29 private static final short SHORT_ZERO = 0;
30
31 private ObjectUtil() {
32 }
33
34
35
36
37
38 public static <T> T checkNotNull(T arg, String text) {
39 if (arg == null) {
40 throw new NullPointerException(text);
41 }
42 return arg;
43 }
44
45
46
47
48
49
50
51
52 public static <T> T[] deepCheckNotNull(String text, T... varargs) {
53 if (varargs == null) {
54 throw new NullPointerException(text);
55 }
56
57 for (T element : varargs) {
58 if (element == null) {
59 throw new NullPointerException(text);
60 }
61 }
62 return varargs;
63 }
64
65
66
67
68
69 public static <T> T checkNotNullWithIAE(final T arg, final String paramName) throws IllegalArgumentException {
70 if (arg == null) {
71 throw new IllegalArgumentException("Param '" + paramName + "' must not be null");
72 }
73 return arg;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87 public static <T> T checkNotNullArrayParam(T value, int index, String name) throws IllegalArgumentException {
88 if (value == null) {
89 throw new IllegalArgumentException(
90 "Array index " + index + " of parameter '" + name + "' must not be null");
91 }
92 return value;
93 }
94
95
96
97
98
99 public static int checkPositive(int i, String name) {
100 if (i <= INT_ZERO) {
101 throw new IllegalArgumentException(name + " : " + i + " (expected: > 0)");
102 }
103 return i;
104 }
105
106
107
108
109
110 public static long checkPositive(long l, String name) {
111 if (l <= LONG_ZERO) {
112 throw new IllegalArgumentException(name + " : " + l + " (expected: > 0)");
113 }
114 return l;
115 }
116
117
118
119
120
121 public static double checkPositive(final double d, final String name) {
122 if (d <= DOUBLE_ZERO) {
123 throw new IllegalArgumentException(name + " : " + d + " (expected: > 0)");
124 }
125 return d;
126 }
127
128
129
130
131
132 public static float checkPositive(final float f, final String name) {
133 if (f <= FLOAT_ZERO) {
134 throw new IllegalArgumentException(name + " : " + f + " (expected: > 0)");
135 }
136 return f;
137 }
138
139
140
141
142
143 public static short checkPositive(short s, String name) {
144 if (s <= SHORT_ZERO) {
145 throw new IllegalArgumentException(name + " : " + s + " (expected: > 0)");
146 }
147 return s;
148 }
149
150
151
152
153
154 public static int checkPositiveOrZero(int i, String name) {
155 if (i < INT_ZERO) {
156 throw new IllegalArgumentException(name + " : " + i + " (expected: >= 0)");
157 }
158 return i;
159 }
160
161
162
163
164
165 public static long checkPositiveOrZero(long l, String name) {
166 if (l < LONG_ZERO) {
167 throw new IllegalArgumentException(name + " : " + l + " (expected: >= 0)");
168 }
169 return l;
170 }
171
172
173
174
175
176 public static double checkPositiveOrZero(final double d, final String name) {
177 if (d < DOUBLE_ZERO) {
178 throw new IllegalArgumentException(name + " : " + d + " (expected: >= 0)");
179 }
180 return d;
181 }
182
183
184
185
186
187 public static float checkPositiveOrZero(final float f, final String name) {
188 if (f < FLOAT_ZERO) {
189 throw new IllegalArgumentException(name + " : " + f + " (expected: >= 0)");
190 }
191 return f;
192 }
193
194
195
196
197
198 public static int checkInRange(int i, int start, int end, String name) {
199 if (i < start || i > end) {
200 throw new IllegalArgumentException(name + ": " + i + " (expected: " + start + "-" + end + ")");
201 }
202 return i;
203 }
204
205
206
207
208
209 public static long checkInRange(long l, long start, long end, String name) {
210 if (l < start || l > end) {
211 throw new IllegalArgumentException(name + ": " + l + " (expected: " + start + "-" + end + ")");
212 }
213 return l;
214 }
215
216
217
218
219
220 public static double checkInRange(double d, double start, double end, String name) {
221 if (d < start || d > end) {
222 throw new IllegalArgumentException(name + ": " + d + " (expected: " + start + "-" + end + ")");
223 }
224 return d;
225 }
226
227
228
229
230
231
232 public static <T> T[] checkNonEmpty(T[] array, String name) {
233
234 if (checkNotNull(array, name).length == 0) {
235 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
236 }
237 return array;
238 }
239
240
241
242
243
244
245 public static byte[] checkNonEmpty(byte[] array, String name) {
246
247 if (checkNotNull(array, name).length == 0) {
248 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
249 }
250 return array;
251 }
252
253
254
255
256
257
258 public static char[] checkNonEmpty(char[] array, String name) {
259
260 if (checkNotNull(array, name).length == 0) {
261 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
262 }
263 return array;
264 }
265
266
267
268
269
270
271 public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
272
273 if (checkNotNull(collection, name).isEmpty()) {
274 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
275 }
276 return collection;
277 }
278
279
280
281
282
283
284 public static String checkNonEmpty(final String value, final String name) {
285 if (checkNotNull(value, name).isEmpty()) {
286 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
287 }
288 return value;
289 }
290
291
292
293
294
295
296 public static <K, V, T extends Map<K, V>> T checkNonEmpty(T value, String name) {
297 if (checkNotNull(value, name).isEmpty()) {
298 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
299 }
300 return value;
301 }
302
303
304
305
306
307
308 public static CharSequence checkNonEmpty(final CharSequence value, final String name) {
309 if (checkNotNull(value, name).length() == 0) {
310 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
311 }
312 return value;
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 public static String checkNonEmptyAfterTrim(final String value, final String name) {
327 String trimmed = checkNotNull(value, name).trim();
328 return checkNonEmpty(trimmed, name);
329 }
330
331
332
333
334
335
336
337 public static int intValue(Integer wrapper, int defaultValue) {
338 return wrapper != null ? wrapper : defaultValue;
339 }
340
341
342
343
344
345
346
347 public static long longValue(Long wrapper, long defaultValue) {
348 return wrapper != null ? wrapper : defaultValue;
349 }
350 }