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    /**
22191838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the array is not {@code null}, and none of its elements are {@code null}.
222b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
223b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value an array of boxed objects
224b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
225b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
226b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated array
227b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
228b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
229b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
230b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) {
231b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        if (value == null) {
232b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            throw new NullPointerException(valueName + " must not be null");
233b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
234b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
235b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        for (int i = 0; i < value.length; ++i) {
236b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            if (value[i] == null) {
237b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new NullPointerException(
238b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] must not be null", valueName, i));
239b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            }
240b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
241b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
242b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
243b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
244b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
245b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    /**
24691838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the {@link Collection} is not {@code null}, and none of its elements are
24791838ded36131525312739c0929913b215519c2aRuben Brunk     * {@code null}.
24891838ded36131525312739c0929913b215519c2aRuben Brunk     *
24991838ded36131525312739c0929913b215519c2aRuben Brunk     * @param value a {@link Collection} of boxed objects
25091838ded36131525312739c0929913b215519c2aRuben Brunk     * @param valueName the name of the argument to use if the check fails
25191838ded36131525312739c0929913b215519c2aRuben Brunk     *
25291838ded36131525312739c0929913b215519c2aRuben Brunk     * @return the validated {@link Collection}
25391838ded36131525312739c0929913b215519c2aRuben Brunk     *
25491838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
25591838ded36131525312739c0929913b215519c2aRuben Brunk     */
25691838ded36131525312739c0929913b215519c2aRuben Brunk    public static <T> Collection<T> checkCollectionElementsNotNull(final Collection<T> value,
25791838ded36131525312739c0929913b215519c2aRuben Brunk            final String valueName) {
25891838ded36131525312739c0929913b215519c2aRuben Brunk        if (value == null) {
25991838ded36131525312739c0929913b215519c2aRuben Brunk            throw new NullPointerException(valueName + " must not be null");
26091838ded36131525312739c0929913b215519c2aRuben Brunk        }
26191838ded36131525312739c0929913b215519c2aRuben Brunk
26291838ded36131525312739c0929913b215519c2aRuben Brunk        long ctr = 0;
26391838ded36131525312739c0929913b215519c2aRuben Brunk        for (T elem : value) {
26491838ded36131525312739c0929913b215519c2aRuben Brunk            if (elem == null) {
26591838ded36131525312739c0929913b215519c2aRuben Brunk                throw new NullPointerException(
26691838ded36131525312739c0929913b215519c2aRuben Brunk                        String.format("%s[%d] must not be null", valueName, ctr));
26791838ded36131525312739c0929913b215519c2aRuben Brunk            }
26891838ded36131525312739c0929913b215519c2aRuben Brunk            ++ctr;
26991838ded36131525312739c0929913b215519c2aRuben Brunk        }
27091838ded36131525312739c0929913b215519c2aRuben Brunk
27191838ded36131525312739c0929913b215519c2aRuben Brunk        return value;
27291838ded36131525312739c0929913b215519c2aRuben Brunk    }
27391838ded36131525312739c0929913b215519c2aRuben Brunk
27491838ded36131525312739c0929913b215519c2aRuben Brunk    /**
27591838ded36131525312739c0929913b215519c2aRuben Brunk     * Ensures that the {@link Collection} is not {@code null}, and contains at least one element.
27691838ded36131525312739c0929913b215519c2aRuben Brunk     *
27791838ded36131525312739c0929913b215519c2aRuben Brunk     * @param value a {@link Collection} of boxed elements.
27891838ded36131525312739c0929913b215519c2aRuben Brunk     * @param valueName the name of the argument to use if the check fails.
27991838ded36131525312739c0929913b215519c2aRuben Brunk
28091838ded36131525312739c0929913b215519c2aRuben Brunk     * @return the validated {@link Collection}
28191838ded36131525312739c0929913b215519c2aRuben Brunk     *
28291838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws NullPointerException if the {@code value} was {@code null}
28391838ded36131525312739c0929913b215519c2aRuben Brunk     * @throws IllegalArgumentException if the {@code value} was empty
28491838ded36131525312739c0929913b215519c2aRuben Brunk     */
28591838ded36131525312739c0929913b215519c2aRuben Brunk    public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value,
28691838ded36131525312739c0929913b215519c2aRuben Brunk            final String valueName) {
28791838ded36131525312739c0929913b215519c2aRuben Brunk        if (value == null) {
28891838ded36131525312739c0929913b215519c2aRuben Brunk            throw new NullPointerException(valueName + " must not be null");
28991838ded36131525312739c0929913b215519c2aRuben Brunk        }
29091838ded36131525312739c0929913b215519c2aRuben Brunk        if (value.isEmpty()) {
29191838ded36131525312739c0929913b215519c2aRuben Brunk            throw new IllegalArgumentException(valueName + " is empty");
29291838ded36131525312739c0929913b215519c2aRuben Brunk        }
29391838ded36131525312739c0929913b215519c2aRuben Brunk        return value;
29491838ded36131525312739c0929913b215519c2aRuben Brunk    }
29591838ded36131525312739c0929913b215519c2aRuben Brunk
29691838ded36131525312739c0929913b215519c2aRuben Brunk    /**
297b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * Ensures that all elements in the argument floating point array are within the inclusive range
298b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
299b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
300b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * will always be out of range.</p>
301b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
302b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param value a floating point array of values
303b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param lower the lower endpoint of the inclusive range
304b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param upper the upper endpoint of the inclusive range
305b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @param valueName the name of the argument to use if the check fails
306b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
307b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @return the validated floating point value
308b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     *
309b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws IllegalArgumentException if any of the elements in {@code value} were out of range
310b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     * @throws NullPointerException if the {@code value} was {@code null}
311b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin     */
312b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    public static float[] checkArrayElementsInRange(float[] value, float lower, float upper,
313b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            String valueName) {
314b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        checkNotNull(value, valueName + " must not be null");
315b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
316b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        for (int i = 0; i < value.length; ++i) {
317b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            float v = value[i];
318b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
319b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            if (Float.isNaN(v)) {
320b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN");
321b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            } else if (v < lower) {
322b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(
323b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] is out of range of [%f, %f] (too low)",
324b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                                valueName, i, lower, upper));
325b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            } else if (v > upper) {
326b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                throw new IllegalArgumentException(
327b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                        String.format("%s[%d] is out of range of [%f, %f] (too high)",
328b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin                                valueName, i, lower, upper));
329b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin            }
330b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        }
331b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin
332b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin        return value;
333b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin    }
334d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey}
335