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.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 * A grab-bag of useful utility methods.
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 * Check that the given varargs is not null and does not contain elements
37 * null elements.
38 *
39 * If it is, throws {@link NullPointerException}.
40 * Otherwise, returns the argument.
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 * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
57 * Otherwise, returns the argument.
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 * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
68 * Otherwise, returns the argument.
69 *
70 * @param <T> type of the given argument value.
71 * @param name of the parameter, belongs to the exception message.
72 * @param index of the array, belongs to the exception message.
73 * @param value to check.
74 * @return the given argument value.
75 * @throws IllegalArgumentException if value is null.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
87 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
98 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
109 * Otherwise, returns the argument.
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 * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
120 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
131 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
142 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
153 * Otherwise, returns the argument.
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 * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
164 * Otherwise, returns the argument.
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 * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
175 * Otherwise, returns the argument.
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 * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
186 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
197 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
198 * Otherwise, returns the argument.
199 */
200 public static <T> T[] checkNonEmpty(T[] array, String name) {
201 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
210 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
211 * Otherwise, returns the argument.
212 */
213 public static byte[] checkNonEmpty(byte[] array, String name) {
214 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
223 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
224 * Otherwise, returns the argument.
225 */
226 public static char[] checkNonEmpty(char[] array, String name) {
227 //No String concatenation for check
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 * Checks that the given argument is neither null nor empty.
236 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
237 * Otherwise, returns the argument.
238 */
239 public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
240 //No String concatenation for check
241 if (requireNonNull(collection, name).isEmpty()) {
242 throw new IllegalArgumentException("Param '" + name + "' must not be empty");
243 }
244 return collection;
245 }
246
247 /**
248 * Checks that the given argument is neither null nor empty.
249 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
250 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
261 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
262 * Otherwise, returns the argument.
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 * Checks that the given argument is neither null nor empty.
273 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
274 * Otherwise, returns the argument.
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 * Trims the given argument and checks whether it is neither null nor empty.
285 * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
286 * Otherwise, returns the trimmed argument.
287 *
288 * @param value to trim and check.
289 * @param name of the parameter.
290 * @return the trimmed (not the original) value.
291 * @throws NullPointerException if value is null.
292 * @throws IllegalArgumentException if the trimmed value is empty.
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 * Resolves a possibly null Integer to a primitive int, using a default value.
301 * @param wrapper the wrapper
302 * @param defaultValue the default value
303 * @return the primitive value
304 */
305 public static int intValue(Integer wrapper, int defaultValue) {
306 return wrapper != null ? wrapper : defaultValue;
307 }
308
309 /**
310 * Resolves a possibly null Long to a primitive long, using a default value.
311 * @param wrapper the wrapper
312 * @param defaultValue the default value
313 * @return the primitive value
314 */
315 public static long longValue(Long wrapper, long defaultValue) {
316 return wrapper != null ? wrapper : defaultValue;
317 }
318 }