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
221 public static <T> T[] checkNonEmpty(T[] array, String name) {
222
223 if (checkNotNull(array, name).length == 0) {
224 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
225 }
226 return array;
227 }
228
229
230
231
232
233
234 public static byte[] checkNonEmpty(byte[] array, String name) {
235
236 if (checkNotNull(array, name).length == 0) {
237 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
238 }
239 return array;
240 }
241
242
243
244
245
246
247 public static char[] checkNonEmpty(char[] array, String name) {
248
249 if (checkNotNull(array, name).length == 0) {
250 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
251 }
252 return array;
253 }
254
255
256
257
258
259
260 public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
261
262 if (checkNotNull(collection, name).isEmpty()) {
263 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
264 }
265 return collection;
266 }
267
268
269
270
271
272
273 public static String checkNonEmpty(final String value, final String name) {
274 if (checkNotNull(value, name).isEmpty()) {
275 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
276 }
277 return value;
278 }
279
280
281
282
283
284
285 public static <K, V, T extends Map<K, V>> T checkNonEmpty(T value, String name) {
286 if (checkNotNull(value, name).isEmpty()) {
287 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
288 }
289 return value;
290 }
291
292
293
294
295
296
297 public static CharSequence checkNonEmpty(final CharSequence value, final String name) {
298 if (checkNotNull(value, name).length() == 0) {
299 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
300 }
301 return value;
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315 public static String checkNonEmptyAfterTrim(final String value, final String name) {
316 String trimmed = checkNotNull(value, name).trim();
317 return checkNonEmpty(trimmed, name);
318 }
319
320
321
322
323
324
325
326 public static int intValue(Integer wrapper, int defaultValue) {
327 return wrapper != null ? wrapper : defaultValue;
328 }
329
330
331
332
333
334
335
336 public static long longValue(Long wrapper, long defaultValue) {
337 return wrapper != null ? wrapper : defaultValue;
338 }
339 }