Class CommonProperties

java.lang.Object
org.klojang.check.CommonProperties

public class CommonProperties extends Object
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 Details

    • box

      public static IntFunction<Integer> box()
      Returns the boxed version of the argument. Equivalent to Integer::valueOf. This "property" is especially useful to get access to the many Relation checks in the CommonChecks class when validating an int 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

      public static ToIntFunction<Integer> unbox()
      Returns the unboxed version of the argument. Equivalent to Integer::intValue.
      Returns:
      The unboxed version of the argument
    • strval

      public static <T> Function<T,String> strval()
      Returns the result of calling toString() on the argument. Equivalent to Object::toString.
      Type Parameters:
      T - The type of the object on which to call toString{}.
      Returns:
      The result of calling toString() on the argument
    • strlen

      public static <T extends CharSequence> ToIntFunction<T> strlen()
      Returns the length of a CharSequence. Equivalent to CharSequence::length.
      Type Parameters:
      T - the type of the CharSequence
      Returns:
      The length of a CharSequence
    • toUpperCase

      public static Function<String,String> toUpperCase()
      Returns the upper case version of the argument. Equivalent to String::toUpperCase.
      Returns:
      The upper case version of the argument
    • toLowerCase

      public static Function<String,String> toLowerCase()
      Returns the lower case version of the argument. Equivalent to String::toLowerCase.
      Returns:
      The lower case version of the argument
    • type

      public static <T> Function<T,Class<?>> type()
      Returns the Class of the argument. Equivalent to Object::getClass.
      Type Parameters:
      T - The type of the object
      Returns:
      The Class of the argument
    • constants

      public static <T extends Enum<T>> Function<Class<T>,T[]> constants()
      Returns the constants of an enum class. Equivalent to Class::getEnumConstants.
      Type Parameters:
      T - The enum class
      Returns:
      The constants of an enum class
    • name

      public static <T extends Enum<?>> Function<T,String> name()
      Returns the name of an enum constant. Equivalent to Enum::name.
      Type Parameters:
      T - The type of the enum class
      Returns:
      The name of the enum constant
    • ordinal

      public static <T extends Enum<?>> ToIntFunction<T> ordinal()
      Returns the ordinal of an enum constant. Equivalent to Enum::ordinal.
      Type Parameters:
      T - The type of the enum class
      Returns:
      The ordinal of the enum constant
    • length

      public static <T> ToIntFunction<T> 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

      public static <C extends Collection<?>> ToIntFunction<C> size()
      Returns the size of a Collection argument. Equivalent to Collection::size.
      Type Parameters:
      C - The type of the Collection
      Returns:
      The size of a Collection argument
    • listSize

      public static <L extends List<?>> ToIntFunction<L> listSize()
      Returns the size of a List argument. Equivalent to List::size.
      Type Parameters:
      L - The type of the List
      Returns:
      Returns the size of a List argument
    • setSize

      public static <S extends Set<?>> ToIntFunction<S> setSize()
      Returns the size of a Set argument. Equivalent to Set::size.
      Type Parameters:
      S - The type of the Set.
      Returns:
      The size of a Set argument
    • mapSize

      public static <M extends Map<?, ?>> ToIntFunction<M> mapSize()
      Returns the size of a Map argument. Equivalent to Map::size.
      Type Parameters:
      M - The type of the Map
      Returns:
      The size of a Map argument
    • keySet

      public static <K, V, M extends Map<K, V>> Function<M,Set<K>> keySet()
      Returns the keys of a Map argument. Equivalent to Map::keySet.
      Type Parameters:
      K - The type of the keys in the map
      V - The type of the values in the map
      M - The type of the map
      Returns:
      The keys of a Map argument
    • values

      public static <K, V, M extends Map<K, V>> Function<M,Collection<V>> values()
      Returns the keys of a Map argument. Equivalent to Map::values.
      Type Parameters:
      K - The type of the keys in the map
      V - The type of the values in the map
      M - The type of the map
      Returns:
      The values of a Map argument
    • key

      public static <K, V> Function<Map.Entry<K,V>,K> key()
      Returns the key of a Map entry. Equivalent to Map.Entry::getKey.
      Type Parameters:
      K - The type of the key of the entry
      V - The type of the value of the entry
      Returns:
      The key of a Map entry
    • value

      public static <K, V> Function<Map.Entry<K,V>,V> value()
      Returns the value of a Map entry. Equivalent to Map.Entry::getValue.
      Type Parameters:
      K - The type of the key of the entry
      V - The type of the value of the entry
      Returns:
      A Function that returns the value of a Map entry
    • abs

      public static IntUnaryOperator abs()
      Returns the absolute value of an int argument. Equivalent to Math::abs.
      Returns:
      A Function that returns the absolute value of an integer
    • ABS

      public static <T extends Number> Function<T,T> ABS()
      Returns the absolute value of a Number. The following Number types are supported: Integer, Double, Long, Float, Short, Byte, BigInteger, BigDecimal.
      Type Parameters:
      T - The type of the Number
      Returns:
      The absolute value of a Number