Class MoreObjects


  • public final class MoreObjects
    extends Object
    Provides some utilities in the context of Object
    Author:
    Oliver Wolff
    • Method Detail

      • requireType

        public static <T> T requireType​(Object underCheck,
                                        Class<T> expectedType)
        Checks and returns the given Object if it is assignable to the given targetType. Otherwise, it throws an IllegalArgumentException. This will be thrown also if one of the parameters is null.
        Type Parameters:
        T - defining the type to be returned.
        Parameters:
        underCheck - KeyStoreType to be checked / cast. If it is null or is not assignable to expectedType an IllegalArgumentException will be thrown.
        expectedType - checks the type . If it is null an IllegalArgumentException will be thrown
        Returns:
        the cast Objected of type T if applicable.
        Throws:
        IllegalArgumentException - if the given type is either null or not the expected type
      • allNonNull

        public static boolean allNonNull​(Object... objects)
        Simple helper checking whether a number of given Objects are not null
        Parameters:
        objects -
        Returns:
        true if there is no null value given, false if at least one null value is given. An empty varags parameter therefore results in true (no null object found)
      • allNull

        public static boolean allNull​(Object... objects)
        Simple helper checking whether a number of given Objects are null
        Parameters:
        objects -
        Returns:
        true if there is no non-null value given, false if at least one non-null value is given. An empty varags parameter therefore results in true (no non-null object found)
      • firstNonNull

        @SafeVarargs
        public static <T> T firstNonNull​(T... values)

        Returns the first value in the array which is not null. If all the values are null or the array is null or empty then null is returned.

         MoreObjects.firstNonNull(null, null)      = null
         MoreObjects.firstNonNull(null, "")        = ""
         MoreObjects.firstNonNull(null, null, "")  = ""
         MoreObjects.firstNonNull(null, "zz")      = "zz"
         MoreObjects.firstNonNull("abc", *)        = "abc"
         MoreObjects.firstNonNull(null, "xyz", *)  = "xyz"
         MoreObjects.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE
         MoreObjects.firstNonNull()                = null
         
        Type Parameters:
        T - the component type of the array
        Parameters:
        values - the values to test, may be null or empty
        Returns:
        the first value from values which is not null, or null if there are no non-null values
      • getFirstNonNull

        @SafeVarargs
        public static <T> T getFirstNonNull​(Supplier<T>... suppliers)

        Executes the given suppliers in order and returns the first return value where a value other than null is returned. Once a non-null value is obtained, all following suppliers are not executed anymore. If all the return values are null or no suppliers are provided then null is returned.

         MoreObjects.getFirstNonNull(null, () -> null) = null
         MoreObjects.getFirstNonNull(() -> null, () -> "") = ""
         MoreObjects.getFirstNonNull(() -> "", () -> throw new IllegalStateException()) = ""
         MoreObjects.getFirstNonNull(() -> null, () -> "zz) = "zz"
         MoreObjects.getFirstNonNull() = null
         
        Type Parameters:
        T - the type of the return values
        Parameters:
        suppliers - the suppliers returning the values to test. null values are ignored. Suppliers may return null or a value of type @{code T}
        Returns:
        the first return value from suppliers which is not null, or null if there are no non-null values