Preconditions.java revision 2dd48256e9657b013dd6fa0ca86d1d7c7c730428
1d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey/*
2d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
3d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey *
4d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * you may not use this file except in compliance with the License.
6d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * You may obtain a copy of the License at
7d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey *
8d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey *
10d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
11d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * See the License for the specific language governing permissions and
14d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * limitations under the License.
15d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */
16d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey
17d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkeypackage com.android.internal.util;
18d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey
1991838ded36131525312739c0929913b215519c2aRuben Brunkimport java.util.Collection;
2091838ded36131525312739c0929913b215519c2aRuben Brunk
21d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey/**
22d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Simple static methods to be called at the start of your own methods to verify
23d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * correct arguments and state.
24d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */
25d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkeypublic class Preconditions {
26d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey
27620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey    public static void checkArgument(boolean expression) {
28620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey        if (!expression) {
29620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey            throw new IllegalArgumentException();
30620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey        }
31620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey    }
32620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey
33d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey    /**
34d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * Ensures that an object reference passed as a parameter to the calling
35d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * method is not null.
36d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     *
37d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @param reference an object reference
38d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @return the non-null reference that was validated
39d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @throws NullPointerException if {@code reference} is null
40d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     */
41b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static <T> T checkNotNull(final T reference) {
42d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        if (reference == null) {
43d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey            throw new NullPointerException();
44d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        }
45d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        return reference;
46d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey    }
47d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey
48d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey    /**
49d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * Ensures that an object reference passed as a parameter to the calling
50d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * method is not null.
51d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     *
52d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @param reference an object reference
53d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @param errorMessage the exception message to use if the check fails; will
54d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     *     be converted to a string using {@link String#valueOf(Object)}
55d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @return the non-null reference that was validated
56d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     * @throws NullPointerException if {@code reference} is null
57d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey     */
58b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static <T> T checkNotNull(final T reference, final Object errorMessage) {
59d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        if (reference == null) {
60d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey            throw new NullPointerException(String.valueOf(errorMessage));
61d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        }
62d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        return reference;
63d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey    }
64d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey
65c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey    /**
66c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     * Ensures the truth of an expression involving the state of the calling
67c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     * instance, but not involving any parameters to the calling method.
68c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     *
69c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     * @param expression a boolean expression
70c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     * @throws IllegalStateException if {@code expression} is false
71c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey     */
72b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static void checkState(final boolean expression) {
73c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey        if (!expression) {
74c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey            throw new IllegalStateException();
75c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey        }
76c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey    }
77ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey
78ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey    /**
79ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey     * Check the requested flags, throwing if any requested flags are outside
80ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey     * the allowed set.
81ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey     */
82b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static void checkFlagsArgument(final int requestedFlags, final int allowedFlags) {
83ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey        if ((requestedFlags & allowedFlags) != requestedFlags) {
84ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey            throw new IllegalArgumentException("Requested flags 0x"
85ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey                    + Integer.toHexString(requestedFlags) + ", but only 0x"
86ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey                    + Integer.toHexString(allowedFlags) + " are allowed");
87ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey        }
88ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey    }
89b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
90b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
91b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that that the argument numeric value is non-negative.
92b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
93b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a numeric int value
94b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param errorMessage the exception message to use if the check fails
95b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated numeric value
96b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if {@code value} was negative
97b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
98b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static int checkArgumentNonnegative(final int value, final String errorMessage) {
99b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (value < 0) {
100b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(errorMessage);
101b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
102b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
103b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
104b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
105b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
106b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
107b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that that the argument numeric value is non-negative.
108b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
109b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a numeric long value
110b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param errorMessage the exception message to use if the check fails
111b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated numeric value
112b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if {@code value} was negative
113b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
114b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static long checkArgumentNonnegative(final long value, final String errorMessage) {
115b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (value < 0) {
116b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(errorMessage);
117b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
118b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
119b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
120b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
121b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
122b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
123b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that that the argument numeric value is positive.
124b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
125b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a numeric int value
126b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param errorMessage the exception message to use if the check fails
127b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated numeric value
128b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if {@code value} was not positive
129b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
130b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static int checkArgumentPositive(final int value, final String errorMessage) {
131b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (value <= 0) {
132b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(errorMessage);
133b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
134b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
135b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
136b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
137b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
138b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
139b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that the argument floating point value is a finite number.
140b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
141b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * <p>A finite number is defined to be both representable (that is, not NaN) and
142b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * not infinite (that is neither positive or negative infinity).</p>
143b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
144b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a floating point value
145b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
146b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
147b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated floating point value
148b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
149b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if {@code value} was not finite
150b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
151b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static float checkArgumentFinite(final float value, final String valueName) {
152b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (Float.isNaN(value)) {
153b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(valueName + " must not be NaN");
154b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        } else if (Float.isInfinite(value)) {
155b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(valueName + " must not be infinite");
156b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
157b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
158b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
159b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
160b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
161b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
162b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that the argument floating point value is within the inclusive range.
163b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
164b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
165b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * will always be out of range.</p>
166b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
167b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a floating point value
168b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param lower the lower endpoint of the inclusive range
169b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param upper the upper endpoint of the inclusive range
170b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
171b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
172b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated floating point value
173b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
174b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if {@code value} was not within the range
175b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
176b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static float checkArgumentInRange(float value, float lower, float upper,
177b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            String valueName) {
178b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (Float.isNaN(value)) {
179b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(valueName + " must not be NaN");
180b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        } else if (value < lower) {
181b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(
182b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                    String.format(
183b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                            "%s is out of range of [%f, %f] (too low)", valueName, lower, upper));
184b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        } else if (value > upper) {
185b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new IllegalArgumentException(
186b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                    String.format(
187b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                            "%s is out of range of [%f, %f] (too high)", valueName, lower, upper));
188b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
189b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
190b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
191b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
192b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
193b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
19497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * Ensures that the argument int value is within the inclusive range.
19597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     *
19697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @param value a int value
19797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @param lower the lower endpoint of the inclusive range
19897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @param upper the upper endpoint of the inclusive range
19997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @param valueName the name of the argument to use if the check fails
20097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     *
20197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @return the validated int value
20297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     *
20397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     * @throws IllegalArgumentException if {@code value} was not within the range
20497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh     */
20597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh    public static int checkArgumentInRange(int value, int lower, int upper,
20697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh            String valueName) {
20797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh        if (value < lower) {
20897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh            throw new IllegalArgumentException(
20997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh                    String.format(
21097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh                            "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
21197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh        } else if (value > upper) {
21297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh            throw new IllegalArgumentException(
21397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh                    String.format(
21497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh                            "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
21597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh        }
21697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh
21797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh        return value;
21897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh    }
21997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh
22097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh    /**
2212dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * Ensures that the argument long value is within the inclusive range.
2222dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     *
2232dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @param value a long value
2242dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @param lower the lower endpoint of the inclusive range
2252dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @param upper the upper endpoint of the inclusive range
2262dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @param valueName the name of the argument to use if the check fails
2272dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     *
2282dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @return the validated long value
2292dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     *
2302dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     * @throws IllegalArgumentException if {@code value} was not within the range
2312dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono     */
2322dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono    public static long checkArgumentInRange(long value, long lower, long upper,
2332dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono            String valueName) {
2342dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono        if (value < lower) {
2352dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono            throw new IllegalArgumentException(
2362dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono                    String.format(
2372dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono                            "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
2382dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono        } else if (value > upper) {
2392dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono            throw new IllegalArgumentException(
2402dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono                    String.format(
2412dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono                            "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
2422dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono        }
2432dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono
2442dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono        return value;
2452dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono    }
2462dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono
2472dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono    /**
24891838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the array is not {@code null}, and none of its elements are {@code null}.
249b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
250b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value an array of boxed objects
251b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
252b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
253b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated array
254b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
255b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
256b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
257b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) {
258b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (value == null) {
259b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new NullPointerException(valueName + " must not be null");
260b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
261b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
262b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        for (int i = 0; i < value.length; ++i) {
263b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            if (value[i] == null) {
264b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new NullPointerException(
265b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] must not be null", valueName, i));
266b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            }
267b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
268b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
269b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
270b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
271b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
272b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
27391838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the {@link Collection} is not {@code null}, and none of its elements are
27491838ded36131525312739c0929913b215519c2aRuben Brunk     * {@code null}.
27591838ded36131525312739c0929913b215519c2aRuben Brunk     *
27691838ded36131525312739c0929913b215519c2aRuben Brunk     * @param value a {@link Collection} of boxed objects
27791838ded36131525312739c0929913b215519c2aRuben Brunk     * @param valueName the name of the argument to use if the check fails
27891838ded36131525312739c0929913b215519c2aRuben Brunk     *
27991838ded36131525312739c0929913b215519c2aRuben Brunk     * @return the validated {@link Collection}
28091838ded36131525312739c0929913b215519c2aRuben Brunk     *
28191838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
28291838ded36131525312739c0929913b215519c2aRuben Brunk     */
28391838ded36131525312739c0929913b215519c2aRuben Brunk    public static <T> Collection<T> checkCollectionElementsNotNull(final Collection<T> value,
28491838ded36131525312739c0929913b215519c2aRuben Brunk            final String valueName) {
28591838ded36131525312739c0929913b215519c2aRuben Brunk        if (value == null) {
28691838ded36131525312739c0929913b215519c2aRuben Brunk            throw new NullPointerException(valueName + " must not be null");
28791838ded36131525312739c0929913b215519c2aRuben Brunk        }
28891838ded36131525312739c0929913b215519c2aRuben Brunk
28991838ded36131525312739c0929913b215519c2aRuben Brunk        long ctr = 0;
29091838ded36131525312739c0929913b215519c2aRuben Brunk        for (T elem : value) {
29191838ded36131525312739c0929913b215519c2aRuben Brunk            if (elem == null) {
29291838ded36131525312739c0929913b215519c2aRuben Brunk                throw new NullPointerException(
29391838ded36131525312739c0929913b215519c2aRuben Brunk                        String.format("%s[%d] must not be null", valueName, ctr));
29491838ded36131525312739c0929913b215519c2aRuben Brunk            }
29591838ded36131525312739c0929913b215519c2aRuben Brunk            ++ctr;
29691838ded36131525312739c0929913b215519c2aRuben Brunk        }
29791838ded36131525312739c0929913b215519c2aRuben Brunk
29891838ded36131525312739c0929913b215519c2aRuben Brunk        return value;
29991838ded36131525312739c0929913b215519c2aRuben Brunk    }
30091838ded36131525312739c0929913b215519c2aRuben Brunk
30191838ded36131525312739c0929913b215519c2aRuben Brunk    /**
30291838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the {@link Collection} is not {@code null}, and contains at least one element.
30391838ded36131525312739c0929913b215519c2aRuben Brunk     *
30491838ded36131525312739c0929913b215519c2aRuben Brunk     * @param value a {@link Collection} of boxed elements.
30591838ded36131525312739c0929913b215519c2aRuben Brunk     * @param valueName the name of the argument to use if the check fails.
30691838ded36131525312739c0929913b215519c2aRuben Brunk
30791838ded36131525312739c0929913b215519c2aRuben Brunk     * @return the validated {@link Collection}
30891838ded36131525312739c0929913b215519c2aRuben Brunk     *
30991838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws NullPointerException if the {@code value} was {@code null}
31091838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws IllegalArgumentException if the {@code value} was empty
31191838ded36131525312739c0929913b215519c2aRuben Brunk     */
31291838ded36131525312739c0929913b215519c2aRuben Brunk    public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value,
31391838ded36131525312739c0929913b215519c2aRuben Brunk            final String valueName) {
31491838ded36131525312739c0929913b215519c2aRuben Brunk        if (value == null) {
31591838ded36131525312739c0929913b215519c2aRuben Brunk            throw new NullPointerException(valueName + " must not be null");
31691838ded36131525312739c0929913b215519c2aRuben Brunk        }
31791838ded36131525312739c0929913b215519c2aRuben Brunk        if (value.isEmpty()) {
31891838ded36131525312739c0929913b215519c2aRuben Brunk            throw new IllegalArgumentException(valueName + " is empty");
31991838ded36131525312739c0929913b215519c2aRuben Brunk        }
32091838ded36131525312739c0929913b215519c2aRuben Brunk        return value;
32191838ded36131525312739c0929913b215519c2aRuben Brunk    }
32291838ded36131525312739c0929913b215519c2aRuben Brunk
32391838ded36131525312739c0929913b215519c2aRuben Brunk    /**
324b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that all elements in the argument floating point array are within the inclusive range
325b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
326b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
327b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * will always be out of range.</p>
328b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
329b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a floating point array of values
330b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param lower the lower endpoint of the inclusive range
331b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param upper the upper endpoint of the inclusive range
332b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
333b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
334b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated floating point value
335b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
336b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if any of the elements in {@code value} were out of range
337b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws NullPointerException if the {@code value} was {@code null}
338b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
339b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static float[] checkArrayElementsInRange(float[] value, float lower, float upper,
340b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            String valueName) {
341b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        checkNotNull(value, valueName + " must not be null");
342b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
343b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        for (int i = 0; i < value.length; ++i) {
344b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            float v = value[i];
345b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
346b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            if (Float.isNaN(v)) {
347b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN");
348b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            } else if (v < lower) {
349b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(
350b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] is out of range of [%f, %f] (too low)",
351b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                                valueName, i, lower, upper));
352b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            } else if (v > upper) {
353b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(
354b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] is out of range of [%f, %f] (too high)",
355b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                                valueName, i, lower, upper));
356b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            }
357b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
358b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
359b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
360b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
361d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey}
362