Package org.axonframework.common
Class ObjectUtils
java.lang.Object
org.axonframework.common.ObjectUtils
Miscellaneous object utility methods.
- Since:
- 3.0
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends CharSequence>
TgetNonEmptyOrDefault(T instance, T defaultValue) Returns the given instance, if notnullor of zero length, or otherwise the givendefaultValue.static <I,T> T getOrDefault(I instance, Function<I, T> valueProvider, T defaultValue) Returns the result of the givenvalueProviderby ingesting the giveninstance, if theinstanceis notnull.static <T> TgetOrDefault(T instance, Supplier<T> defaultProvider) Returns the given instance, if notnull, or otherwise the value provided bydefaultProvider.static <T> TgetOrDefault(T instance, T defaultValue) Returns the given instance, if notnull, or otherwise the givendefaultValue.static longgetRemainingOfDeadline(long deadline) Gets number of millis which are remaining of current deadline to be reached bySystem.currentTimeMillis().static <T> Class<T> nullSafeTypeOf(T instance) Returns the type of the giveninstance, if it is notnull.static <T> Supplier<T> sameInstanceSupplier(Supplier<T> supplier) Wraps the givensupplierto ensure that the same instance is returned on multiple consecutive invocations.
-
Method Details
-
getOrDefault
Returns the given instance, if notnull, or otherwise the value provided bydefaultProvider.- Type Parameters:
T- the type of value to return- Parameters:
instance- the value to return, if notnulldefaultProvider- to provide the value, wheninstanceisnull- Returns:
instanceif notnull, otherwise the value provided bydefaultProvider
-
getOrDefault
public static <T> T getOrDefault(T instance, T defaultValue) Returns the given instance, if notnull, or otherwise the givendefaultValue.- Type Parameters:
T- the type of value to return- Parameters:
instance- the value to return, if notnulldefaultValue- the value, wheninstanceisnull- Returns:
instanceif notnull, otherwisedefaultValue
-
getNonEmptyOrDefault
Returns the given instance, if notnullor of zero length, or otherwise the givendefaultValue.- Type Parameters:
T- the type of value to return- Parameters:
instance- the value to return, if notnulldefaultValue- the value, wheninstanceisnull- Returns:
instanceif notnull, otherwisedefaultValue
-
getOrDefault
Returns the result of the givenvalueProviderby ingesting the giveninstance, if theinstanceis notnull. If it is, thedefaultValueis returned.- Type Parameters:
I- the type of theinstanceto verify and use by thevalueProviderT- the type of value to return- Parameters:
instance- the value to verify if it is notnull. If it isn't, the givenvalueProviderwill be invoked with this objectvalueProvider- the function to return the result of by ingesting theinstanceif it is not nulldefaultValue- the value to return if the giveninstanceisnull- Returns:
- the output of
valueProviderby ingestinginstanceif it is notnull, otherwise thedefaultValue
-
nullSafeTypeOf
Returns the type of the giveninstance, if it is notnull. If it isnull,Object.getClass()will be returned.- Type Parameters:
T- the generic type of theClassto return- Parameters:
instance- the object to return the type for- Returns:
- the type of the given
instanceif it is notnull, otherwiseObject.getClass()
-
getRemainingOfDeadline
public static long getRemainingOfDeadline(long deadline) Gets number of millis which are remaining of current deadline to be reached bySystem.currentTimeMillis(). If deadline is passed, 0 will be returned.- Parameters:
deadline- deadline to be met- Returns:
- number of millis to deadline
-
sameInstanceSupplier
Wraps the givensupplierto ensure that the same instance is returned on multiple consecutive invocations. While it guarantees that the same instance is returned, concurrent access may cause givensupplierto be invoked more than once.- Type Parameters:
T- The type of object supplied- Parameters:
supplier- The supplier to provide the instance to return- Returns:
- a supplier that returns the same instance
-