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