public final class Validate extends Object
| Modifier | Constructor and Description |
|---|---|
private |
Validate()
Hidden for pure static class.
|
| Modifier and Type | Method and Description |
|---|---|
private static String |
badElementIndex(int index,
int size,
String desc)
TODO: Review this method.
|
private static String |
badPositionIndex(int index,
int size,
String desc)
TODO Write comment
|
private static String |
badPositionIndexes(int start,
int end,
int size)
TODO Write comment
|
static int |
checkElementIndex(int index,
int size)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static int |
checkElementIndex(int index,
int size,
String desc)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static void |
checkPositionIndexes(int start,
int end,
int size)
Ensures that
start and end specify a valid positions
in an array, list or string of size size, and are in order. |
(package private) static String |
format(String template,
Object... args)
Substitutes each
%s in template with an argument. |
static int |
greaterThan(int reference,
int lowerBound)
Convenience method for
greaterThan(int, int, java.lang.String) with null as name. |
static int |
greaterThan(int reference,
int lowerBound,
String name)
Tests that a given integer reference value is greater than the given lower bound.
|
static long |
greaterThan(long reference,
long lowerBound)
Convenience method for
greaterThan(long, long, java.lang.String) with null as name. |
static long |
greaterThan(long reference,
long lowerBound,
String name)
Tests that a given integer reference value is greater than the given lower bound.
|
static int |
greaterThanOrEqual(int reference,
int lowerBound)
Convenience method for
greaterThanOrEqual(int, int, java.lang.String) with null as name. |
static int |
greaterThanOrEqual(int reference,
int lowerBound,
String name)
Tests that a given integer reference value is greater than or equal the given lower bound.
|
static long |
greaterThanOrEqual(long reference,
long lowerBound)
Convenience method for
greaterThanOrEqual(long, long, java.lang.String) with null as name. |
static long |
greaterThanOrEqual(long reference,
long lowerBound,
String name)
Tests that a given integer reference value is greater than or equal the given lower bound.
|
static void |
isTrue(boolean expression,
String message)
Tests that a given expression is true.
|
static String |
notEmpty(String reference)
Convenience message for
notEmpty(java.lang.String, java.lang.String) with null as second
parameter. |
static String |
notEmpty(String reference,
String name)
Validates that a given string reference is not
null or empty. |
static <T> T |
notNull(T reference)
Convenience message for
notNull(java.lang.Object, java.lang.String) with null as second
parameter. |
static <T> T |
notNull(T reference,
String name)
Validates that a given reference is not
null. |
public static <T> T notNull(T reference,
String name)
null.
This example validates that input is not null. If it is null a NullPointerException will
be thrown with the name as hint in the exception message.:
void someMethod(final Object input) {
final Object validInput = Validate.notNull(input, "input");
// ...
}
The second parameter name may be omitted. Then a NullPointerException without any message will be
thrown. For this purpose you can call the convenience method notNull(java.lang.Object).
T - type of referencereference - validated referencename - name of validated reference, may be nullpublic static <T> T notNull(T reference)
notNull(java.lang.Object, java.lang.String) with null as second
parameter.T - type of referencereference - validated referencepublic static String notEmpty(String reference, String name)
null or empty.
This example validates that input is not null. If it is null a NullPointerException will
be thrown with the name as hint in the exception message. Also it validates that input is not
empty by calling String.isEmpty(). If the string is empty an IllegalArgumentException will be
thrown:
void someMethod(final String input) {
final Strign validInput = Validate.notEmpty(input, "input");
// ...
}
The second parameter name may be omitted. Then a NullPointerException or
IllegalArgumentException without any message will be thrown. For this purpose you can call the
convenience method notEmpty(java.lang.String).
reference - validated referencename - name of validated reference, may be nullpublic static String notEmpty(String reference)
notEmpty(java.lang.String, java.lang.String) with null as second
parameter.reference - validated referencepublic static int greaterThan(int reference,
int lowerBound,
String name)
The method throws an IllegalArgumentException if the given reference is less than or equal the given
lower bound value.
reference - validated referencelowerBound - bound to test againstname - name of validated reference, may be nullpublic static int greaterThan(int reference,
int lowerBound)
greaterThan(int, int, java.lang.String) with null as name.reference - validated referencelowerBound - bound to test againstpublic static int greaterThanOrEqual(int reference,
int lowerBound,
String name)
The method throws an IllegalArgumentException if the given reference is less than the given lower bound
value.
reference - validated referencelowerBound - bound to test againstname - name of validated reference, may be nullpublic static int greaterThanOrEqual(int reference,
int lowerBound)
greaterThanOrEqual(int, int, java.lang.String) with null as name.reference - validated referencelowerBound - bound to test againstpublic static long greaterThan(long reference,
long lowerBound,
String name)
The method throws an IllegalArgumentException if the given reference is less than or equal the given
lower bound value.
reference - validated referencelowerBound - bound to test againstname - name of validated reference, may be nullpublic static long greaterThan(long reference,
long lowerBound)
greaterThan(long, long, java.lang.String) with null as name.reference - validated referencelowerBound - bound to test againstpublic static long greaterThanOrEqual(long reference,
long lowerBound,
String name)
The method throws an IllegalArgumentException if the given reference is less than the given lower bound
value.
reference - validated referencelowerBound - bound to test againstname - name of validated reference, may be nullpublic static long greaterThanOrEqual(long reference,
long lowerBound)
greaterThanOrEqual(long, long, java.lang.String) with null as name.reference - validated referencelowerBound - bound to test againstpublic static void isTrue(boolean expression,
String message)
Throws an IllegalArgumentException if the expression is false.
expression - tested expressionmessage - must not be null or emptypublic static int checkElementIndex(int index,
int size)
index specifies a valid element in an array, list or string of size size. An
element index may range from zero, inclusive, to size, exclusive.
TODO: Review this method.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringindexIndexOutOfBoundsException - if index is negative or is not less than sizeIllegalArgumentException - if size is negativepublic static int checkElementIndex(int index,
int size,
String desc)
index specifies a valid element in an array, list or string of size size. An
element index may range from zero, inclusive, to size, exclusive.
TODO: Review this method.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringdesc - the text to use to describe this index in an error messageindexIndexOutOfBoundsException - if index is negative or is not less than sizeIllegalArgumentException - if size is negativeprivate static String badElementIndex(int index, int size, String desc)
index - TODO Write commentsize - TODO Write commentdesc - TODO Write commentstatic String format(String template, Object... args)
%s in template with an argument. These are matched by position - the first
%s gets args[0], etc. If there are more arguments than placeholders, the unmatched arguments will
be appended to the end of the formatted message in square braces.
TODO: Review this method.template - a non-null string containing 0 or more %s placeholders.args - the arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object). Arguments can be null.public static void checkPositionIndexes(int start,
int end,
int size)
start and end specify a valid positions
in an array, list or string of size size, and are in order. A position index may range from zero to
size, inclusive.
TODO: Review this method.start - a user-supplied index identifying a starting position in an array, list or stringend - a user-supplied index identifying a ending position in an array, list or stringsize - the size of that array, list or stringIndexOutOfBoundsException - if either index is negative or is greater than size, or if end
is less than startIllegalArgumentException - if size is negativeprivate static String badPositionIndexes(int start, int end, int size)
start - TODO Write commentend - TODO Write commentsize - TODO Write commentCopyright © 2014 Sven Strittmatter. All Rights Reserved.