Class ObjectUtils


  • public abstract class ObjectUtils
    extends Object
    Several general utilities to deal with objects and the like.
    • Method Detail

      • nonNullOrDefault

        public static <T> T nonNullOrDefault​(T instance,
                                             T ifNull)
        Returns the given instance if it is not null. Otherwise ifNull is returned.
        Type Parameters:
        T - the type of the given instance and ifNull
        Parameters:
        instance - the object to validate if it is no null
        ifNull - the object to return if instance is null
        Returns:
        the given instance if it is not null. Otherwise ifNull is returned
      • doIfNotNull

        public static <T> ObjectUtils.OrElse doIfNotNull​(T instance,
                                                         Consumer<T> action)
        Performs the given action consuming the given instance, granted that it is not null.
        Type Parameters:
        T - the type of the given instance and object to consume by the given action
        Parameters:
        instance - the object to consume by the given action, if it is not null
        action - the Consumer invoked with the given instance
        Returns:
        an ObjectUtils.OrElse to allow to perform another operation if the given action was not executed
      • silently

        public static <T> void silently​(T instance,
                                        Consumer<T> action)
        Silently performs the given action consuming the given instance, granted that it is not null. Will swallow any exceptions thrown by the action
        Type Parameters:
        T - the type of the given instance and object to consume by the given action
        Parameters:
        instance - the object to consume by the given action, if it is not null
        action - the Consumer to silently invoke with the given instance
      • hasLength

        public static boolean hasLength​(String s)
        Verify if the given String is not null and is not String.isEmpty().
        Parameters:
        s - the String to validate if it has any length.
        Returns:
        true if the given String is not null or empty, false otherwise
      • randomHex

        public static String randomHex​(int length)
        Creates a random String of the given length.
        Parameters:
        length - the size of the random String to return
        Returns:
        a random String of the given length