• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 package org.tensorflow.lite.support.common;
17 
18 import org.checkerframework.checker.nullness.qual.Nullable;
19 
20 /** Static error checking util methods. */
21 public final class SupportPreconditions {
22   /**
23    * Ensures that an object reference passed as a parameter to the calling method is not null.
24    *
25    * @param reference an object reference
26    * @return the non-null reference that was validated
27    * @throws NullPointerException if {@code reference} is null
28    */
checkNotNull(T reference)29   public static <T extends Object> T checkNotNull(T reference) {
30     if (reference == null) {
31       throw new NullPointerException("The object reference is null.");
32     }
33     return reference;
34   }
35 
36   /**
37    * Ensures that an object reference passed as a parameter to the calling method is not null.
38    *
39    * @param reference an object reference
40    * @param errorMessage the exception message to use if the check fails; will be converted to a
41    *     string using {@link String#valueOf(Object)}
42    * @return the non-null reference that was validated
43    * @throws NullPointerException if {@code reference} is null
44    */
checkNotNull(T reference, @Nullable Object errorMessage)45   public static <T extends Object> T checkNotNull(T reference, @Nullable Object errorMessage) {
46     if (reference == null) {
47       throw new NullPointerException(String.valueOf(errorMessage));
48     }
49     return reference;
50   }
51 
52   /**
53    * Ensures that the given String is not empty and not null.
54    *
55    * @param string the String to test
56    * @return the non-null non-empty String that was validated
57    * @throws IllegalArgumentException if {@code string} is null or empty
58    */
checkNotEmpty(String string)59   public static String checkNotEmpty(String string) {
60     if (string == null || string.length() == 0) {
61       throw new IllegalArgumentException("Given String is empty or null.");
62     }
63     return string;
64   }
65 
66   /**
67    * Ensures that the given String is not empty and not null.
68    *
69    * @param string the String to test
70    * @param errorMessage the exception message to use if the check fails; will be converted to a
71    *     string using {@link String#valueOf(Object)}
72    * @return the non-null non-empty String that was validated
73    * @throws IllegalArgumentException if {@code string} is null or empty
74    */
checkNotEmpty(String string, Object errorMessage)75   public static String checkNotEmpty(String string, Object errorMessage) {
76     if (string == null || string.length() == 0) {
77       throw new IllegalArgumentException(String.valueOf(errorMessage));
78     }
79     return string;
80   }
81 
82   /**
83    * Ensures the truth of an expression involving one or more parameters to the calling method.
84    *
85    * @param expression a boolean expression.
86    * @throws IllegalArgumentException if {@code expression} is false.
87    */
checkArgument(boolean expression)88   public static void checkArgument(boolean expression) {
89     if (!expression) {
90       throw new IllegalArgumentException();
91     }
92   }
93 
94   /**
95    * Ensures the truth of an expression involving one or more parameters to the calling method.
96    *
97    * @param expression a boolean expression.
98    * @param errorMessage the exception message to use if the check fails; will be converted to a
99    *     string using {@link String#valueOf(Object)}.
100    * @throws IllegalArgumentException if {@code expression} is false.
101    */
checkArgument(boolean expression, @Nullable Object errorMessage)102   public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
103     if (!expression) {
104       throw new IllegalArgumentException(String.valueOf(errorMessage));
105     }
106   }
107 
108   /**
109    * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
110    * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
111    *
112    * @param index a user-supplied index identifying an element of an array, list or string
113    * @param size the size of that array, list or string
114    * @return the value of {@code index}
115    * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
116    * @throws IllegalArgumentException if {@code size} is negative
117    */
checkElementIndex(int index, int size)118   public static int checkElementIndex(int index, int size) {
119     return checkElementIndex(index, size, "index");
120   }
121 
122   /**
123    * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
124    * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
125    *
126    * @param index a user-supplied index identifying an element of an array, list or string
127    * @param size the size of that array, list or string
128    * @param desc the text to use to describe this index in an error message
129    * @return the value of {@code index}
130    * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
131    * @throws IllegalArgumentException if {@code size} is negative
132    */
checkElementIndex(int index, int size, @Nullable String desc)133   public static int checkElementIndex(int index, int size, @Nullable String desc) {
134     // Carefully optimized for execution by hotspot (explanatory comment above)
135     if (index < 0 || index >= size) {
136       throw new IndexOutOfBoundsException(badElementIndex(index, size, desc));
137     }
138     return index;
139   }
140 
141   /**
142    * Ensures the truth of an expression involving the state of the calling instance, but not
143    * involving any parameters to the calling method.
144    *
145    * @param expression a boolean expression
146    * @throws IllegalStateException if {@code expression} is false
147    * @see Verify#verify Verify.verify()
148    */
checkState(boolean expression)149   public static void checkState(boolean expression) {
150     if (!expression) {
151       throw new IllegalStateException();
152     }
153   }
154 
155   /**
156    * Ensures the truth of an expression involving the state of the calling instance, but not
157    * involving any parameters to the calling method.
158    *
159    * @param expression a boolean expression
160    * @param errorMessage the exception message to use if the check fails; will be converted to a
161    *     string using {@link String#valueOf(Object)}
162    * @throws IllegalStateException if {@code expression} is false
163    * @see Verify#verify Verify.verify()
164    */
checkState(boolean expression, @Nullable Object errorMessage)165   public static void checkState(boolean expression, @Nullable Object errorMessage) {
166     if (!expression) {
167       throw new IllegalStateException(String.valueOf(errorMessage));
168     }
169   }
170 
badElementIndex(int index, int size, @Nullable String desc)171   private static String badElementIndex(int index, int size, @Nullable String desc) {
172     if (index < 0) {
173       return String.format("%s (%s) must not be negative", desc, index);
174     } else if (size < 0) {
175       throw new IllegalArgumentException("negative size: " + size);
176     } else { // index >= size
177       return String.format("%s (%s) must be less than size (%s)", desc, index, size);
178     }
179   }
180 
SupportPreconditions()181   private SupportPreconditions() {
182     throw new AssertionError("SupportPreconditions is Uninstantiable.");
183   }
184 }
185