Class ObjectUtils

java.lang.Object
org.axonframework.common.ObjectUtils

public final class ObjectUtils extends Object
Miscellaneous object utility methods.
Since:
3.0
Author:
Allard Buijze
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends CharSequence>
    T
    getNonEmptyOrDefault(T instance, T defaultValue)
    Returns the given instance, if not null or of zero length, or otherwise the given defaultValue.
    static <I, T> T
    getOrDefault(I instance, Function<I,T> valueProvider, T defaultValue)
    Returns the result of the given valueProvider by ingesting the given instance, if the instance is not null.
    static <T> T
    getOrDefault(T instance, Supplier<T> defaultProvider)
    Returns the given instance, if not null, or otherwise the value provided by defaultProvider.
    static <T> T
    getOrDefault(T instance, T defaultValue)
    Returns the given instance, if not null, or otherwise the given defaultValue.
    static long
    getRemainingOfDeadline(long deadline)
    Gets number of millis which are remaining of current deadline to be reached by System.currentTimeMillis().
    static <T> Class<T>
    nullSafeTypeOf(T instance)
    Returns the type of the given instance, if it is not null.
    static <T> Supplier<T>
    Wraps the given supplier to ensure that the same instance is returned on multiple consecutive invocations.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getOrDefault

      public static <T> T getOrDefault(T instance, Supplier<T> defaultProvider)
      Returns the given instance, if not null, or otherwise the value provided by defaultProvider.
      Type Parameters:
      T - the type of value to return
      Parameters:
      instance - the value to return, if not null
      defaultProvider - to provide the value, when instance is null
      Returns:
      instance if not null, otherwise the value provided by defaultProvider
    • getOrDefault

      public static <T> T getOrDefault(T instance, T defaultValue)
      Returns the given instance, if not null, or otherwise the given defaultValue.
      Type Parameters:
      T - the type of value to return
      Parameters:
      instance - the value to return, if not null
      defaultValue - the value, when instance is null
      Returns:
      instance if not null, otherwise defaultValue
    • getNonEmptyOrDefault

      public static <T extends CharSequence> T getNonEmptyOrDefault(T instance, T defaultValue)
      Returns the given instance, if not null or of zero length, or otherwise the given defaultValue.
      Type Parameters:
      T - the type of value to return
      Parameters:
      instance - the value to return, if not null
      defaultValue - the value, when instance is null
      Returns:
      instance if not null, otherwise defaultValue
    • getOrDefault

      public static <I, T> T getOrDefault(I instance, Function<I,T> valueProvider, T defaultValue)
      Returns the result of the given valueProvider by ingesting the given instance, if the instance is not null. If it is, the defaultValue is returned.
      Type Parameters:
      I - the type of the instance to verify and use by the valueProvider
      T - the type of value to return
      Parameters:
      instance - the value to verify if it is not null. If it isn't, the given valueProvider will be invoked with this object
      valueProvider - the function to return the result of by ingesting the instance if it is not null
      defaultValue - the value to return if the given instance is null
      Returns:
      the output of valueProvider by ingesting instance if it is not null, otherwise the defaultValue
    • nullSafeTypeOf

      public static <T> Class<T> nullSafeTypeOf(T instance)
      Returns the type of the given instance, if it is not null. If it is null, Object.getClass() will be returned.
      Type Parameters:
      T - the generic type of the Class to return
      Parameters:
      instance - the object to return the type for
      Returns:
      the type of the given instance if it is not null, otherwise Object.getClass()
    • getRemainingOfDeadline

      public static long getRemainingOfDeadline(long deadline)
      Gets number of millis which are remaining of current deadline to be reached by System.currentTimeMillis(). If deadline is passed, 0 will be returned.
      Parameters:
      deadline - deadline to be met
      Returns:
      number of millis to deadline
    • sameInstanceSupplier

      public static <T> Supplier<T> sameInstanceSupplier(Supplier<T> supplier)
      Wraps the given supplier to ensure that the same instance is returned on multiple consecutive invocations. While it guarantees that the same instance is returned, concurrent access may cause given supplier to 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