1 /*
2 * Copyright 2014 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5 * "License"); you may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
13 * the License.
14 */
15 package io.netty.util.internal;
16
17 import java.util.Collection;
18 import java.util.Map;
19
20 /**
21 * A grab-bag of useful utility methods.
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 * Checks that the given argument is not null. If it is, throws {@link NullPointerException}.
36 * Otherwise, returns the argument.
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 * Check that the given varargs is not null and does not contain elements
47 * null elements.
48 *
49 * If it is, throws {@link NullPointerException}.
50 * Otherwise, returns the argument.
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 * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
67 * Otherwise, returns the argument.
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 * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
78 * Otherwise, returns the argument.
79 *
80 * @param <T> type of the given argument value.
81 * @param name of the parameter, belongs to the exception message.
82 * @param index of the array, belongs to the exception message.
83 * @param value to check.
84 * @return the given argument value.
85 * @throws IllegalArgumentException if value is null.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
97 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
108 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
119 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
130 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
141 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
152 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
163 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
174 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
185 * Otherwise, returns the argument.
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 * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
196 * Otherwise, returns the argument.
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 * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
207 * Otherwise, returns the argument.
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 * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
218 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
229 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
230 * Otherwise, returns the argument.
231 */
232 public static <T> T[] checkNonEmpty(T[] array, String name) {
233 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
242 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
243 * Otherwise, returns the argument.
244 */
245 public static byte[] checkNonEmpty(byte[] array, String name) {
246 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
255 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
256 * Otherwise, returns the argument.
257 */
258 public static char[] checkNonEmpty(char[] array, String name) {
259 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
268 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
269 * Otherwise, returns the argument.
270 */
271 public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
272 //No String concatenation for check
273 if (checkNotNull(collection, name).isEmpty()) {
274 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
275 }
276 return collection;
277 }
278
279 /**
280 * Checks that the given argument is neither null nor empty.
281 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
282 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
293 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
294 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
305 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
306 * Otherwise, returns the argument.
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 * Trims the given argument and checks whether it is neither null nor empty.
317 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
318 * Otherwise, returns the trimmed argument.
319 *
320 * @param value to trim and check.
321 * @param name of the parameter.
322 * @return the trimmed (not the original) value.
323 * @throws NullPointerException if value is null.
324 * @throws IllegalArgumentException if the trimmed value is empty.
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 * Resolves a possibly null Integer to a primitive int, using a default value.
333 * @param wrapper the wrapper
334 * @param defaultValue the default value
335 * @return the primitive value
336 */
337 public static int intValue(Integer wrapper, int defaultValue) {
338 return wrapper != null ? wrapper : defaultValue;
339 }
340
341 /**
342 * Resolves a possibly null Long to a primitive long, using a default value.
343 * @param wrapper the wrapper
344 * @param defaultValue the default value
345 * @return the primitive value
346 */
347 public static long longValue(Long wrapper, long defaultValue) {
348 return wrapper != null ? wrapper : defaultValue;
349 }
350 }