Class ObjectHelper


  • public class ObjectHelper
    extends Object
    Helper class for objects
    • Constructor Detail

      • ObjectHelper

        public ObjectHelper()
    • Method Detail

      • anyIsNull

        public static boolean anyIsNull​(Object... elements)
        Returns true if some element is null. If elements is empty or null then returns true.
      • areAllNulls

        public static boolean areAllNulls​(Object... elements)
        Returns true if all elements are null. If elements is empty or null then returns true.
      • coalesce

        @SafeVarargs
        public static <T> T coalesce​(T... values)
        Returns the first non-null value
      • compare

        public static <T extends Comparable<T>> int compare​(T o1,
                                                            T o2)
        Compares 2 Comparables, handling nulls
      • defaultValue

        public static <T,​R> R defaultValue​(R object,
                                                 T fstArg,
                                                 Function<T,​R> whenNull)
        Lazy version of defaultValue(Object, Object) but accepting an argument that will be passed to the function (invoked ONLY if the first parameter is null). This will allow use lambda expressions an method references.
      • defaultValue

        public static <T> T defaultValue​(T object,
                                         T whenNull)
        Returns the second parameter when the first is null
      • emptyToNull

        public static Object emptyToNull​(Object value)
        If the given value is empty (see isEmpty(Object object)) returns null, otherwise returns the same value.
      • equalsOrBothNull

        public static boolean equalsOrBothNull​(Object first,
                                               Object second)
        Returns true when either both objects are non null and equals or both null
      • hashCode

        public static int hashCode​(Object object)
        Returns the hashCode of the given object, handling nulls (-1 if null)
      • ifType

        public static <T> T ifType​(Object object,
                                   Class<T> expectedType)
        Returns the argument if it is an instance of the given class, otherwise null
      • isEmpty

        public static boolean isEmpty​(Object object)
        Returns whether the given object is null or an empty string, collection, map or array
      • isNotOneOf

        public static boolean isNotOneOf​(Object element,
                                         Object... objects)
        Returns true if element is not one of the given objects. If element or objects are null it returns false;
      • isOneOf

        public static boolean isOneOf​(Object element,
                                      Object... objects)
        Returns true if element is one of the objects If element or objects are null it returns false;
      • requireOneOf

        public static void requireOneOf​(Supplier<String> errorMessageSupplier,
                                        Object element,
                                        Object... objects)
                                 throws IllegalArgumentException
        Checks if element is one of the objects
        Parameters:
        errorMessageSupplier - (Optional) Supplier to get the message in case of error
        Throws:
        IllegalArgumentException - if element is not one of the given objects.