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 IntUnaryOperator
abs()
Returns the absolute value of anint
argument.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 aMap
entry.keySet()
Returns the keys of aMap
argument.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 aList
argument.static <M extends Map<?,
?>>
ToIntFunction<M> mapSize()
Returns the size of aMap
argument.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 aSet
argument.static <C extends Collection<?>>
ToIntFunction<C> size()
Returns the size of aCollection
argument.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 theClass
of the argument.static ToIntFunction
<Integer> unbox()
Returns the unboxed version of the argument.value()
Returns the value of aMap
entry.static <K,
V, M extends Map<K, V>>
Function<M, Collection<V>> values()
Returns the keys of aMap
argument.
-
Method Details
-
box
Returns the boxed version of the argument. Equivalent toInteger::valueOf
. This "property" is especially useful to get access to the manyRelation
checks in theCommonChecks
class when validating anint
argument:// 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 theClass
of the argument. Equivalent toObject::getClass
.- Type Parameters:
T
- The type of the object- Returns:
- The
Class
of 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
Function
that returns the length of an array
-
size
Returns the size of aCollection
argument. Equivalent toCollection::size
.- Type Parameters:
C
- The type of theCollection
- Returns:
- The size of a
Collection
argument
-
listSize
Returns the size of aList
argument. Equivalent toList::size
.- Type Parameters:
L
- The type of theList
- Returns:
- Returns the size of a
List
argument
-
setSize
Returns the size of aSet
argument. Equivalent toSet::size
.- Type Parameters:
S
- The type of theSet
.- Returns:
- The size of a
Set
argument
-
mapSize
Returns the size of aMap
argument. Equivalent toMap::size
.- Type Parameters:
M
- The type of theMap
- Returns:
- The size of a
Map
argument
-
keySet
Returns the keys of aMap
argument. 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
Map
argument
-
values
Returns the keys of aMap
argument. 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
Map
argument
-
key
Returns the key of aMap
entry. Equivalent toMap.Entry::getKey
.- Type Parameters:
K
- The type of the key of the entryV
- The type of the value of the entry- Returns:
- The key of a
Map
entry
-
value
Returns the value of aMap
entry. Equivalent toMap.Entry::getValue
.- Type Parameters:
K
- The type of the key of the entryV
- The type of the value of the entry- Returns:
- A
Function
that returns the value of aMap
entry
-
abs
Returns the absolute value of anint
argument. Equivalent toMath::abs
.- Returns:
- A
Function
that returns the absolute value of an integer
-
ABS
Returns the absolute value of aNumber
. The followingNumber
types are supported:Integer
,Double
,Long
,Float
,Short
,Byte
,BigInteger
,BigDecimal
.- Type Parameters:
T
- The type of theNumber
- Returns:
- The absolute value of a
Number
-