Package io.cui.tools.lang
Class MoreObjects
- java.lang.Object
-
- io.cui.tools.lang.MoreObjects
-
public final class MoreObjects extends Object
Provides some utilities in the context ofObject- Author:
- Oliver Wolff
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanallNonNull(Object... objects)Simple helper checking whether a number of given Objects are notnullstatic booleanallNull(Object... objects)Simple helper checking whether a number of given Objects arenullstatic <T> TfirstNonNull(T... values)Returns the first value in the array which is notnull.static <T> TgetFirstNonNull(Supplier<T>... suppliers)Executes the given suppliers in order and returns the first return value where a value other thannullis returned.static <T> TrequireType(Object underCheck, Class<T> expectedType)Checks and returns the given Object if it is assignable to the given targetType.
-
-
-
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 anIllegalArgumentException. This will be thrown also if one of the parameters isnull.- 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 anIllegalArgumentExceptionwill be thrown.expectedType- checks the type . If it is null anIllegalArgumentExceptionwill 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 notnull- Parameters:
objects-- Returns:
trueif there is nonullvalue given,falseif at least one null value is given. An empty varags parameter therefore results intrue(no null object found)
-
allNull
public static boolean allNull(Object... objects)
Simple helper checking whether a number of given Objects arenull- Parameters:
objects-- Returns:
trueif there is no non-nullvalue given,falseif at least one non-null value is given. An empty varags parameter therefore results intrue(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 arenullor the array isnullor empty thennullis 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 benullor empty- Returns:
- the first value from
valueswhich is notnull, ornullif 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
nullis returned. Once a non-nullvalue is obtained, all following suppliers are not executed anymore. If all the return values arenullor no suppliers are provided thennullis 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.nullvalues are ignored. Suppliers may returnnullor a value of type @{code T}- Returns:
- the first return value from
supplierswhich is notnull, ornullif there are no non-null values
-
-