@GwtCompatible public final class StringCheck extends ObjectBaseCheck<String,StringCheck>
String
arguments.
While String is a very generic data type, string arguments are often used to provide a specific kind of data, for example names, URLs or even numbers. Therefore, Strings have to be validated. This class provides many check methods to simplify this.
The simple checks include methods like isNotEmpty()
or
hasLength(int)
. But there are also more powerful checks like
containsAll(CharSequence...)
,
matches(String)
or even isInt()
.
arg, nullAllowed
Constructor and Description |
---|
StringCheck(String arg)
For internal use only.
|
Modifier and Type | Method and Description |
---|---|
StringCheck |
contains(CharSequence sequence)
Checks that the string argument contains the given character sequence,
throwing an exception otherwise.
|
StringCheck |
containsAll(CharSequence... sequences)
Checks that the string argument contains all one of the given character
sequences, throwing an exception otherwise.
|
StringCheck |
containsAny(CharSequence... sequences)
Checks that the string argument contains at least one of the given
character sequences, throwing an exception otherwise.
|
StringCheck |
endsWith(String suffix)
Checks that the string argument end with the given string, throwing an
exception otherwise.
|
StringCheck |
hasLength(int length)
Checks that the string argument has the given length, throwing an
exception otherwise.
|
StringCheck |
hasLengthBetween(int min,
int max)
Checks that the string argument's length is between the two given
numbers, throwing an exception otherwise.
|
IntCheck |
hasLengthWhich()
Creates an
IntCheck property check for the length of the string
argument. |
StringCheck |
is(String string)
Checks that the string argument is the same string as the given one, throwing an
exception otherwise.
|
StringCheck |
isInt()
Checks that the string argument is is a valid
int , throwing
an exception otherwise. |
IntCheck |
isIntWhich()
First checks that the string argument is a valid
int and
then "converts" this check into an IntCheck and returns it. |
StringCheck |
isNotEmpty()
Checks that the string argument is not empty, throwing an exception
otherwise.
|
StringCheck |
isNotWhitespace()
Checks that the string argument is not empty and does not consist
entirely of whitespace, throwing an exception otherwise.
|
StringCheck |
isUrl()
Checks that the string argument is is a valid URL, throwing an exception
otherwise.
|
UrlCheck |
isUrlWhich()
First checks that the string argument is a valid URL and then "converts"
this check into an
UrlCheck and returns it. |
StringCheck |
matches(Pattern regex)
Checks that the string argument matches the given compiled regular
expression pattern, throwing an exception otherwise.
|
StringCheck |
matches(String regex)
Checks that the string argument matches the given regular expression,
throwing an exception otherwise.
|
StringCheck |
startsWith(String prefix)
Checks that the string argument starts with the given string, throwing an
exception otherwise.
|
check, checkNull, checkWithCause, hasClass, intPropertyCheck, isEqualTo, isInstanceOf, isNotNull, isNullOr, isSameAs, objectPropertyCheck
checkConversion, me, named, not
public StringCheck(String arg)
public StringCheck isNotEmpty()
The message type used for exceptions thrown by this method is
MessageType.ARG_EMPTY
.
IllegalArgumentException
- if the string argument is emptypublic StringCheck isNotWhitespace()
The message type used for exceptions thrown by this method is
MessageType.ARG_WHITESPACE
.
IllegalArgumentException
- if the string argument is empty or consists entirely of
whitespacepublic StringCheck hasLength(int length)
The message type used for exceptions thrown by this method is
MessageType.ARG_LENGTH
.
To create more sophisticated checks using the string size, use the
hasLengthWhich()
property check.
length
- The length the string should haveIllegalArgumentException
- if the string argument has a size different from the given
onehasLengthBetween(int, int)
public StringCheck hasLengthBetween(int min, int max)
The message type used for exceptions thrown by this method is
MessageType.ARG_LENGTH_BETWEEN
.
To create more sophisticated checks using the array length, use the
hasLengthWhich()
property check.
min
- The minimum length of the stringmax
- The maximum lengthIllegalArgumentException
- If the length of the array is strictly less than
min
or strictly greater than
max
public IntCheck hasLengthWhich()
IntCheck
property check for the length of the string
argument. It can be used to create advanced checks using the string
length. For example:
Check.that(string).isNullOr().hasLengthWhich().isGreaterThan(3);
Just like a check method, this method first asserts the non-nullness of the argument, throwing an exception in case of a failure.
The returned check has the length of this check's string set as the
argument and an argument name that indicates this fact. If the string
reference is null
and allowed to be so (like in the above
example), the returned property check is completely disabled, meaning
none of its check methods will throw an exception. An inversion called
before this method is ignored and does not have any
effect on the check methods called on the returned property check.
public StringCheck startsWith(String prefix)
The message type used for exceptions thrown by this method is
MessageType.ARG_STARTS
.
prefix
- The string the string argument of this check should start withIllegalArgumentException
- if the string argument does not start with the given stringendsWith(String)
public StringCheck endsWith(String suffix)
The message type used for exceptions thrown by this method is
MessageType.ARG_ENDS
.
suffix
- The string the string argument of this check should end withIllegalArgumentException
- if the string argument does not end with the given stringstartsWith(String)
public StringCheck contains(CharSequence sequence)
containsAny
check can be very
useful in their inverted form:
Check.that(message).not().containsAny(swearWords);
The message type used for exceptions thrown by this method is
MessageType.ARG_CONTAINS
.
sequence
- The sequence the string argument should containIllegalArgumentException
- if the string argument does not contain the given sequencecontainsAny(CharSequence...)
,
containsAll(CharSequence...)
public StringCheck containsAny(CharSequence... sequences)
Check.that(message).not().containsAny(swearWords);
The message type used for exceptions thrown by this method is
MessageType.ARG_CONTAINS_ANY
.
sequences
- The array of sequences of which the string argument should
contain at least oneIllegalArgumentException
- if the string argument contains none of the given sequencescontainsAll(CharSequence...)
public StringCheck containsAll(CharSequence... sequences)
The message type used for exceptions thrown by this method is
MessageType.ARG_CONTAINS_ALL
.
sequences
- The sequences the string argument must containIllegalArgumentException
- if the string argument does not contain all the given
sequencescontainsAny(CharSequence...)
public StringCheck matches(String regex)
Pattern
object by passing it to the
matches(Pattern)
check.
The message type used for exceptions thrown by this method is
MessageType.ARG_MATCHES
.
regex
- The regular expression the string argument must match. See
Pattern
for more information.IllegalArgumentException
- if the string argument does not contain match the given
regular expressionmatches(Pattern)
@GwtIncompatible(value="java.util.regex.Pattern") public StringCheck matches(Pattern regex)
matches(String)
check, except that it is more
efficient if the same pattern object is used multiple times. Since
Pattern
objects are immutable (and therefore thread-safe), it is
safe to pass the same pattern to multiple checks. For maximum
performance, consider creating a static
pattern field for
each kind of matches()
check.
The message type used for exceptions thrown by this method is
MessageType.ARG_MATCHES
.
regex
- The regular expression the string argument must match. See
Pattern
for more information.IllegalArgumentException
- if the string argument does not contain match the given
regular expressionmatches(Pattern)
public StringCheck is(String string)
UrlCheck.hasHostWhich()
for example. However,
in conjunction with inversion (see BaseCheck.not()
), this method can also
be used to exclude a single value.
Note that this check behaves like ObjectBaseCheck.isEqualTo(Object)
, but is
shorter, accepts only String arguments, and uses the MessageType.ARG_IS
message type.
string
- The string this argument must be equal toIllegalArgumentException
- if the check argument is not equal to the given string@GwtIncompatible(value="java.net.URL") public StringCheck isUrl()
http://
, for example ftp://
or
file://
. To specify further requirements regarding the
protocol, the host or the path, use the isUrlWhich()
conversion
check. For more information about valid URLs, see the URL
class.
The message type used for exceptions thrown by this method is
MessageType.ARG_URL
.
IllegalArgumentException
- if the check argument is not a valid URL@GwtIncompatible(value="java.net.URL") public UrlCheck isUrlWhich()
UrlCheck
and returns it. This can be used to
create checks involving properties of the URL. For example:
Check.that(string).isUrlWhich().hasProtocol("http");
Just like a normal check method, this method first asserts the non-nullness of the argument, throwing an exception in case of a failure. Otherwise, the returned check has the same argument name as this check object and also behaves the same regarding the nullness of the argument.
The message type used for exceptions thrown by this method (not
the subsequent UrlCheck
checks) is MessageType.ARG_URL
.
InvalidCheckException
- If this check has just been inverted using BaseCheck.not()
.
This is prohibited as it would allow unintuitive checks.public StringCheck isInt()
int
, throwing
an exception otherwise. Note that numbers outside of the int
range (given by Integer.MIN_VALUE
and Integer.MAX_VALUE
)
are invalid. See Integer.parseInt(String)
for a description of
valid integer representations. To specify further requirements for the
int
that the string argument should represent, use the
isIntWhich()
conversion check.
The message type used for exceptions thrown by this method is
MessageType.ARG_INT
.
IllegalArgumentException
- if the check argument is not a valid int
public IntCheck isIntWhich()
int
and
then "converts" this check into an IntCheck
and returns it. This
can be used to create checks involving properties of the int
. For example:
Check.that(string).isIntWhich().isPositive();
Just like a normal check method, this method first asserts the
non-nullness of the argument, throwing an exception in case of a failure.
Otherwise, the returned check has the same argument name as this check
object. Also, if this check has been modified to allow null
and the argument is null
, the returned check is
completely disabled, meaning none of its check methods throw an
exception.
The message type used for exceptions thrown by this method (not
the subsequent IntCheck
checks) is MessageType.ARG_INT
.
int
check for asserting properties of the
int
represented by the string argumentInvalidCheckException
- If this check has just been inverted using BaseCheck.not()
.
This is prohibited as it would allow unintuitive checks.Copyright © 2012–2017 Michael Faes. All rights reserved.