public final class Ints extends Object
int values.| Modifier and Type | Method and Description |
|---|---|
static boolean |
assertIfEnabled(IntPredicate requirement,
int value)
Asserts that the provided
value satisfies the provided requirement if
assertions is enabled (i.e. |
static String |
failDescription(IntPredicate requirement,
int value)
Returns a human-readable form of a failure message provided that the provided
value did not
satisfy the provided requirement. |
static IntPredicate |
nonNegative()
Returns a predicate that can test if a value is non-negative (i.e.
|
static int |
requireNonNegative(int value)
Returns the provided
value after checking that it is non-negative throwing
an IllegalArgumentException if the check fails. |
public static int requireNonNegative(int value)
value after checking that it is non-negative throwing
an IllegalArgumentException if the check fails.
This method is designed primarily for doing parameter validation in public methods and constructors, as demonstrated below:
public Foo(int bar) {
this.bar = requireNonNegative(bar);
}
This method is functionally equivalent to:
require(negative().negate(), value);
but is potentially optimized for performance.value - the value to checkvalue if the check passesIllegalArgumentException - if the check failspublic static boolean assertIfEnabled(IntPredicate requirement, int value)
value satisfies the provided requirement if
assertions is enabled (i.e. -ea) and AssertUtil.SKIP_ASSERTIONS is false.
This method is designed primarily for doing parameter validation in private methods and constructors, as demonstrated below:
private Foo(int bar) {
assertIfEnabled(nonNegative, bar);
this.bar = bar;
}
requirement - to impose on the provided valuevalue - the value to checktrueNullPointerException - if the provided requirement is null. There is no guarantee that this
exception is thrown. For example, if assertions are not enabled, then the exception
might not be thrown.AssertionError - if the check fails and assertions are enabled both via the -ea JVM command
line option and by setting AssertUtil.SKIP_ASSERTIONS to false.public static String failDescription(IntPredicate requirement, int value)
value did not
satisfy the provided requirement.requirement - to imposed on the provided valuesvalue - the value to checkvalue did not
satisfy the provided requirementNullPointerException - if the provided requirement is null.public static IntPredicate nonNegative()
This is equivalent to: negative().negate()
Copyright © 2024. All rights reserved.