Package no.mnemonic.commons.utilities
Class ObjectUtils
java.lang.Object
no.mnemonic.commons.utilities.ObjectUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
V> V Applies a conversion to 'value', or returns null if 'value' is null.static <T,
V> V Applies a conversion to 'value', or returns 'nullValue' if 'value' is null.static <T> void
ifNotNullDo
(T value, Consumer<T> consumer) Executes a consumer function on given value, unless value is nullstatic <T> T
Returns a default value if 'value' is null, returns 'value' otherwise.static <T> T
ifNull
(T value, T defaultValue) Returns a default value if 'value' is null, returns 'value' otherwise.static <T> T
Throws an exception if 'value' is null, returns 'value' otherwise.static <T> T
Throws a RuntimeException if 'value' is null, returns 'value' otherwise.
-
Method Details
-
notNull
Throws a RuntimeException if 'value' is null, returns 'value' otherwise.- Type Parameters:
T
- Type of value parameter.- Parameters:
value
- Object to be tested for null.message
- Message passed to the exception thrown.- Returns:
- 'value' if not null, throws a RuntimeException otherwise.
-
notNull
Throws an exception if 'value' is null, returns 'value' otherwise.- Type Parameters:
T
- Type of value parameter.- Parameters:
value
- Object to be tested for null.exception
- Exception to be thrown.- Returns:
- 'value' if not null, throws an exception otherwise.
- Throws:
Exception
- Thrown if 'value' is null.
-
ifNull
public static <T> T ifNull(T value, T defaultValue) Returns a default value if 'value' is null, returns 'value' otherwise.- Type Parameters:
T
- Type of value parameter.- Parameters:
value
- Value to be tested for null.defaultValue
- Value to return if 'value' is null.- Returns:
- Either 'value' or 'defaultValue'.
-
ifNull
Returns a default value if 'value' is null, returns 'value' otherwise.- Type Parameters:
T
- Type of value parameter.- Parameters:
value
- Value to be tested for null.defaultValue
- Supplier which provides a value to return if 'value' is null.- Returns:
- Either 'value' or default value provided by supplier.
-
ifNotNull
Applies a conversion to 'value', or returns null if 'value' is null.- Type Parameters:
T
- Type of value parameter.V
- Type of return value.- Parameters:
value
- Value to be converted.converter
- Converter function.- Returns:
- Converted value, or null if 'value' was null.
-
ifNotNullDo
Executes a consumer function on given value, unless value is null- Type Parameters:
T
- Type of value parameter.- Parameters:
value
- Value to be evaluated.consumer
- Consumer function to give value to
-
ifNotNull
Applies a conversion to 'value', or returns 'nullValue' if 'value' is null.- Type Parameters:
T
- Type of value parameter.V
- Type of return value.- Parameters:
value
- Value to be converted.converter
- Converter function.nullValue
- Value to return if 'value' is null.- Returns:
- Converted value, or 'nullValue' if 'value' was null.
-