java.lang.Object
me.sbasalaev.Require
Precondition checks.
This class contains convenience static methods that help to check whether
the preconditions of a method or constructor are met. In contrast to
Assert these should only be used when the exceptional situation is
the caller's fault. The method names are chosen so that
Require class itself is imported rather than individual methods.
For instance, the start of the method repeating the string given number of
times could be
String repeat(String string, int times) {
Require.nonNull(string, "string");
Require.nonNegative(times, "times");
...
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidEnsuresconditionistrue.static voidEnsuresconditionistrue.static intnonNegative(int value, String name) Checks that thevalueis non-negative and returns it.static longnonNegative(long value, String name) Checks that thevalueis non-negative and returns it.static <T> TChecks that thevalueis notnull.static <T> T[]Checks that thearrayis notnulland contains nonullelements.static <T> T[]Checks that thearrayis notnulland contains nonullelements.static intChecks that thevalueis positive and returns it.static longChecks that thevalueis positive and returns it.static voidEnsuresconditionistrue.static voidEnsuresconditionistrue.
-
Method Details
-
nonNull
Checks that thevalueis notnull.- Type Parameters:
T- the type of the value.- Parameters:
value- the value to be checked.name- the name of the parameter to report when there isnull.- Returns:
- the value if it is not
null. - Throws:
NullPointerException- if thevalueisnull.- Since:
- 4.1
- See Also:
-
noNulls
Checks that thearrayis notnulland contains nonullelements.- Type Parameters:
T- the type of the elements of the array.- Parameters:
array- the array to check fornullreferences.name- the name of the parameter to report when there isnull.- Returns:
- the array if it is not
nulland contains nonullelements. - Throws:
NullPointerException- if eitherarrayor any of its elements isnull.- Since:
- 4.1
-
noNulls
Checks that thearrayis notnulland contains nonullelements.- Type Parameters:
T- the type of the elements of the array.- Parameters:
array- the array to check fornullreferences.- Returns:
- the array if it is not
nulland contains nonullelements. - Throws:
NullPointerException- if eitherarrayor any of its elements isnull.
-
argument
Ensuresconditionistrue.- Parameters:
condition- a boolean expression to be checked.message- the message to be constructed and reported whenconditionis false.- Throws:
IllegalArgumentException- ifconditionisfalse.
-
argument
Ensuresconditionistrue.- Parameters:
condition- a boolean expression to be checked.message- the message to be reported whenconditionis false.- Throws:
IllegalArgumentException- ifconditionisfalse.
-
state
Ensuresconditionistrue.- Parameters:
condition- a boolean expression to be checked.message- the message to be constructed and reported whenconditionis false.- Throws:
IllegalStateException- ifconditionisfalse.
-
state
Ensuresconditionistrue.- Parameters:
condition- a boolean expression to be checked.message- the message to be reported whenconditionis false.- Throws:
IllegalStateException- ifconditionisfalse.
-
nonNegative
Checks that thevalueis non-negative and returns it.- Parameters:
value- the value to be checked.name- the name of the parameter to report when the value is negative.- Returns:
- the value if it is non-negative.
- Throws:
IllegalArgumentException- if thevalueis negative.
-
nonNegative
Checks that thevalueis non-negative and returns it.- Parameters:
value- the value to be checked.name- the name of the parameter to report when the value is negative.- Returns:
- the value if it is non-negative.
- Throws:
IllegalArgumentException- if thevalueis negative.- Since:
- 4.1
-
positive
Checks that thevalueis positive and returns it.- Parameters:
value- the value to be checked.name- the name of the parameter to report when the value is not positive.- Returns:
- the value if it is positive.
- Throws:
IllegalArgumentException- if thevalueis zero or negative.- Since:
- 4.1
-
positive
Checks that thevalueis positive and returns it.- Parameters:
value- the value to be checked.name- the name of the parameter to report when the value is not positive.- Returns:
- the value if it is positive.
- Throws:
IllegalArgumentException- if thevalueis zero or negative.- Since:
- 4.1
-