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    * http://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  
19  /**
20   * A grab-bag of useful utility methods.
21   */
22  public final class ObjectUtil {
23  
24      private ObjectUtil() {
25      }
26  
27      /**
28       * Checks that the given argument is not null. If it is, throws {@link NullPointerException}.
29       * Otherwise, returns the argument.
30       */
31      public static <T> T checkNotNull(T arg, String text) {
32          if (arg == null) {
33              throw new NullPointerException(text);
34          }
35          return arg;
36      }
37  
38      /**
39       * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
40       * Otherwise, returns the argument.
41       */
42      public static int checkPositive(int i, String name) {
43          if (i <= 0) {
44              throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
45          }
46          return i;
47      }
48  
49      /**
50       * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
51       * Otherwise, returns the argument.
52       */
53      public static long checkPositive(long i, String name) {
54          if (i <= 0) {
55              throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
56          }
57          return i;
58      }
59  
60      /**
61       * Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
62       * Otherwise, returns the argument.
63       */
64      public static int checkPositiveOrZero(int i, String name) {
65          if (i < 0) {
66              throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
67          }
68          return i;
69      }
70  
71      /**
72       * Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
73       * Otherwise, returns the argument.
74       */
75      public static long checkPositiveOrZero(long i, String name) {
76          if (i < 0) {
77              throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
78          }
79          return i;
80      }
81  
82      /**
83       * Checks that the given argument is neither null nor empty.
84       * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
85       * Otherwise, returns the argument.
86       */
87      public static <T> T[] checkNonEmpty(T[] array, String name) {
88          checkNotNull(array, name);
89          checkPositive(array.length, name + ".length");
90          return array;
91      }
92  
93      /**
94       * Checks that the given argument is neither null nor empty.
95       * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
96       * Otherwise, returns the argument.
97       */
98      public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
99          checkNotNull(collection, name);
100         checkPositive(collection.size(), name + ".size");
101         return collection;
102     }
103 
104     /**
105      * Resolves a possibly null Integer to a primitive int, using a default value.
106      * @param wrapper the wrapper
107      * @param defaultValue the default value
108      * @return the primitive value
109      */
110     public static int intValue(Integer wrapper, int defaultValue) {
111         return wrapper != null ? wrapper : defaultValue;
112     }
113 
114     /**
115      * Resolves a possibly null Long to a primitive long, using a default value.
116      * @param wrapper the wrapper
117      * @param defaultValue the default value
118      * @return the primitive value
119      */
120     public static long longValue(Long wrapper, long defaultValue) {
121         return wrapper != null ? wrapper : defaultValue;
122     }
123 }