Class ObjectUtils

java.lang.Object
org.aksw.commons.util.obj.ObjectUtils

public class ObjectUtils extends Object
  • Constructor Details

    • ObjectUtils

      public ObjectUtils()
  • Method Details

    • getClass

      public static Class<?> getClass(Object object)
      Return an object's class - returns null for a null argument
    • toStringWithIdentityHashCode

      public static String toStringWithIdentityHashCode(Object obj)
      For a given object derive a string of the form className@identityHashCode
    • toStringWithIdentityHashCode

      public static String toStringWithIdentityHashCode(Object obj, String nullDefault)
      For a given object derive a string of the form className@identityHashCode
    • canCastAs

      public static boolean canCastAs(Class<?> clazz, Object o)
      Check if the given object can be assigned to given class. Also works for primitive types, e.g. int can be assigned to Long.
    • tryCastAs

      public static <T> Optional<T> tryCastAs(Class<T> clazz, Object o)
    • castAs

      public static <T> T castAs(Class<T> clazz, Object o)
    • castAsOrNull

      public static <T> T castAsOrNull(Class<T> clazz, Object o)
    • mergeNonNull

      public static <T> T mergeNonNull(T a, T b, BinaryOperator<T> merger)
      If both arguments are non-null then invoke the merger with them and return its result. If both arguments are null return null; Otherwise return the non-null argument.
    • mergeNonNull

      public static <T> T mergeNonNull(T a, T b, BinaryOperator<T> combiner, Supplier<T> nullCase)
      Similar to mergeNonNull(Object, Object, BinaryOperator) but with an additional supplier if both arguments are null.
    • coalesce

      @SafeVarargs public static <T> T coalesce(Supplier<T>... ts)
      Supplier-based coalesce function as described in https://benjiweber.co.uk/blog/2013/12/08/null-coalescing-in-java-8/
      Example usage: coalesce(obj::getName, obj::getId, () -> obj.hasFoo() ? obj.foo().toString() : "bar")
      Type Parameters:
      T -
      Parameters:
      ts -
      Returns:
    • requireNullOrEqual

      public static <T> T requireNullOrEqual(T a, T b)
      A consistency check utility. If both given values are non-null then they must be equal otherwise an exception is raised. Returns the first non-null value (if present) otherwise null.