Class ObjectUtils

java.lang.Object
org.apache.juneau.internal.ObjectUtils

public class ObjectUtils extends Object
Various generic object utility methods.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    cast(Class<T> c, Object o)
    Casts an object to a specific type if it's an instance of that type.
    static <T> T
    If the specified object is an instance of the specified class, casts it to that type.
    static final int
    compare(int i1, int i2)
    Compare two integers numerically.
    static int
    Compares two objects for equality.
    static <T> boolean
    eq(T[] o1, T[] o2)
    Tests two arrays for equality, gracefully handling nulls.
    static <T> boolean
    eq(T o1, T o2)
    Tests two objects for equality, gracefully handling nulls and arrays.
    static <T, U> boolean
    eq(T o1, U o2, BiPredicate<T,U> test)
    Tests two objects for equality, gracefully handling nulls.
    static <T> T
    firstNonNull(T... t)
    Returns the first non-null value in the specified array
    static String
    Converts the specified object into an identifiable string of the form "Class[identityHashCode]"
    static boolean
    Returns true if the specified object is empty.
    static boolean
    Returns true if the specified object is not null and not empty.
    static <T extends Number>
    boolean
    isNotMinusOne(T value)
    Returns true if the specified number is not null and not -1.
    static <T> boolean
    isNotNull(T value)
    Returns true if the specified object is not null.
    static boolean
    isTrue(Boolean value)
    Returns true if the specified boolean is not null and is true.
    static boolean
    ne(Object o1, Object o2)
    Tests two objects for equality, gracefully handling nulls and arrays.
    static <T, U> boolean
    ne(T o1, U o2, BiPredicate<T,U> test)
    Tests two objects for inequality, gracefully handling nulls.
    static JsonMap
    Searches for all properties() methods on the specified object and creates a combine map of them.
    static Object
    If the specified object is a Supplier or Value, returns the inner value, otherwise the same value.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • castOrNull

      public static <T> T castOrNull(Object o, Class<T> c)
      If the specified object is an instance of the specified class, casts it to that type.
      Type Parameters:
      T - The class to cast to.
      Parameters:
      o - The object to cast.
      c - The class to cast to.
      Returns:
      The cast object, or null if the object wasn't an instance of the specified class.
    • compare

      public static int compare(Object o1, Object o2)
      Compares two objects for equality.

      Nulls are always considered less-than unless both are null.

      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      Returns:
      -1, 0, or 1 if o1 is less-than, equal, or greater-than o2.
      0 if objects are not of the same type or do not implement the Comparable interface.
    • compare

      public static final int compare(int i1, int i2)
      Compare two integers numerically.
      Parameters:
      i1 - Integer #1
      i2 - Integer #2
      Returns:
      The value 0 if Integer #1 is equal to Integer #2; a value less than 0 if Integer #1 numerically less than Integer #2; and a value greater than 0 if Integer #1 is numerically greater than Integer #2 (signed comparison).
    • eq

      public static <T, U> boolean eq(T o1, U o2, BiPredicate<T,U> test)
      Tests two objects for equality, gracefully handling nulls.
      Type Parameters:
      T - Object 1 type.
      U - Object 2 type.
      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      test - The test to use for equality.
      Returns:
      true if both objects are equal based on the test.
    • eq

      public static <T> boolean eq(T o1, T o2)
      Tests two objects for equality, gracefully handling nulls and arrays.
      Type Parameters:
      T - The value types.
      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      Returns:
      true if both objects are equal based on the Object.equals(Object) method.
    • eq

      public static <T> boolean eq(T[] o1, T[] o2)
      Tests two arrays for equality, gracefully handling nulls.
      Type Parameters:
      T - The value types.
      Parameters:
      o1 - Array 1.
      o2 - Array 2.
      Returns:
      true if both arrays are equal based on the Object.equals(Object) method on each element.
    • ne

      public static <T, U> boolean ne(T o1, U o2, BiPredicate<T,U> test)
      Tests two objects for inequality, gracefully handling nulls.
      Type Parameters:
      T - Object 1 type.
      U - Object 2 type.
      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      test - The test to use for equality.
      Returns:
      false if both objects are equal based on the test.
    • ne

      public static boolean ne(Object o1, Object o2)
      Tests two objects for equality, gracefully handling nulls and arrays.
      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      Returns:
      false if both objects are equal based on the Object.equals(Object) method.
    • unwrap

      public static Object unwrap(Object o)
      If the specified object is a Supplier or Value, returns the inner value, otherwise the same value.
      Parameters:
      o - The object to unwrap.
      Returns:
      The unwrapped object.
    • isEmpty

      public static boolean isEmpty(Object o)
      Returns true if the specified object is empty.

      Return true if the value is any of the following:

      • null
      • An empty Collection
      • An empty Map
      • An empty array
      • An empty CharSequence
      • An empty String when serialized to a string using Object.toString().
      Parameters:
      o - The object to test.
      Returns:
      true if the specified object is empty.
    • firstNonNull

      @SafeVarargs public static <T> T firstNonNull(T... t)
      Returns the first non-null value in the specified array
      Type Parameters:
      T - The value types.
      Parameters:
      t - The values to check.
      Returns:
      The first non-null value, or null if the array is null or empty or contains only null values.
    • cast

      public static <T> T cast(Class<T> c, Object o)
      Casts an object to a specific type if it's an instance of that type.
      Type Parameters:
      T - The type to cast to.
      Parameters:
      c - The type to cast to.
      o - The object to cast to.
      Returns:
      The cast object, or null if the object wasn't the specified type.
    • identity

      public static String identity(Object o)
      Converts the specified object into an identifiable string of the form "Class[identityHashCode]"
      Parameters:
      o - The object to convert to a string.
      Returns:
      An identity string.
    • toPropertyMap

      public static JsonMap toPropertyMap(Object o)
      Searches for all properties() methods on the specified object and creates a combine map of them.
      Parameters:
      o - The object to return a property map of.
      Returns:
      A new property map.
    • isNotNull

      public static <T> boolean isNotNull(T value)
      Returns true if the specified object is not null.
      Type Parameters:
      T - The value type.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified object is not null.
    • isTrue

      public static boolean isTrue(Boolean value)
      Returns true if the specified boolean is not null and is true.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified boolean is not null and is true.
    • isNotMinusOne

      public static <T extends Number> boolean isNotMinusOne(T value)
      Returns true if the specified number is not null and not -1.
      Type Parameters:
      T - The value types.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified number is not null and not -1.
    • isNotEmpty

      public static boolean isNotEmpty(Object value)
      Returns true if the specified object is not null and not empty. Works on any of the following data types: String, CharSequence, Collection, Map, array. All other types are stringified and then checked as a String.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified object is not null and not empty.