View Javadoc
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 neither null nor empty.
218      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
219      * Otherwise, returns the argument.
220      */
221     public static <T> T[] checkNonEmpty(T[] array, String name) {
222         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
231      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
232      * Otherwise, returns the argument.
233      */
234     public static byte[] checkNonEmpty(byte[] array, String name) {
235         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
244      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
245      * Otherwise, returns the argument.
246      */
247     public static char[] checkNonEmpty(char[] array, String name) {
248         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
257      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
258      * Otherwise, returns the argument.
259      */
260     public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
261         //No String concatenation for check
262         if (checkNotNull(collection, name).isEmpty()) {
263             throw new IllegalArgumentException("Param '" + name + "' must not be empty");
264         }
265         return collection;
266     }
267 
268     /**
269      * Checks that the given argument is neither null nor empty.
270      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
271      * Otherwise, returns the argument.
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      * Checks that the given argument is neither null nor empty.
282      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
283      * Otherwise, returns the argument.
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      * Checks that the given argument is neither null nor empty.
294      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
295      * Otherwise, returns the argument.
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      * Trims the given argument and checks whether it is neither null nor empty.
306      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
307      * Otherwise, returns the trimmed argument.
308      *
309      * @param value to trim and check.
310      * @param name of the parameter.
311      * @return the trimmed (not the original) value.
312      * @throws NullPointerException if value is null.
313      * @throws IllegalArgumentException if the trimmed value is empty.
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      * Resolves a possibly null Integer to a primitive int, using a default value.
322      * @param wrapper the wrapper
323      * @param defaultValue the default value
324      * @return the primitive value
325      */
326     public static int intValue(Integer wrapper, int defaultValue) {
327         return wrapper != null ? wrapper : defaultValue;
328     }
329 
330     /**
331      * Resolves a possibly null Long to a primitive long, using a default value.
332      * @param wrapper the wrapper
333      * @param defaultValue the default value
334      * @return the primitive value
335      */
336     public static long longValue(Long wrapper, long defaultValue) {
337         return wrapper != null ? wrapper : defaultValue;
338     }
339 }