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