Class ObjectTools


  • public final class ObjectTools
    extends Object
    Tool methods for objects, similar to Objects.
    • Constructor Detail

      • ObjectTools

        public ObjectTools()
    • Method Detail

      • compareToNullSafe

        public static <T> int compareToNullSafe​(T o1,
                                                T o2,
                                                Comparator<? super T> comp)
        Compares the designated objects using the designated comparator. If one of the elements is null, it will be treated as lesser. Both elements being null will be treated as equal objects.
        Parameters:
        o1 - The first object to be compared.
        o2 - The second object to be compared.
        comp - The comparator for the objects.
        Returns:
        The comparison result of the two objects.
      • compareToNullSafe

        public static <T extends Comparable<? super T>> int compareToNullSafe​(T o1,
                                                                              T o2)
        Compares the designated objects checking for null before applying Comparable.compareTo(Object). If one of the elements is null, it will be treated as lesser. Both elements being null will be treated as equal objects.
        Parameters:
        o1 - The first object to be compared.
        o2 - The second object to be compared.
        Returns:
        The comparison result of the two objects.
      • transformNullSafe

        public static <S,​T> Function<S,​T> transformNullSafe​(Function<S,​T> transformator)
        Handles null before calling the designated transformating function. If the provided value is null, null will be returned without calling the designated function.
        Parameters:
        transformator - The transformator which to apply which does not handle null yet or not properly.This must not be null.
        Returns:
        The transformed object instance or null.
      • transformNullSafe

        public static <S,​T> T transformNullSafe​(S s,
                                                      Function<S,​T> transformator)
        Transforms the designated object using the designated transformator. If the object is null, null will be returned without transforming anything.
        Parameters:
        s - The object which to transform or null.
        transformator - The transformator which to apply. This must not be null.
        Returns:
        The transformed object instance or null.
      • transformNullRepl

        public static <S,​T> T transformNullRepl​(S s,
                                                      Function<S,​T> transformator,
                                                      Supplier<T> nullSupplier)
        Transforms the designated object using the designated transformator. If the object is null, the designated supplier will be called.
        Parameters:
        s - The object which to transform or null.
        transformator - The transformator which to apply. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        The transformed object instance or null.