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