public final class ArgChecker extends Object
This utility is used throughout the system to validate inputs to methods. Most of the methods return their validated input, allowing patterns like this:
// constructor
public Person(String name, int age) {
this.name = ArgChecker.notBlank(name, "name");
this.age = ArgChecker.notNegative(age, "age");
}
| Modifier and Type | Method and Description |
|---|---|
static <T> void |
inOrderNotEqual(Comparable<? super T> obj1,
T obj2,
String name1,
String name2)
Checks that the two values are in order and not equal.
|
static <T> void |
inOrderOrEqual(Comparable<? super T> obj1,
T obj2,
String name1,
String name2)
Checks that the two values are in order or equal.
|
static double |
inRange(double argument,
double lowInclusive,
double highExclusive,
String name)
Checks that the argument is within the range defined by
low <= x < high. |
static int |
inRange(int argument,
int lowInclusive,
int highExclusive,
String name)
Checks that the argument is within the range defined by
low <= x < high. |
static double |
inRangeExclusive(double argument,
double lowExclusive,
double highExclusive,
String name)
Checks that the argument is within the range defined by
low < x < high. |
static int |
inRangeExclusive(int argument,
int lowExclusive,
int highExclusive,
String name)
Checks that the argument is within the range defined by
low < x < high. |
static double |
inRangeInclusive(double argument,
double lowInclusive,
double highInclusive,
String name)
Checks that the argument is within the range defined by
low <= x <= high. |
static int |
inRangeInclusive(int argument,
int lowInclusive,
int highInclusive,
String name)
Checks that the argument is within the range defined by
low <= x <= high. |
static void |
isFalse(boolean validIfFalse,
String message)
Checks that the specified boolean is false.
|
static void |
isFalse(boolean validIfFalse,
String message,
Object... arg)
Checks that the specified boolean is false.
|
static void |
isTrue(boolean validIfTrue)
Checks that the specified boolean is true.
|
static void |
isTrue(boolean validIfTrue,
String message)
Checks that the specified boolean is true.
|
static void |
isTrue(boolean validIfTrue,
String message,
double arg)
Checks that the specified boolean is true.
|
static void |
isTrue(boolean validIfTrue,
String message,
long arg)
Checks that the specified boolean is true.
|
static void |
isTrue(boolean validIfTrue,
String message,
Object... arg)
Checks that the specified boolean is true.
|
static String |
matches(CharMatcher matcher,
int minLength,
int maxLength,
String argument,
String name,
String equivalentRegex)
Checks that the specified argument is non-null and only contains the specified characters.
|
static String |
matches(Pattern pattern,
String argument,
String name)
Checks that the specified argument is non-null and matches the specified pattern.
|
static double[] |
noDuplicates(double[] argument,
String name)
Checks that the specified argument array is non-null and does not contain any duplicate values.
|
static double[] |
noDuplicatesSorted(double[] argument,
String name)
Checks that the specified argument array is non-null, sorted, and does not contain any duplicate values.
|
static <T,I extends Iterable<T>> |
noNulls(I argument,
String name)
Checks that the specified argument collection is non-null and contains no nulls.
|
static <K,V,M extends Map<K,V>> |
noNulls(M argument,
String name)
Checks that the specified argument map is non-null and contains no nulls.
|
static <T> T[] |
noNulls(T[] argument,
String name)
Checks that the specified argument array is non-null and contains no nulls.
|
static String |
notBlank(String argument,
String name)
Checks that the specified argument is non-null and not blank.
|
static <T,C extends Collection<T>> |
notEmpty(C argument,
String name)
Checks that the specified argument collection is non-null and not empty.
|
static double[] |
notEmpty(double[] argument,
String name)
Checks that the specified argument array is non-null and not empty.
|
static int[] |
notEmpty(int[] argument,
String name)
Checks that the specified argument array is non-null and not empty.
|
static <T,I extends Iterable<T>> |
notEmpty(I argument,
String name)
Checks that the specified argument iterable is non-null and not empty.
|
static long[] |
notEmpty(long[] argument,
String name)
Checks that the specified argument array is non-null and not empty.
|
static <K,V,M extends Map<K,V>> |
notEmpty(M argument,
String name)
Checks that the specified argument map is non-null and not empty.
|
static String |
notEmpty(String argument,
String name)
Checks that the specified argument is non-null and not empty.
|
static <T> T[] |
notEmpty(T[] argument,
String name)
Checks that the specified argument array is non-null and not empty.
|
static double |
notNaN(double argument,
String name)
Checks that the argument is a number and not NaN.
|
static double |
notNegative(double argument,
String name)
Checks that the argument is not negative.
|
static int |
notNegative(int argument,
String name)
Checks that the argument is not negative.
|
static long |
notNegative(long argument,
String name)
Checks that the argument is not negative.
|
static double |
notNegativeOrZero(double argument,
double tolerance,
String name)
Checks that the argument is greater than zero to within a given accuracy.
|
static double |
notNegativeOrZero(double argument,
String name)
Checks that the argument is not negative or zero.
|
static int |
notNegativeOrZero(int argument,
String name)
Checks that the argument is not negative or zero.
|
static long |
notNegativeOrZero(long argument,
String name)
Checks that the argument is not negative or zero.
|
static <T> T |
notNull(T argument,
String name)
Checks that the specified argument is non-null.
|
static <T> T |
notNullItem(T argument)
Checks that the specified item is non-null.
|
static double |
notZero(double argument,
double tolerance,
String name)
Checks that the argument is not equal to zero to within a given accuracy.
|
static double |
notZero(double argument,
String name)
Checks that the argument is not equal to zero.
|
public static void isTrue(boolean validIfTrue)
Given the input argument, this returns normally only if it is true. This will typically be the result of a caller-specific check. For example:
ArgChecker.isTrue(collection.contains("value"));
It is strongly recommended to pass an additional message argument using
isTrue(boolean, String).
validIfTrue - a boolean resulting from testing an argumentIllegalArgumentException - if the test value is falsepublic static void isTrue(boolean validIfTrue,
String message)
Given the input argument, this returns normally only if it is true. This will typically be the result of a caller-specific check. For example:
ArgChecker.isTrue(collection.contains("value"), "Collection must contain 'value'");
validIfTrue - a boolean resulting from testing an argumentmessage - the error message, not nullIllegalArgumentException - if the test value is falsepublic static void isTrue(boolean validIfTrue,
String message,
Object... arg)
Given the input argument, this returns normally only if it is true. This will typically be the result of a caller-specific check. For example:
ArgChecker.isTrue(collection.contains("value"), "Collection must contain 'value': {}", collection);
This returns void, and not the value being checked, as there is
never a good reason to validate a boolean argument value.
The message is produced using a template that contains zero to many "{}" placeholders.
Each placeholder is replaced by the next available argument.
If there are too few arguments, then the message will be left with placeholders.
If there are too many arguments, then the excess arguments are appended to the
end of the message. No attempt is made to format the arguments.
See Messages.format(String, Object...) for more details.
validIfTrue - a boolean resulting from testing an argumentmessage - the error message with {} placeholders, not nullarg - the message argumentsIllegalArgumentException - if the test value is falsepublic static void isTrue(boolean validIfTrue,
String message,
long arg)
Given the input argument, this returns normally only if it is true. This will typically be the result of a caller-specific check. For example:
ArgChecker.isTrue(value > check, "Value must be greater than check: {}", value);
This returns void, and not the value being checked, as there is
never a good reason to validate a boolean argument value.
The message is produced using a template that contains zero or one "{}" placeholders. The placeholder, if present, is replaced by the argument. If there is no placeholder, the argument is appended to the end of the message.
validIfTrue - a boolean resulting from testing an argumentmessage - the error message with {} placeholders, not nullarg - the message argumentIllegalArgumentException - if the test value is falsepublic static void isTrue(boolean validIfTrue,
String message,
double arg)
Given the input argument, this returns normally only if it is true. This will typically be the result of a caller-specific check. For example:
ArgChecker.isTrue(value > check, "Value must be greater than check: {}", value);
This returns void, and not the value being checked, as there is
never a good reason to validate a boolean argument value.
The message is produced using a template that contains zero or one "{}" placeholders. The placeholder, if present, is replaced by the argument. If there is no placeholder, the argument is appended to the end of the message.
validIfTrue - a boolean resulting from testing an argumentmessage - the error message with {} placeholders, not nullarg - the message argumentIllegalArgumentException - if the test value is falsepublic static void isFalse(boolean validIfFalse,
String message)
Given the input argument, this returns normally only if it is false. This will typically be the result of a caller-specific check. For example:
ArgChecker.isFalse(collection.contains("value"), "Collection must not contain 'value'");
This returns void, and not the value being checked, as there is
never a good reason to validate a boolean argument value.
validIfFalse - a boolean resulting from testing an argumentmessage - the error message, not nullIllegalArgumentException - if the test value is truepublic static void isFalse(boolean validIfFalse,
String message,
Object... arg)
Given the input argument, this returns normally only if it is false. This will typically be the result of a caller-specific check. For example:
ArgChecker.isFalse(collection.contains("value"), "Collection must not contain 'value': {}", collection);
This returns void, and not the value being checked, as there is
never a good reason to validate a boolean argument value.
The message is produced using a template that contains zero to many "{}" placeholders.
Each placeholder is replaced by the next available argument.
If there are too few arguments, then the message will be left with placeholders.
If there are too many arguments, then the excess arguments are appended to the
end of the message. No attempt is made to format the arguments.
See Messages.format(String, Object...) for more details.
validIfFalse - a boolean resulting from testing an argumentmessage - the error message with {} placeholders, not nullarg - the message arguments, not nullIllegalArgumentException - if the test value is truepublic static <T> T notNull(T argument,
String name)
Given the input argument, this returns only if it is non-null. For example, in a constructor:
this.name = ArgChecker.notNull(name, "name");
T - the type of the input argument reflected in the resultargument - the argument to check, null throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is nullpublic static <T> T notNullItem(T argument)
Given the input argument, this returns only if it is non-null. One use for this method is in a stream:
ArgChecker.notNull(coll, "coll")
coll.stream()
.map(ArgChecker::notNullItem)
...
T - the type of the input argument reflected in the resultargument - the argument to check, null throws an exceptionargument, not nullIllegalArgumentException - if the input is nullpublic static String matches(Pattern pattern, String argument, String name)
Given the input argument, this returns only if it is non-null and matches the regular expression pattern specified. For example, in a constructor:
this.name = ArgChecker.matches(REGEX_NAME, name, "name");
pattern - the pattern to check against, not nullargument - the argument to check, null throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static String matches(CharMatcher matcher, int minLength, int maxLength, String argument, String name, String equivalentRegex)
Given the input argument, this returns only if it is non-null and matches
the CharMatcher specified.
For example, in a constructor:
this.name = ArgChecker.matches(REGEX_NAME, 1, Integer.MAX_VALUE, name, "name", "[A-Z]+");
matcher - the matcher to check against, not nullminLength - the minimum length to allowmaxLength - the minimum length to allowargument - the argument to check, null throws an exceptionname - the name of the argument to use in the error message, not nullequivalentRegex - the equivalent regular expression patternargument, not nullIllegalArgumentException - if the input is null or emptypublic static String notBlank(String argument, String name)
Given the input argument, this returns the input only if it is non-null
and contains at least one non whitespace character.
This is often linked with a call to trim().
For example, in a constructor:
this.name = ArgChecker.notBlank(name, "name").trim();
The argument is trimmed using String.trim() to determine if it is empty.
The result is the original argument, not the trimmed one.
argument - the argument to check, null or blank throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or blankpublic static String notEmpty(String argument, String name)
Given the input argument, this returns only if it is non-null and contains
at least one character, which may be a whitespace character.
See also notBlank(String, String).
For example, in a constructor:
this.name = ArgChecker.notEmpty(name, "name");
argument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static <T> T[] notEmpty(T[] argument,
String name)
Given the input argument, this returns only if it is non-null and contains at least one element. The element is not validated and may be null. For example, in a constructor:
this.names = ArgChecker.notEmpty(names, "names");
T - the type of the input array reflected in the resultargument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static int[] notEmpty(int[] argument,
String name)
Given the input argument, this returns only if it is non-null and contains at least one element. For example, in a constructor:
this.values = ArgChecker.notEmpty(values, "values");
argument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static long[] notEmpty(long[] argument,
String name)
Given the input argument, this returns only if it is non-null and contains at least one element. For example, in a constructor:
this.values = ArgChecker.notEmpty(values, "values");
argument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static double[] notEmpty(double[] argument,
String name)
Given the input argument, this returns only if it is non-null and contains at least one element. For example, in a constructor:
this.values = ArgChecker.notEmpty(values, "values");
argument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static <T,I extends Iterable<T>> I notEmpty(I argument, String name)
Given the input argument, this returns only if it is non-null and contains at least one element. The element is not validated and may be null. For example, in a constructor:
this.values = ArgChecker.notEmpty(values, "values");
T - the element type of the input iterable reflected in the resultI - the type of the input iterable, reflected in the resultargument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static <T,C extends Collection<T>> C notEmpty(C argument, String name)
Given the input argument, this returns only if it is non-null and contains at least one element. The element is not validated and may contain nulls if the collection allows nulls. For example, in a constructor:
this.values = ArgChecker.notEmpty(values, "values");
T - the element type of the input collection reflected in the resultC - the type of the input collection, reflected in the resultargument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static <K,V,M extends Map<K,V>> M notEmpty(M argument, String name)
Given the input argument, this returns only if it is non-null and contains at least one mapping. The element is not validated and may contain nulls if the collection allows nulls. For example, in a constructor:
this.keyValues = ArgChecker.notEmpty(keyValues, "keyValues");
K - the key type of the input map key, reflected in the resultV - the value type of the input map value, reflected in the resultM - the type of the input map, reflected in the resultargument - the argument to check, null or empty throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or emptypublic static <T> T[] noNulls(T[] argument,
String name)
Given the input argument, this returns only if it is non-null and contains no nulls. For example, in a constructor:
this.values = ArgChecker.noNulls(values, "values");
T - the type of the input array reflected in the resultargument - the argument to check, null or contains null throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or contains nullspublic static <T,I extends Iterable<T>> I noNulls(I argument, String name)
Given the input argument, this returns only if it is non-null and contains no nulls. For example, in a constructor:
this.values = ArgChecker.noNulls(values, "values");
T - the element type of the input iterable reflected in the resultI - the type of the input iterable, reflected in the resultargument - the argument to check, null or contains null throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or contains nullspublic static <K,V,M extends Map<K,V>> M noNulls(M argument, String name)
Given the input argument, this returns only if it is non-null and contains no nulls. For example, in a constructor:
this.keyValues = ArgChecker.noNulls(keyValues, "keyValues");
K - the key type of the input map key, reflected in the resultV - the value type of the input map value, reflected in the resultM - the type of the input map, reflected in the resultargument - the argument to check, null or contains null throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or contains nullspublic static double[] noDuplicates(double[] argument,
String name)
Given the input argument, this returns only if it is non-null and does not contain duplicate values. For example, in a constructor:
this.values = ArgChecker.noDuplicates(values, "values");
If you know the argument is sorted increasing then noDuplicatesSorted(double[], String) might be more
performant.
argument - the argument to check, null or duplicate values throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null or contains duplicate valuespublic static double[] noDuplicatesSorted(double[] argument,
String name)
Given the input argument, this returns only if it is non-null, sorted, and does not contain duplicate values. For example, in a constructor:
this.values = ArgChecker.noDuplicatesSorted(values, "values");
argument - the argument to check, null, out of order or duplicate values throws an exceptionname - the name of the argument to use in the error message, not nullargument, not nullIllegalArgumentException - if the input is null, unsorted, or contains duplicate valuespublic static int notNegative(int argument,
String name)
Given the input argument, this returns only if it is zero or greater. For example, in a constructor:
this.amount = ArgChecker.notNegative(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negativepublic static long notNegative(long argument,
String name)
Given the input argument, this returns only if it is zero or greater. For example, in a constructor:
this.amount = ArgChecker.notNegative(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negativepublic static double notNegative(double argument,
String name)
Given the input argument, this returns only if it is zero or greater. For example, in a constructor:
this.amount = ArgChecker.notNegative(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negativepublic static double notNaN(double argument,
String name)
Given the input argument, this returns only if it is an actual number. For example, in a constructor:
this.amount = ArgChecker.notNaN(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is NaNpublic static int notNegativeOrZero(int argument,
String name)
Given the input argument, this returns only if it is greater than zero. For example, in a constructor:
this.amount = ArgChecker.notNegativeOrZero(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negative or zeropublic static long notNegativeOrZero(long argument,
String name)
Given the input argument, this returns only if it is greater than zero. For example, in a constructor:
this.amount = ArgChecker.notNegativeOrZero(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negative or zeropublic static double notNegativeOrZero(double argument,
String name)
Given the input argument, this returns only if it is greater than zero. For example, in a constructor:
this.amount = ArgChecker.notNegativeOrZero(amount, "amount");
argument - the argument to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the input is negative or zeropublic static double notNegativeOrZero(double argument,
double tolerance,
String name)
Given the input argument, this returns only if it is greater than zero
using the eps accuracy for zero.
For example, in a constructor:
this.amount = ArgChecker.notNegativeOrZero(amount, 0.0001d, "amount");
argument - the value to checktolerance - the tolerance to use for zeroname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the absolute value of the argument is less than epspublic static double notZero(double argument,
String name)
Given the input argument, this returns only if it is not zero. Both positive and negative zero are checked. For example, in a constructor:
this.amount = ArgChecker.notZero(amount, "amount");
argument - the value to checkname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is zeropublic static double notZero(double argument,
double tolerance,
String name)
Given the input argument, this returns only if it is not zero comparing
using the eps accuracy.
For example, in a constructor:
this.amount = ArgChecker.notZero(amount, 0.0001d, "amount");
argument - the value to checktolerance - the tolerance to use for zeroname - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the absolute value of the argument is less than the tolerancepublic static double inRange(double argument,
double lowInclusive,
double highExclusive,
String name)
low <= x < high.
Given a value, this returns true if it is within the specified range including the lower boundary but excluding the upper boundary. For example, in a constructor:
this.amount = ArgChecker.inRange(amount, 0d, 1d, "amount");
argument - the value to checklowInclusive - the low value of the rangehighExclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static double inRangeInclusive(double argument,
double lowInclusive,
double highInclusive,
String name)
low <= x <= high.
Given a value, this returns true if it is within the specified range including both boundaries. For example, in a constructor:
this.amount = ArgChecker.inRangeInclusive(amount, 0d, 1d, "amount");
argument - the value to checklowInclusive - the low value of the rangehighInclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static double inRangeExclusive(double argument,
double lowExclusive,
double highExclusive,
String name)
low < x < high.
Given a value, this returns true if it is within the specified range excluding both boundaries. For example, in a constructor:
this.amount = ArgChecker.inRangeExclusive(amount, 0d, 1d, "amount");
argument - the value to checklowExclusive - the low value of the rangehighExclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static int inRange(int argument,
int lowInclusive,
int highExclusive,
String name)
low <= x < high.
Given a value, this returns true if it is within the specified range including the lower boundary but excluding the upper boundary. For example, in a constructor:
this.amount = ArgChecker.inRange(amount, 0d, 1d, "amount");
argument - the value to checklowInclusive - the low value of the rangehighExclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static int inRangeInclusive(int argument,
int lowInclusive,
int highInclusive,
String name)
low <= x <= high.
Given a value, this returns true if it is within the specified range including both boundaries. For example, in a constructor:
this.amount = ArgChecker.inRangeInclusive(amount, 0d, 1d, "amount");
argument - the value to checklowInclusive - the low value of the rangehighInclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static int inRangeExclusive(int argument,
int lowExclusive,
int highExclusive,
String name)
low < x < high.
Given a value, this returns true if it is within the specified range excluding both boundaries. For example, in a constructor:
this.amount = ArgChecker.inRangeExclusive(amount, 0d, 1d, "amount");
argument - the value to checklowExclusive - the low value of the rangehighExclusive - the high value of the rangename - the name of the argument to use in the error message, not nullargumentIllegalArgumentException - if the argument is outside the valid rangepublic static <T> void inOrderNotEqual(Comparable<? super T> obj1, T obj2, String name1, String name2)
Given two comparable instances, this checks that the first is "less than" the second. Two equal values also throw the exception.
T - the typeobj1 - the first object, null throws an exceptionobj2 - the second object, null throws an exceptionname1 - the first argument name, not nullname2 - the second argument name, not nullIllegalArgumentException - if either input is null or they are not in orderpublic static <T> void inOrderOrEqual(Comparable<? super T> obj1, T obj2, String name1, String name2)
Given two comparable instances, this checks that the first is "less than" or "equal to" the second.
T - the typeobj1 - the first object, null throws an exceptionobj2 - the second object, null throws an exceptionname1 - the first argument name, not nullname2 - the second argument name, not nullIllegalArgumentException - if either input is null or they are not in orderCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.