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 19c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmannimport android.annotation.IntRange; 20c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmannimport android.annotation.NonNull; 21c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmannimport android.text.TextUtils; 22c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann 2391838ded36131525312739c0929913b215519c2aRuben Brunkimport java.util.Collection; 2491838ded36131525312739c0929913b215519c2aRuben Brunk 25d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey/** 26d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Simple static methods to be called at the start of your own methods to verify 27d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * correct arguments and state. 28d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */ 29d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkeypublic class Preconditions { 30d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey 31620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey public static void checkArgument(boolean expression) { 32620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey if (!expression) { 33620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey throw new IllegalArgumentException(); 34620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey } 35620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey } 36620b32b316fd4f1bab4eef55ec8802d14a55e7ddJeff Sharkey 37d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey /** 38d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * Ensures that an expression checking an argument is true. 39d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * 40d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * @param expression the expression to check 41d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * @param errorMessage the exception message to use if the check fails; will 42d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * be converted to a string using {@link String#valueOf(Object)} 43d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann * @throws IllegalArgumentException if {@code expression} is false 44d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann */ 45d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann public static void checkArgument(boolean expression, final Object errorMessage) { 46d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann if (!expression) { 47d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann throw new IllegalArgumentException(String.valueOf(errorMessage)); 48d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann } 49d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann } 50d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann 51d74d1e549168ba521e8009961b76e8718be37aa1Philip P. Moltmann /** 52c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * Ensures that an string reference passed as a parameter to the calling 53c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * method is not empty. 54c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * 55c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * @param string an string reference 56c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * @return the string reference that was validated 57c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann * @throws IllegalArgumentException if {@code string} is empty 58c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann */ 599dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string) { 6076d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann if (TextUtils.isEmpty(string)) { 6176d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann throw new IllegalArgumentException(); 6276d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann } 6376d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann return string; 6476d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann } 6576d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann 6676d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann /** 6776d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * Ensures that an string reference passed as a parameter to the calling 6876d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * method is not empty. 6976d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * 7076d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * @param string an string reference 7176d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * @param errorMessage the exception message to use if the check fails; will 7276d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * be converted to a string using {@link String#valueOf(Object)} 7376d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * @return the string reference that was validated 7476d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann * @throws IllegalArgumentException if {@code string} is empty 7576d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann */ 769dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string, 77c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann final Object errorMessage) { 78c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann if (TextUtils.isEmpty(string)) { 79c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann throw new IllegalArgumentException(String.valueOf(errorMessage)); 80c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann } 81c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann return string; 82c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann } 83c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann 84c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann /** 85d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Ensures that an object reference passed as a parameter to the calling 86d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * method is not null. 87d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * 88d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param reference an object reference 89d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @return the non-null reference that was validated 90d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @throws NullPointerException if {@code reference} is null 91d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */ 92c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann public static @NonNull <T> T checkNotNull(final T reference) { 93d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey if (reference == null) { 94d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey throw new NullPointerException(); 95d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 96d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey return reference; 97d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 98d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey 99d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey /** 100d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * Ensures that an object reference passed as a parameter to the calling 101d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * method is not null. 102d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * 103d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param reference an object reference 104d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @param errorMessage the exception message to use if the check fails; will 105d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * be converted to a string using {@link String#valueOf(Object)} 106d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @return the non-null reference that was validated 107d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey * @throws NullPointerException if {@code reference} is null 108d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey */ 109c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann public static @NonNull <T> T checkNotNull(final T reference, final Object errorMessage) { 110d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey if (reference == null) { 111d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey throw new NullPointerException(String.valueOf(errorMessage)); 112d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 113d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey return reference; 114d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey } 115d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey 116c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey /** 117c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * Ensures the truth of an expression involving the state of the calling 118c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * instance, but not involving any parameters to the calling method. 119c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * 120c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * @param expression a boolean expression 1211a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * @param message exception message 122c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey * @throws IllegalStateException if {@code expression} is false 123c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey */ 1241a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki public static void checkState(final boolean expression, String message) { 125c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey if (!expression) { 1261a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki throw new IllegalStateException(message); 127c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey } 128c268f0b19efd0b6c6c89c21be0893787f3cc9cf7Jeff Sharkey } 129ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey 130ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey /** 1311a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * Ensures the truth of an expression involving the state of the calling 1321a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * instance, but not involving any parameters to the calling method. 1331a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * 1341a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * @param expression a boolean expression 1351a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki * @throws IllegalStateException if {@code expression} is false 1361a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki */ 1371a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki public static void checkState(final boolean expression) { 1381a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki checkState(expression, null); 1391a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki } 1401a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki 1411a5ee776ee51ae6fba30c8f3b33e26eb7f9dedc6Makoto Onuki /** 142ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey * Check the requested flags, throwing if any requested flags are outside 143ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey * the allowed set. 1449dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * 1459dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * @return the validated requested flags. 146ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey */ 1479dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) { 148ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey if ((requestedFlags & allowedFlags) != requestedFlags) { 149ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey throw new IllegalArgumentException("Requested flags 0x" 150ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey + Integer.toHexString(requestedFlags) + ", but only 0x" 151ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey + Integer.toHexString(allowedFlags) + " are allowed"); 152ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey } 1539dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann 1549dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann return requestedFlags; 155ee2f7df9ee8a4f43c3b0858bad08a4f0a59a627fJeff Sharkey } 156b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 157b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 158b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is non-negative. 159b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 160b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric int value 161b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 162b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 163b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was negative 164b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 165c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann public static @IntRange(from = 0) int checkArgumentNonnegative(final int value, 166c2ad22663ba8cbd0ceb35e760c5f3c4084fb5033Philip P. Moltmann final String errorMessage) { 167b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value < 0) { 168b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 169b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 170b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 171b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 172b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 173b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 174b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 175b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is non-negative. 176b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 1779dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * @param value a numeric int value 1789dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * 1799dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * @return the validated numeric value 1809dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * @throws IllegalArgumentException if {@code value} was negative 1819dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann */ 1829dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann public static @IntRange(from = 0) int checkArgumentNonnegative(final int value) { 1839dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann if (value < 0) { 1849dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann throw new IllegalArgumentException(); 1859dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann } 1869dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann 1879dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann return value; 1889dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann } 1899dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann 1909dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann /** 1919dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * Ensures that that the argument numeric value is non-negative. 1929dcb86a48d73f399fb1b5c020005d76d350eeac2Philip P. Moltmann * 193b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric long value 1944723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann * @return the validated numeric value 1954723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann * @throws IllegalArgumentException if {@code value} was negative 1964723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann */ 1974723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann public static long checkArgumentNonnegative(final long value) { 1984723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann if (value < 0) { 1994723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann throw new IllegalArgumentException(); 2004723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann } 2014723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann 2024723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann return value; 2034723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann } 2044723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann 2054723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann /** 2064723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann * Ensures that that the argument numeric value is non-negative. 2074723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann * 2084723f36d3e93ce4585233de31c72143a46b657f7Philip P. Moltmann * @param value a numeric long value 209b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 210b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 211b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was negative 212b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 213b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static long checkArgumentNonnegative(final long value, final String errorMessage) { 214b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value < 0) { 215b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 216b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 217b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 218b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 219b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 220b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 221b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 222b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that that the argument numeric value is positive. 223b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 224b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a numeric int value 225b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param errorMessage the exception message to use if the check fails 226b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated numeric value 227b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not positive 228b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 229b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static int checkArgumentPositive(final int value, final String errorMessage) { 230b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value <= 0) { 231b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(errorMessage); 232b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 233b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 234b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 235b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 236b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 237b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 238b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that the argument floating point value is a finite number. 239b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 240b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>A finite number is defined to be both representable (that is, not NaN) and 241b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * not infinite (that is neither positive or negative infinity).</p> 242b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 243b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point value 244b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 245b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 246b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 247b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 248b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not finite 249b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 250b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float checkArgumentFinite(final float value, final String valueName) { 251b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(value)) { 252b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be NaN"); 253b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (Float.isInfinite(value)) { 254b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be infinite"); 255b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 256b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 257b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 258b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 259b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 260b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 261b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that the argument floating point value is within the inclusive range. 262b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 263b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 264b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * will always be out of range.</p> 265b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 266b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point value 267b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param lower the lower endpoint of the inclusive range 268b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param upper the upper endpoint of the inclusive range 269b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 270b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 271b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 272b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 273b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if {@code value} was not within the range 274b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 275b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float checkArgumentInRange(float value, float lower, float upper, 276b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String valueName) { 277b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(value)) { 278b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + " must not be NaN"); 279b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (value < lower) { 280b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 281b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format( 282b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin "%s is out of range of [%f, %f] (too low)", valueName, lower, upper)); 283b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (value > upper) { 284b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 285b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format( 286b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin "%s is out of range of [%f, %f] (too high)", valueName, lower, upper)); 287b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 288b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 289b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 290b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 291b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 292b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 29397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * Ensures that the argument int value is within the inclusive range. 29497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 29597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param value a int value 29697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param lower the lower endpoint of the inclusive range 29797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param upper the upper endpoint of the inclusive range 29897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @param valueName the name of the argument to use if the check fails 29997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 30097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @return the validated int value 30197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * 30297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh * @throws IllegalArgumentException if {@code value} was not within the range 30397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh */ 30497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh public static int checkArgumentInRange(int value, int lower, int upper, 30597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String valueName) { 30697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh if (value < lower) { 30797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh throw new IllegalArgumentException( 30897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String.format( 30997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); 31097f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } else if (value > upper) { 31197f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh throw new IllegalArgumentException( 31297f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh String.format( 31397f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); 31497f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } 31597f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh 31697f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh return value; 31797f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh } 31897f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh 31997f1c854993a65b2c700426a1e3a83b23ea65337Yin-Chia Yeh /** 3202dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * Ensures that the argument long value is within the inclusive range. 3212dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * 3222dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @param value a long value 3232dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @param lower the lower endpoint of the inclusive range 3242dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @param upper the upper endpoint of the inclusive range 3252dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @param valueName the name of the argument to use if the check fails 3262dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * 3272dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @return the validated long value 3282dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * 3292dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono * @throws IllegalArgumentException if {@code value} was not within the range 3302dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono */ 3312dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono public static long checkArgumentInRange(long value, long lower, long upper, 3322dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono String valueName) { 3332dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono if (value < lower) { 3342dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono throw new IllegalArgumentException( 3352dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono String.format( 3362dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); 3372dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono } else if (value > upper) { 3382dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono throw new IllegalArgumentException( 3392dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono String.format( 3402dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); 3412dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono } 3422dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono 3432dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono return value; 3442dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono } 3452dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono 3462dd48256e9657b013dd6fa0ca86d1d7c7c730428Daichi Hirono /** 34791838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the array is not {@code null}, and none of its elements are {@code null}. 348b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 349b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value an array of boxed objects 350b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 351b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 352b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated array 353b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 354b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 355b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 356b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) { 357b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value == null) { 358b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new NullPointerException(valueName + " must not be null"); 359b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 360b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 361b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin for (int i = 0; i < value.length; ++i) { 362b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (value[i] == null) { 363b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new NullPointerException( 364b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] must not be null", valueName, i)); 365b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 366b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 367b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 368b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 369b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 370b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 371b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin /** 37291838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the {@link Collection} is not {@code null}, and none of its elements are 37391838ded36131525312739c0929913b215519c2aRuben Brunk * {@code null}. 37491838ded36131525312739c0929913b215519c2aRuben Brunk * 37591838ded36131525312739c0929913b215519c2aRuben Brunk * @param value a {@link Collection} of boxed objects 37691838ded36131525312739c0929913b215519c2aRuben Brunk * @param valueName the name of the argument to use if the check fails 37791838ded36131525312739c0929913b215519c2aRuben Brunk * 37891838ded36131525312739c0929913b215519c2aRuben Brunk * @return the validated {@link Collection} 37991838ded36131525312739c0929913b215519c2aRuben Brunk * 38091838ded36131525312739c0929913b215519c2aRuben Brunk * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 38191838ded36131525312739c0929913b215519c2aRuben Brunk */ 38276d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann public static @NonNull <C extends Collection<T>, T> C checkCollectionElementsNotNull( 38376d7e3ee70c4299b22b1a03505d2b4f108716c75Philip P. Moltmann final C value, final String valueName) { 38491838ded36131525312739c0929913b215519c2aRuben Brunk if (value == null) { 38591838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException(valueName + " must not be null"); 38691838ded36131525312739c0929913b215519c2aRuben Brunk } 38791838ded36131525312739c0929913b215519c2aRuben Brunk 38891838ded36131525312739c0929913b215519c2aRuben Brunk long ctr = 0; 38991838ded36131525312739c0929913b215519c2aRuben Brunk for (T elem : value) { 39091838ded36131525312739c0929913b215519c2aRuben Brunk if (elem == null) { 39191838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException( 39291838ded36131525312739c0929913b215519c2aRuben Brunk String.format("%s[%d] must not be null", valueName, ctr)); 39391838ded36131525312739c0929913b215519c2aRuben Brunk } 39491838ded36131525312739c0929913b215519c2aRuben Brunk ++ctr; 39591838ded36131525312739c0929913b215519c2aRuben Brunk } 39691838ded36131525312739c0929913b215519c2aRuben Brunk 39791838ded36131525312739c0929913b215519c2aRuben Brunk return value; 39891838ded36131525312739c0929913b215519c2aRuben Brunk } 39991838ded36131525312739c0929913b215519c2aRuben Brunk 40091838ded36131525312739c0929913b215519c2aRuben Brunk /** 40191838ded36131525312739c0929913b215519c2aRuben Brunk * Ensures that the {@link Collection} is not {@code null}, and contains at least one element. 40291838ded36131525312739c0929913b215519c2aRuben Brunk * 40391838ded36131525312739c0929913b215519c2aRuben Brunk * @param value a {@link Collection} of boxed elements. 40491838ded36131525312739c0929913b215519c2aRuben Brunk * @param valueName the name of the argument to use if the check fails. 40591838ded36131525312739c0929913b215519c2aRuben Brunk 40691838ded36131525312739c0929913b215519c2aRuben Brunk * @return the validated {@link Collection} 40791838ded36131525312739c0929913b215519c2aRuben Brunk * 40891838ded36131525312739c0929913b215519c2aRuben Brunk * @throws NullPointerException if the {@code value} was {@code null} 40991838ded36131525312739c0929913b215519c2aRuben Brunk * @throws IllegalArgumentException if the {@code value} was empty 41091838ded36131525312739c0929913b215519c2aRuben Brunk */ 41191838ded36131525312739c0929913b215519c2aRuben Brunk public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value, 41291838ded36131525312739c0929913b215519c2aRuben Brunk final String valueName) { 41391838ded36131525312739c0929913b215519c2aRuben Brunk if (value == null) { 41491838ded36131525312739c0929913b215519c2aRuben Brunk throw new NullPointerException(valueName + " must not be null"); 41591838ded36131525312739c0929913b215519c2aRuben Brunk } 41691838ded36131525312739c0929913b215519c2aRuben Brunk if (value.isEmpty()) { 41791838ded36131525312739c0929913b215519c2aRuben Brunk throw new IllegalArgumentException(valueName + " is empty"); 41891838ded36131525312739c0929913b215519c2aRuben Brunk } 41991838ded36131525312739c0929913b215519c2aRuben Brunk return value; 42091838ded36131525312739c0929913b215519c2aRuben Brunk } 42191838ded36131525312739c0929913b215519c2aRuben Brunk 42291838ded36131525312739c0929913b215519c2aRuben Brunk /** 423b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * Ensures that all elements in the argument floating point array are within the inclusive range 424b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 425b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 426b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * will always be out of range.</p> 427b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 428b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param value a floating point array of values 429b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param lower the lower endpoint of the inclusive range 430b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param upper the upper endpoint of the inclusive range 431b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @param valueName the name of the argument to use if the check fails 432b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 433b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @return the validated floating point value 434b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * 435b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws IllegalArgumentException if any of the elements in {@code value} were out of range 436b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin * @throws NullPointerException if the {@code value} was {@code null} 437b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin */ 438b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin public static float[] checkArrayElementsInRange(float[] value, float lower, float upper, 439b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String valueName) { 440b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin checkNotNull(value, valueName + " must not be null"); 441b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 442b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin for (int i = 0; i < value.length; ++i) { 443b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin float v = value[i]; 444b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 445b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin if (Float.isNaN(v)) { 446b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN"); 447b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (v < lower) { 448b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 449b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] is out of range of [%f, %f] (too low)", 450b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin valueName, i, lower, upper)); 451b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } else if (v > upper) { 452b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin throw new IllegalArgumentException( 453b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin String.format("%s[%d] is out of range of [%f, %f] (too high)", 454b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin valueName, i, lower, upper)); 455b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 456b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 457b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin 458b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin return value; 459b3a78b2ca9655396e2d73950221d187b7e5bb3baIgor Murashkin } 460d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey} 461