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 27d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey /** 28d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Ensures that an object reference passed as a parameter to the calling 29d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * method is not null. 30d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * 31d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param reference an object reference 32d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @return the non-null reference that was validated 33d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @throws NullPointerException if {@code reference} is null 34d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */ 35b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static <T> T checkNotNull(final T reference) { 36d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey if (reference == null) { 37d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey throw new NullPointerException(); 38d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 39d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey return reference; 40d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 41d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey 42d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey /** 43d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Ensures that an object reference passed as a parameter to the calling 44d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * method is not null. 45d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * 46d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param reference an object reference 47d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param errorMessage the exception message to use if the check fails; will 48d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * be converted to a string using {@link String#valueOf(Object)} 49d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @return the non-null reference that was validated 50d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @throws NullPointerException if {@code reference} is null 51d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */ 52b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static <T> T checkNotNull(final T reference, final Object errorMessage) { 53d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey if (reference == null) { 54d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey throw new NullPointerException(String.valueOf(errorMessage)); 55d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 56d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey return reference; 57d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 58d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey 59c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey /** 60c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * Ensures the truth of an expression involving the state of the calling 61c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * instance, but not involving any parameters to the calling method. 62c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * 63c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * @param expression a boolean expression 64c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * @throws IllegalStateException if {@code expression} is false 65c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey */ 66b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static void checkState(final boolean expression) { 67c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey if (!expression) { 68c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey throw new IllegalStateException(); 69c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey } 70c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey } 71ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey 72ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey /** 73ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey * Check the requested flags, throwing if any requested flags are outside 74ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey * the allowed set. 75ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey */ 76b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static void checkFlagsArgument(final int requestedFlags, final int allowedFlags) { 77ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey if ((requestedFlags & allowedFlags) != requestedFlags) { 78ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey throw new IllegalArgumentException("Requested flags 0x" 79ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey + Integer.toHexString(requestedFlags) + ", but only 0x" 80ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey + Integer.toHexString(allowedFlags) + " are allowed"); 81ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey } 82ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey } 83b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 84b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 85b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is non-negative. 86b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 87b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric int value 88b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 89b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 90b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was negative 91b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 92b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static int checkArgumentNonnegative(final int value, final String errorMessage) { 93b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value < 0) { 94b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 95b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 96b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 97b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 98b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 99b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 100b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 101b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is non-negative. 102b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 103b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric long value 104b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 105b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 106b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was negative 107b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 108b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static long checkArgumentNonnegative(final long value, final String errorMessage) { 109b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value < 0) { 110b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 111b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 112b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 113b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 114b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 115b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 116b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 117b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is positive. 118b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 119b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric int value 120b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 121b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 122b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not positive 123b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 124b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static int checkArgumentPositive(final int value, final String errorMessage) { 125b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value <= 0) { 126b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 127b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 128b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 129b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 130b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 131b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 132b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 133b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that the argument floating point value is a finite number. 134b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 135b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>A finite number is defined to be both representable (that is, not NaN) and 136b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * not infinite (that is neither positive or negative infinity).</p> 137b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 138b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point value 139b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 140b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 141b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 142b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 143b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not finite 144b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 145b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float checkArgumentFinite(final float value, final String valueName) { 146b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(value)) { 147b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be NaN"); 148b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (Float.isInfinite(value)) { 149b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be infinite"); 150b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 151b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 152b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 153b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 154b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 155b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 156b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that the argument floating point value is within the inclusive range. 157b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 158b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 159b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * will always be out of range.</p> 160b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 161b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point value 162b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param lower the lower endpoint of the inclusive range 163b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param upper the upper endpoint of the inclusive range 164b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 165b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 166b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 167b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 168b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not within the range 169b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 170b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float checkArgumentInRange(float value, float lower, float upper, 171b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String valueName) { 172b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(value)) { 173b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be NaN"); 174b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (value < lower) { 175b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 176b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format( 177b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin "%s is out of range of [%f, %f] (too low)", valueName, lower, upper)); 178b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (value > upper) { 179b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 180b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format( 181b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin "%s is out of range of [%f, %f] (too high)", valueName, lower, upper)); 182b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 183b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 184b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 185b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 186b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 187b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 18897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * Ensures that the argument int value is within the inclusive range. 18997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 19097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param value a int value 19197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param lower the lower endpoint of the inclusive range 19297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param upper the upper endpoint of the inclusive range 19397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param valueName the name of the argument to use if the check fails 19497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 19597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @return the validated int value 19697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 19797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @throws IllegalArgumentException if {@code value} was not within the range 19897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh */ 19997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh public static int checkArgumentInRange(int value, int lower, int upper, 20097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String valueName) { 20197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh if (value < lower) { 20297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh throw new IllegalArgumentException( 20397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String.format( 20497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); 20597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } else if (value > upper) { 20697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh throw new IllegalArgumentException( 20797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String.format( 20897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); 20997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } 21097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh 21197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh return value; 21297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } 21397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh 21497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh /** 21591838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the array is not {@code null}, and none of its elements are {@code null}. 216b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 217b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value an array of boxed objects 218b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 219b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 220b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated array 221b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 222b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 223b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 224b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) { 225b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value == null) { 226b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new NullPointerException(valueName + " must not be null"); 227b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 228b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 229b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin for (int i = 0; i < value.length; ++i) { 230b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value[i] == null) { 231b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new NullPointerException( 232b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] must not be null", valueName, i)); 233b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 234b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 235b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 236b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 237b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 238b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 239b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 24091838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the {@link Collection} is not {@code null}, and none of its elements are 24191838ded36131525312739c0929913b215519c2aRuben Brunk * {@code null}. 24291838ded36131525312739c0929913b215519c2aRuben Brunk * 24391838ded36131525312739c0929913b215519c2aRuben Brunk * @param value a {@link Collection} of boxed objects 24491838ded36131525312739c0929913b215519c2aRuben Brunk * @param valueName the name of the argument to use if the check fails 24591838ded36131525312739c0929913b215519c2aRuben Brunk * 24691838ded36131525312739c0929913b215519c2aRuben Brunk * @return the validated {@link Collection} 24791838ded36131525312739c0929913b215519c2aRuben Brunk * 24891838ded36131525312739c0929913b215519c2aRuben Brunk * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 24991838ded36131525312739c0929913b215519c2aRuben Brunk */ 25091838ded36131525312739c0929913b215519c2aRuben Brunk public static <T> Collection<T> checkCollectionElementsNotNull(final Collection<T> value, 25191838ded36131525312739c0929913b215519c2aRuben Brunk final String valueName) { 25291838ded36131525312739c0929913b215519c2aRuben Brunk if (value == null) { 25391838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException(valueName + " must not be null"); 25491838ded36131525312739c0929913b215519c2aRuben Brunk } 25591838ded36131525312739c0929913b215519c2aRuben Brunk 25691838ded36131525312739c0929913b215519c2aRuben Brunk long ctr = 0; 25791838ded36131525312739c0929913b215519c2aRuben Brunk for (T elem : value) { 25891838ded36131525312739c0929913b215519c2aRuben Brunk if (elem == null) { 25991838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException( 26091838ded36131525312739c0929913b215519c2aRuben Brunk String.format("%s[%d] must not be null", valueName, ctr)); 26191838ded36131525312739c0929913b215519c2aRuben Brunk } 26291838ded36131525312739c0929913b215519c2aRuben Brunk ++ctr; 26391838ded36131525312739c0929913b215519c2aRuben Brunk } 26491838ded36131525312739c0929913b215519c2aRuben Brunk 26591838ded36131525312739c0929913b215519c2aRuben Brunk return value; 26691838ded36131525312739c0929913b215519c2aRuben Brunk } 26791838ded36131525312739c0929913b215519c2aRuben Brunk 26891838ded36131525312739c0929913b215519c2aRuben Brunk /** 26991838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the {@link Collection} is not {@code null}, and contains at least one element. 27091838ded36131525312739c0929913b215519c2aRuben Brunk * 27191838ded36131525312739c0929913b215519c2aRuben Brunk * @param value a {@link Collection} of boxed elements. 27291838ded36131525312739c0929913b215519c2aRuben Brunk * @param valueName the name of the argument to use if the check fails. 27391838ded36131525312739c0929913b215519c2aRuben Brunk 27491838ded36131525312739c0929913b215519c2aRuben Brunk * @return the validated {@link Collection} 27591838ded36131525312739c0929913b215519c2aRuben Brunk * 27691838ded36131525312739c0929913b215519c2aRuben Brunk * @throws NullPointerException if the {@code value} was {@code null} 27791838ded36131525312739c0929913b215519c2aRuben Brunk * @throws IllegalArgumentException if the {@code value} was empty 27891838ded36131525312739c0929913b215519c2aRuben Brunk */ 27991838ded36131525312739c0929913b215519c2aRuben Brunk public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value, 28091838ded36131525312739c0929913b215519c2aRuben Brunk final String valueName) { 28191838ded36131525312739c0929913b215519c2aRuben Brunk if (value == null) { 28291838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException(valueName + " must not be null"); 28391838ded36131525312739c0929913b215519c2aRuben Brunk } 28491838ded36131525312739c0929913b215519c2aRuben Brunk if (value.isEmpty()) { 28591838ded36131525312739c0929913b215519c2aRuben Brunk throw new IllegalArgumentException(valueName + " is empty"); 28691838ded36131525312739c0929913b215519c2aRuben Brunk } 28791838ded36131525312739c0929913b215519c2aRuben Brunk return value; 28891838ded36131525312739c0929913b215519c2aRuben Brunk } 28991838ded36131525312739c0929913b215519c2aRuben Brunk 29091838ded36131525312739c0929913b215519c2aRuben Brunk /** 291b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that all elements in the argument floating point array are within the inclusive range 292b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 293b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 294b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * will always be out of range.</p> 295b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 296b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point array of values 297b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param lower the lower endpoint of the inclusive range 298b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param upper the upper endpoint of the inclusive range 299b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 300b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 301b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 302b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 303b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if any of the elements in {@code value} were out of range 304b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws NullPointerException if the {@code value} was {@code null} 305b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 306b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float[] checkArrayElementsInRange(float[] value, float lower, float upper, 307b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String valueName) { 308b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin checkNotNull(value, valueName + " must not be null"); 309b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 310b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin for (int i = 0; i < value.length; ++i) { 311b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin float v = value[i]; 312b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 313b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(v)) { 314b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN"); 315b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (v < lower) { 316b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 317b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] is out of range of [%f, %f] (too low)", 318b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin valueName, i, lower, upper)); 319b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (v > upper) { 320b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 321b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] is out of range of [%f, %f] (too high)", 322b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin valueName, i, lower, upper)); 323b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 324b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 325b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 326b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 327b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 328d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey} 329