Class CommonProperties
java.lang.Object
org.klojang.check.CommonProperties
Defines various functions that can optionally be passed as the first argument to
the
has(...) and notHas(...) methods of IntCheck and
ObjectCheck. These functions are associated with a description of the
property they expose, so generating an error message requires very little
hand-crafting. For example:
Check.that(car, "car").has(strval(), equalTo(), "BMW"); // Error message: "car.toString() must be equal to BMW (was Toyota)"
Note that the word "property" is suggestive, but also misleading. The functions defined here really are just that: functions that transform the argument into some other value, which can then be subjected to further tests.
Check.that(temperature, "temperature").has(abs(), lt(), 20); // Error message: "abs(temperature) must be < 20 (was -39)"
As with the checks in the CommonChecks class, none of the functions
defined here execute a preliminary null check. Many of them simply return a
method reference. They rely upon being embedded in chain of checks, the first of
which should be the notNull check (if necessary).
- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptionstatic IntUnaryOperatorabs()Returns the absolute value of anintargument.ABS()Returns the absolute value of aNumber.static IntFunction<Integer> box()Returns the boxed version of the argument.Returns the constants of an enum class.key()Returns the key of aMapentry.keySet()Returns the keys of aMapargument.static <T> ToIntFunction<T> length()A function that returns the length of an array argument.static <L extends List<?>>
ToIntFunction<L> listSize()Returns the size of aListargument.static <M extends Map<?,?>>
ToIntFunction<M> mapSize()Returns the size of aMapargument.name()Returns the name of an enum constant.static <T extends Enum<?>>
ToIntFunction<T> ordinal()Returns the ordinal of an enum constant.static <S extends Set<?>>
ToIntFunction<S> setSize()Returns the size of aSetargument.static <C extends Collection<?>>
ToIntFunction<C> size()Returns the size of aCollectionargument.static <T extends CharSequence>
ToIntFunction<T> strlen()Returns the length of aCharSequence.strval()Returns the result of callingtoString()on the argument.Returns the lower case version of the argument.Returns the upper case version of the argument.type()Returns theClassof the argument.static ToIntFunction<Integer> unbox()Returns the unboxed version of the argument.value()Returns the value of aMapentry.static <K, V, M extends Map<K,V>>
Function<M, Collection<V>> values()Returns the keys of aMapargument.
-
Method Details
-
box
Returns the boxed version of the argument. Equivalent toInteger::valueOf. This "property" is especially useful to get access to the manyRelationchecks in theCommonChecksclass when validating anintargument:// WON'T COMPILE! IntCheck does not have method is(Relation, Object) Check.that(42).is(keyIn(), map); // OK. ObjectCheck does have method is(Relation, Object) Check.that((Integer) 42).is(keyIn(), map); // More idiomatic: Check.that(42).has(box(), keyIn(), map);- Returns:
- The boxed version of the argument
-
unbox
Returns the unboxed version of the argument. Equivalent toInteger::intValue.- Returns:
- The unboxed version of the argument
-
strval
Returns the result of callingtoString()on the argument. Equivalent toObject::toString.- Type Parameters:
T- The type of the object on which to calltoString{}.- Returns:
- The result of calling
toString()on the argument
-
strlen
Returns the length of aCharSequence. Equivalent toCharSequence::length.- Type Parameters:
T- the type of theCharSequence- Returns:
- The length of a
CharSequence
-
toUpperCase
Returns the upper case version of the argument. Equivalent toString::toUpperCase.- Returns:
- The upper case version of the argument
-
toLowerCase
Returns the lower case version of the argument. Equivalent toString::toLowerCase.- Returns:
- The lower case version of the argument
-
type
Returns theClassof the argument. Equivalent toObject::getClass.- Type Parameters:
T- The type of the object- Returns:
- The
Classof the argument
-
constants
Returns the constants of an enum class. Equivalent toClass::getEnumConstants.- Type Parameters:
T- The enum class- Returns:
- The constants of an enum class
-
name
Returns the name of an enum constant. Equivalent toEnum::name.- Type Parameters:
T- The type of the enum class- Returns:
- The name of the enum constant
-
ordinal
Returns the ordinal of an enum constant. Equivalent toEnum::ordinal.- Type Parameters:
T- The type of the enum class- Returns:
- The ordinal of the enum constant
-
length
A function that returns the length of an array argument.- Type Parameters:
T- The type of the array.- Returns:
- A
Functionthat returns the length of an array
-
size
Returns the size of aCollectionargument. Equivalent toCollection::size.- Type Parameters:
C- The type of theCollection- Returns:
- The size of a
Collectionargument
-
listSize
Returns the size of aListargument. Equivalent toList::size.- Type Parameters:
L- The type of theList- Returns:
- Returns the size of a
Listargument
-
setSize
Returns the size of aSetargument. Equivalent toSet::size.- Type Parameters:
S- The type of theSet.- Returns:
- The size of a
Setargument
-
mapSize
Returns the size of aMapargument. Equivalent toMap::size.- Type Parameters:
M- The type of theMap- Returns:
- The size of a
Mapargument
-
keySet
Returns the keys of aMapargument. Equivalent toMap::keySet.- Type Parameters:
K- The type of the keys in the mapV- The type of the values in the mapM- The type of the map- Returns:
- The keys of a
Mapargument
-
values
Returns the keys of aMapargument. Equivalent toMap::values.- Type Parameters:
K- The type of the keys in the mapV- The type of the values in the mapM- The type of the map- Returns:
- The values of a
Mapargument
-
key
-
value
-
abs
Returns the absolute value of anintargument. Equivalent toMath::abs.- Returns:
- A
Functionthat returns the absolute value of an integer
-
ABS
-