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  
30      private ObjectUtil() {
31      }
32  
33      /**
34       * Checks that the given argument is not null. If it is, throws {@link NullPointerException}.
35       * Otherwise, returns the argument.
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       * Check that the given varargs is not null and does not contain elements
46       * null elements.
47       *
48       * If it is, throws {@link NullPointerException}.
49       * Otherwise, returns the argument.
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       * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
66       * Otherwise, returns the argument.
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       * Checks that the given argument is not null. If it is, throws {@link IllegalArgumentException}.
77       * Otherwise, returns the argument.
78       *
79       * @param <T> type of the given argument value.
80       * @param name of the parameter, belongs to the exception message.
81       * @param index of the array, belongs to the exception message.
82       * @param value to check.
83       * @return the given argument value.
84       * @throws IllegalArgumentException if value is null.
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       * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
96       * Otherwise, returns the argument.
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      * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
107      * Otherwise, returns the argument.
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      * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
118      * Otherwise, returns the argument.
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      * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
129      * Otherwise, returns the argument.
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      * Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
140      * Otherwise, returns the argument.
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      * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
151      * Otherwise, returns the argument.
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      * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
162      * Otherwise, returns the argument.
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      * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
173      * Otherwise, returns the argument.
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      * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
184      * Otherwise, returns the argument.
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      * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}.
195      * Otherwise, returns the argument.
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      * Checks that the given argument is neither null nor empty.
206      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
207      * Otherwise, returns the argument.
208      */
209     public static <T> T[] checkNonEmpty(T[] array, String name) {
210         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
219      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
220      * Otherwise, returns the argument.
221      */
222     public static byte[] checkNonEmpty(byte[] array, String name) {
223         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
232      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
233      * Otherwise, returns the argument.
234      */
235     public static char[] checkNonEmpty(char[] array, String name) {
236         //No String concatenation for check
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      * Checks that the given argument is neither null nor empty.
245      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
246      * Otherwise, returns the argument.
247      */
248     public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
249         //No String concatenation for check
250         if (checkNotNull(collection, name).isEmpty()) {
251             throw new IllegalArgumentException("Param '" + name + "' must not be empty");
252         }
253         return collection;
254     }
255 
256     /**
257      * Checks that the given argument is neither null nor empty.
258      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
259      * Otherwise, returns the argument.
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      * 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 <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      * 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 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      * Trims the given argument and checks whether it is neither null nor empty.
294      * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
295      * Otherwise, returns the trimmed argument.
296      *
297      * @param value to trim and check.
298      * @param name of the parameter.
299      * @return the trimmed (not the original) value.
300      * @throws NullPointerException if value is null.
301      * @throws IllegalArgumentException if the trimmed value is empty.
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      * Resolves a possibly null Integer to a primitive int, using a default value.
310      * @param wrapper the wrapper
311      * @param defaultValue the default value
312      * @return the primitive value
313      */
314     public static int intValue(Integer wrapper, int defaultValue) {
315         return wrapper != null ? wrapper : defaultValue;
316     }
317 
318     /**
319      * Resolves a possibly null Long to a primitive long, using a default value.
320      * @param wrapper the wrapper
321      * @param defaultValue the default value
322      * @return the primitive value
323      */
324     public static long longValue(Long wrapper, long defaultValue) {
325         return wrapper != null ? wrapper : defaultValue;
326     }
327 }