java.lang.Object
de.cuioss.tools.lang.MoreObjects
Provides some utilities in the context of
Object- Author:
- Oliver Wolff
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanallNonNull(Object... objects) Simple helper checking whether a number of given Objects are notnullstatic booleanSimple 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.
-
Constructor Details
-
MoreObjects
public MoreObjects()
-
-
Method Details
-
requireType
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
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
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
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
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
-