Ensures the truth of an expression involving one or more parameters to the calling method.
Ensures the truth of an expression involving one or more parameters to the calling method.
an expression: Boolean
a template for the exception message should the
check fail. The message is formed by replacing each %s
placeholder in the template with an argument. These are matched by
position - the first %s
gets errorMessageArgs[0]
, etc.
Unmatched arguments will be appended to the formatted message in square
braces. Unmatched placeholders will be left as-is. Arguments are converted
to strings using String#valueOf(Any)
.
the first argument to be substituted into the message template.
the other arguments to be substituted into the message template.
IllegalArgumentException
if expression
is false
NullPointerException
if the check fails and either
errorMessageTemplate
or errorMessageArgs
is null (don't let
this happen)
Ensures the truth of an expression involving one or more parameters to the calling method.
Ensures the truth of an expression involving one or more parameters to the calling method.
an expression: Boolean
the exception message to use if the check fails will
be converted to a string using String#valueOf(Any)
IllegalArgumentException
if expression
is false
Ensures the truth of an expression involving one or more parameters to the calling method.
Ensures the truth of an expression involving one or more parameters to the calling method.
an expression: Boolean
IllegalArgumentException
if expression
is false
Ensures that index
specifies a valid element in an array,
list or string of size size
.
Ensures that 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.
a user-supplied index identifying an element of an array, list or string
the size of that array, list or string
the text to use to describe this index in an error message
the value of index
IllegalArgumentException
if size
is negative
IndexOutOfBoundsException
if index
is negative or is not
less than size
Ensures that index
specifies a valid element in an array,
list or string of size size
.
Ensures that 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.
a user-supplied index identifying an element of an array, list or string
the size of that array, list or string
the value of index
IllegalArgumentException
if size
is negative
IndexOutOfBoundsException
if index
is negative or is not
less than size
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
an objecreference: T
a template for the exception message should the
check fail. The message is formed by replacing each %s
placeholder in the template with an argument. These are matched by
position - the first %s
gets errorMessageArgs[0]
, etc.
Unmatched arguments will be appended to the formatted message in square
braces. Unmatched placeholders will be left as-is. Arguments are converted
to strings using String#valueOf(Any)
.
the first argument to be substituted into the message template.
the other arguments to be substituted into the message template.
the non-null reference that was validated
NullPointerException
if reference
is null
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
an objecreference: T
the exception message to use if the check fails will
be converted to a string using String#valueOf(Any)
the non-null reference that was validated
NullPointerException
if reference
is null
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
Ensures that an objecreference: T passed as a parameter to the calling method is not null.
an objecreference: T
the non-null reference that was validated
NullPointerException
if reference
is null
Ensures that index
specifies a valid position in an array,
list or string of size size
.
Ensures that index
specifies a valid position in an array,
list or string of size size
. A position index may range from zero
to size
, inclusive.
a user-supplied index identifying a position in an array, list or string
the size of that array, list or string
the text to use to describe this index in an error message
the value of index
IllegalArgumentException
if size
is negative
IndexOutOfBoundsException
if index
is negative or is
greater than size
Ensures that index
specifies a valid position in an array,
list or string of size size
.
Ensures that index
specifies a valid position in an array,
list or string of size size
. A position index may range from zero
to size
, inclusive.
a user-supplied index identifying a position in an array, list or string
the size of that array, list or string
the value of index
IllegalArgumentException
if size
is negative
IndexOutOfBoundsException
if index
is negative or is
greater than size
Ensures that start
and end
specify a valid positions
in an array, list or string of size size
, and are in order.
Ensures that 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.
a user-supplied index identifying a starting position in an array, list or string
a user-supplied index identifying a ending position in an array, list or string
the size of that array, list or string
IllegalArgumentException
if size
is negative
IndexOutOfBoundsException
if either index is negative or is
greater than size
, or if end
is less than start
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
a expression: Boolean
a template for the exception message should the
check fail. The message is formed by replacing each %s
placeholder in the template with an argument. These are matched by
position - the first %s
gets errorMessageArgs[0]
, etc.
Unmatched arguments will be appended to the formatted message in square
braces. Unmatched placeholders will be left as-is. Arguments are converted
to strings using String#valueOf(Any)
.
the first argument to be substituted into the message template.
the other arguments to be substituted into the message template.
IllegalStateException
if expression
is false
NullPointerException
if the check fails and either
errorMessageTemplate
or errorMessageArgs
is null (don't let
this happen)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
an expression: Boolean
the exception message to use if the check fails will
be converted to a string using String#valueOf(Any)
IllegalStateException
if expression
is false
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
an expression: Boolean
IllegalStateException
if expression
is false
Simple static methods to be called at the start of your own methods to verify correct arguments and state. This allows constructs such as
to be replaced with the more compact
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 JUnitassertTrue
call.Warning: only the
"%s"
specifier is recognized as a placeholder in these messages.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 also the Guava User Guide on using Preconditions
0.7 (copied from guava-libraries)