public final class Preconditions extends Object
Simple static methods to be called at the start of your own methods to verify correct arguments and state. This allows constructs such as
if (null == reference) { throw new IllegalArgumentException("reference cannot be null"); }
to be replaced with the more compact
checkNotNull(reference, "reference");
Note that the sense of the expression is inverted; with Preconditions
you declare what you expect to be
true, just as you do with an assert
or a JUnit assertTrue
call.
Take care not to confuse precondition checking with other similar types of checks! Precondition exceptions --
including those provided here, but also IndexOutOfBoundsException
, NoSuchElementException
,
UnsupportedOperationException
and others -- are used to signal that the calling method has made an
error. This tells the caller that it should not have invoked the method when it did, with the arguments it did, or
perhaps ever. Postcondition or other invariant failures should not throw these types of exceptions.
See the Guava User Guide on using
Preconditions
.
Preconditions
Modifier and Type | Method and Description |
---|---|
static byte[] |
checkLength(byte[] reference,
String referenceName,
int minLength,
int maxLength)
Ensures that an array passed as a parameter to the calling method is not shorter than a given minimum length and
not greater than a maximum length.
|
static CharSequence |
checkLength(CharSequence reference,
String referenceName,
int minLength,
int maxLength)
Ensures that a
CharSequence passed as a parameter to the calling method is not shorter than a given
minimum length and not greater than a maximum length. |
static <K,V,C extends Map<K,V>> |
checkLength(C reference,
String referenceName,
int minLength,
int maxLength)
Ensures that a
Map passed as a parameter to the calling method is not shorter than a given minimum length
and not greater than a maximum length. |
static <T,C extends Collection<T>> |
checkLength(C reference,
String referenceName,
int minLength,
int maxLength)
Ensures that a
Collection passed as a parameter to the calling method is not shorter than a given minimum
length and not greater than a maximum length. |
static <T> T[] |
checkLength(T[] reference,
String referenceName,
int minLength,
int maxLength)
Ensures that an array passed as a parameter to the calling method is not shorter than a given minimum length and
not greater than a maximum length.
|
static char[] |
checkNotBlank(char[] reference,
String referenceName)
Ensures that an array reference passed as a parameter to the calling method is neither null, empty nor blank.
|
static String |
checkNotBlank(String reference,
String referenceName)
Ensures that a
String passed as a parameter to the calling method is not blank. |
static String |
checkNotBlankState(String reference,
String referenceName)
Ensures that a
String (involved in the state of the calling instance) is not blank. |
static byte[] |
checkNotEmpty(byte[] reference,
String referenceName)
Ensures that an array reference passed as a parameter to the calling method is neither null nor empty.
|
static char[] |
checkNotEmpty(char[] reference,
String referenceName)
Ensures that an array reference passed as a parameter to the calling method is neither null nor empty.
|
static <E> Collection<E> |
checkNotEmpty(Collection<E> reference,
String referenceName)
Ensures that a Collection reference passed as a parameter to the calling method is neither null nor empty.
|
static int[] |
checkNotEmpty(int[] reference,
String referenceName)
Ensures that an array reference passed as a parameter to the calling method is neither null nor empty.
|
static <K,V> Map<K,V> |
checkNotEmpty(Map<K,V> reference,
String referenceName)
Ensures that a Map reference passed as a parameter to the calling method is neither null nor empty.
|
static <T> T[] |
checkNotEmpty(T[] reference,
String referenceName)
Ensures that an array reference passed as a parameter to the calling method is neither null nor empty.
|
static byte[] |
checkNotLonger(byte[] reference,
String referenceName,
int maxLength)
Ensures that an array passed as a parameter to the calling method is not longer than a given maximum length.
|
static CharSequence |
checkNotLonger(CharSequence reference,
String referenceName,
int maxLength)
Ensures that a
CharSequence passed as a parameter to the calling method is not longer than a given
maximum length. |
static <K,V,C extends Map<K,V>> |
checkNotLonger(C reference,
String referenceName,
int maxLength)
Ensures that a
Map passed as a parameter to the calling method is not longer than a given maximum
length. |
static <T,C extends Collection<T>> |
checkNotLonger(C reference,
String referenceName,
int maxLength)
Ensures that a
Collection passed as a parameter to the calling method is not longer than a given maximum
length. |
static <T> T[] |
checkNotLonger(T[] reference,
String referenceName,
int maxLength)
Ensures that an array passed as a parameter to the calling method is not longer than a given maximum length.
|
static int |
checkNotNegative(int value,
String referenceName)
Ensures that a given value is non negative (ie >= 0).
|
static <T> T |
checkNotNull(T reference,
String referenceName)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static <T> T |
checkNotNullState(T reference,
String referenceName)
Ensures that an object reference (involved in the state of the calling instance) is not null.
|
static byte[] |
checkNotShorter(byte[] reference,
String referenceName,
int minLength)
Ensures that an array passed as a parameter to the calling method is not shorter than a given minimum
length.
|
static CharSequence |
checkNotShorter(CharSequence reference,
String referenceName,
int minLength)
Ensures that a
CharSequence passed as a parameter to the calling method is not shorter than a given
minimum length. |
static <K,V,C extends Map<K,V>> |
checkNotShorter(C reference,
String referenceName,
int minLength)
Ensures that a
Map passed as a parameter to the calling method is not shorter than a given minimum
length. |
static <T,C extends Collection<T>> |
checkNotShorter(C reference,
String referenceName,
int minLength)
Ensures that a
Collection passed as a parameter to the calling method is not shorter than a given minimum
length. |
static <T> T[] |
checkNotShorter(T[] reference,
String referenceName,
int minLength)
Ensures that an array passed as a parameter to the calling method is not shorter than a given minimum
length.
|
static List<String> |
checkSingleValuedList(List<String> referenceList,
String referenceName)
Ensures that a
List of String s passed as a parameter to the calling method is single-valued i.e. |
static String |
checkURISyntax(String reference,
String referenceName) |
static int |
checkWithinBounds(int value,
String referenceName,
int min,
int max)
Ensures that a given value is within the given bounds (greater or equal to the given minimum and lower or eqal to
the given maximum).
|
static String |
isAlphanumeric(String reference,
String referenceName)
Checks if the reference contains only Unicode letters or digits.
|
static String |
isAlphanumericSpace(String reference,
String referenceName)
Checks if the reference contains only Unicode letters, digits or space (
' ' ). |
@Nonnull public static CharSequence checkLength(CharSequence reference, @Nonnull String referenceName, int minLength, int maxLength) throws IllegalArgumentException
CharSequence
passed as a parameter to the calling method is not shorter than a given
minimum length and not greater than a maximum length.reference
- a CharSequence
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedmaxLength
- the maximum length allowedCharSequence
that was validatedIllegalArgumentException
- if the CharSequence
is shorter than the given minLength
or
longer than the given maxLength
@Nonnull public static <T,C extends Collection<T>> C checkLength(C reference, @Nonnull String referenceName, int minLength, int maxLength) throws IllegalArgumentException
Collection
passed as a parameter to the calling method is not shorter than a given minimum
length and not greater than a maximum length.T
- the typeC
- the typereference
- a Collection
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedmaxLength
- the maximum length allowedCollection
that was validatedIllegalArgumentException
- if the Collection
is shorter than the given minLength
or longer
than the given maxLength
@Nonnull public static <K,V,C extends Map<K,V>> C checkLength(C reference, @Nonnull String referenceName, int minLength, int maxLength) throws IllegalArgumentException
Map
passed as a parameter to the calling method is not shorter than a given minimum length
and not greater than a maximum length.K
- the typeV
- the typeC
- the typereference
- a Map
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedmaxLength
- the maximum length allowedMap
that was validatedIllegalArgumentException
- if the Map
is shorter than the given minLength
or longer than
the given maxLength
@Nonnull public static <T> T[] checkLength(T[] reference, @Nonnull String referenceName, int minLength, int maxLength) throws IllegalArgumentException
T
- the typereference
- an arrayreferenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedmaxLength
- the maximum length allowedIllegalArgumentException
- if the array is shorter than the given minLength
or longer than the
given maxLength
@Nonnull public static byte[] checkLength(byte[] reference, @Nonnull String referenceName, int minLength, int maxLength) throws IllegalArgumentException
reference
- an arrayreferenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedmaxLength
- the maximum length allowedIllegalArgumentException
- if the array is shorter than the given minLength
or longer than the
given maxLength
@Nonnull public static String checkNotBlank(String reference, @Nonnull String referenceName) throws IllegalArgumentException
String
passed as a parameter to the calling method is not blank.reference
- a String
referenceName
- the String
name which will appear in the exception messageString
that was validatedIllegalArgumentException
- if reference
is null, empty or blank@Nonnull public static char[] checkNotBlank(char[] reference, @Nonnull String referenceName) throws IllegalArgumentException
reference
- an array referencereferenceName
- the array name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static String checkNotBlankState(String reference, @Nonnull String referenceName) throws IllegalStateException
String
(involved in the state of the calling instance) is not blank.reference
- a String
referenceName
- the String
name which will appear in the exception messageString
that was validatedIllegalStateException
- if reference
is null, empty or blank@Nonnull public static char[] checkNotEmpty(char[] reference, @Nonnull String referenceName) throws IllegalArgumentException
reference
- an array referencereferenceName
- the array name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static byte[] checkNotEmpty(byte[] reference, @Nonnull String referenceName) throws IllegalArgumentException
reference
- an array referencereferenceName
- the array name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static int[] checkNotEmpty(int[] reference, @Nonnull String referenceName) throws IllegalArgumentException
reference
- an array referencereferenceName
- the array name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static <T> T[] checkNotEmpty(T[] reference, @Nonnull String referenceName) throws IllegalArgumentException
T
- the typereference
- an array referencereferenceName
- the array name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static <E> Collection<E> checkNotEmpty(Collection<E> reference, @Nonnull String referenceName) throws IllegalArgumentException
E
- the typereference
- a Collection referencereferenceName
- the Collection name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static <K,V> Map<K,V> checkNotEmpty(Map<K,V> reference, @Nonnull String referenceName) throws IllegalArgumentException
K
- the typeV
- the typereference
- a Map referencereferenceName
- the Map name which will appear in the exception messageIllegalArgumentException
- if reference
is null or empty@Nonnull public static CharSequence checkNotLonger(CharSequence reference, @Nonnull String referenceName, int maxLength) throws IllegalArgumentException
CharSequence
passed as a parameter to the calling method is not longer than a given
maximum length.reference
- a CharSequence
referenceName
- the String
name which will appear in the exception messagemaxLength
- the maximum length allowedCharSequence
that was validatedIllegalArgumentException
- if the CharSequence
is longer than the given maxLength
@Nonnull public static <T,C extends Collection<T>> C checkNotLonger(C reference, @Nonnull String referenceName, int maxLength) throws IllegalArgumentException
Collection
passed as a parameter to the calling method is not longer than a given maximum
length.T
- the typeC
- the typereference
- a Collection
referenceName
- the String
name which will appear in the exception messagemaxLength
- the maximum length allowedCollection
that was validatedIllegalArgumentException
- if the Collection
is longer than the given maxLength
@Nonnull public static <K,V,C extends Map<K,V>> C checkNotLonger(C reference, @Nonnull String referenceName, int maxLength) throws IllegalArgumentException
Map
passed as a parameter to the calling method is not longer than a given maximum
length.K
- the typeV
- the typeC
- the typereference
- a Map
referenceName
- the String
name which will appear in the exception messagemaxLength
- the maximum length allowedMap
that was validatedIllegalArgumentException
- if the Map
is longer than the given maxLength
@Nonnull public static <T> T[] checkNotLonger(T[] reference, @Nonnull String referenceName, int maxLength) throws IllegalArgumentException
T
- the typereference
- an arrayreferenceName
- the String
name which will appear in the exception messagemaxLength
- the maximum length allowedIllegalArgumentException
- if the array is longer than the given maxLength
@Nonnull public static byte[] checkNotLonger(byte[] reference, @Nonnull String referenceName, int maxLength) throws IllegalArgumentException
reference
- an arrayreferenceName
- the String
name which will appear in the exception messagemaxLength
- the maximum length allowedIllegalArgumentException
- if the array is longer than the given maxLength
public static int checkNotNegative(int value, @Nonnull String referenceName) throws IllegalArgumentException
value
- the value to checkreferenceName
- the variable name which will appear in the exception messagevalue
when valid otherwise throws an IllegalArgumentException.IllegalArgumentException
- if value
is lower than the given min
or is higher than the given
max
.@Nonnull public static <T> T checkNotNull(T reference, @Nonnull String referenceName) throws IllegalArgumentException
T
- the typereference
- an object referencereferenceName
- the object name which will appear in the exception messageIllegalArgumentException
- if reference
is null@Nonnull public static <T> T checkNotNullState(T reference, @Nonnull String referenceName) throws IllegalStateException
T
- the typereference
- an object referencereferenceName
- the object name which will appear in the exception messageIllegalStateException
- if reference
is null@Nonnull public static CharSequence checkNotShorter(CharSequence reference, @Nonnull String referenceName, int minLength) throws IllegalArgumentException
CharSequence
passed as a parameter to the calling method is not shorter than a given
minimum length.reference
- a CharSequence
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedCharSequence
that was validatedIllegalArgumentException
- if the CharSequence
is shorter than the given minLength
@Nonnull public static <T,C extends Collection<T>> C checkNotShorter(C reference, @Nonnull String referenceName, int minLength) throws IllegalArgumentException
Collection
passed as a parameter to the calling method is not shorter than a given minimum
length.T
- the typeC
- the typereference
- a Collection
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedCollection
that was validatedIllegalArgumentException
- if the Collection
is shorter than the given minLength
@Nonnull public static <K,V,C extends Map<K,V>> C checkNotShorter(C reference, @Nonnull String referenceName, int minLength) throws IllegalArgumentException
Map
passed as a parameter to the calling method is not shorter than a given minimum
length.K
- the typeV
- the typeC
- the typereference
- a Map
referenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedMap
that was validatedIllegalArgumentException
- if the Map
is shorter than the given minLength
@Nonnull public static <T> T[] checkNotShorter(T[] reference, @Nonnull String referenceName, int minLength) throws IllegalArgumentException
T
- the typereference
- the arrayreferenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedCollection
that was validatedIllegalArgumentException
- if the Collection
is shorter than the given minLength
@Nonnull public static byte[] checkNotShorter(byte[] reference, @Nonnull String referenceName, int minLength) throws IllegalArgumentException
reference
- the arrayreferenceName
- the String
name which will appear in the exception messageminLength
- the minimum length allowedCollection
that was validatedIllegalArgumentException
- if the Collection
is shorter than the given minLength
public static List<String> checkSingleValuedList(List<String> referenceList, String referenceName)
List
of String
s passed as a parameter to the calling method is single-valued i.e.
not null and contains exactly one non-blank value.referenceList
- a List
of String
sreferenceName
- the String
name which will appear in the exception messageIllegalArgumentException
- if referenceList
is null, empty, contains more than one value or
contains a blank value.public static String checkURISyntax(String reference, String referenceName) throws IllegalArgumentException
IllegalArgumentException
public static int checkWithinBounds(int value, @Nonnull String referenceName, int min, int max) throws IllegalArgumentException
value
- the value to checkreferenceName
- the variable name which will appear in the exception messagemin
- the given min
valuemax
- the given max
valuevalue
when valid otherwise throws an IllegalArgumentException.IllegalArgumentException
- if value
is lower than the given min
or is higher than the given
max
.public static String isAlphanumeric(@Nullable String reference, @Nonnull String referenceName) throws IllegalArgumentException
Checks if the reference contains only Unicode letters or digits.
null
will throw an IllegalArgumentException
. An empty reference (length()=0
) will throw
an IllegalArgumentException
.
Preconditions.isAlphanumeric(null) = IllegalArgumentException Preconditions.isAlphanumeric("") = IllegalArgumentException Preconditions.isAlphanumeric(" ") = IllegalArgumentException Preconditions.isAlphanumeric("abc") = true Preconditions.isAlphanumeric("ab c") = IllegalArgumentException Preconditions.isAlphanumeric("ab2c") = true Preconditions.isAlphanumeric("ab-c") = IllegalArgumentException
reference
- the value to checkreferenceName
- the variable name which will appear in the exception messagereference
when valid otherwise throws an IllegalArgumentException.IllegalArgumentException
- if reference
contains non alphanumeric characters.public static String isAlphanumericSpace(@Nullable String reference, @Nonnull String referenceName) throws IllegalArgumentException
Checks if the reference contains only Unicode letters, digits or space (' '
).
null
will throw an IllegalArgumentException
. An empty reference
(length()=0) will return
reference
.
Preconditions.isAlphanumericSpace(null) = IllegalArgumentException Preconditions.isAlphanumericSpace("") = true Preconditions.isAlphanumericSpace(" ") = true Preconditions.isAlphanumericSpace("abc") = true Preconditions.isAlphanumericSpace("ab c") = true Preconditions.isAlphanumericSpace("ab2c") = true Preconditions.isAlphanumericSpace("ab-c") = IllegalArgumentException
reference
- the value to checkreferenceName
- the variable name which will appear in the exception messagereference
when valid otherwise throws an IllegalArgumentException.IllegalArgumentException
- if reference
contains non alphanumeric and/or space characters.Copyright © 2020. All Rights Reserved.