Class ObjectUtils

java.lang.Object
org.apache.juneau.commons.utils.ObjectUtils

public class ObjectUtils extends Object
Canonical home for object-level helpers: null/empty checks, equality, comparison, coalescing, boolean folds, identity/stringify, small numeric helpers, and optionals.

Terse aliases for these methods live in Shorts.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor — this class is meant to be subclassed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Number>
    T
    abs(T value)
    Absolute value across Number subtypes.
    static boolean
    allTrue(boolean... values)
    Boolean AND-fold (vacuously true when empty).
    static boolean
    anyTrue(boolean... values)
    Boolean OR-fold (false when empty).
    static <T> T
    coalesce(T... t)
    Returns the first non-null value, or null.
    static <T> T
    coalesce(T value, T defaultValue)
    Returns the value if non-null, else the default (coalesce).
    static int
    Null-tolerant natural-order compare (0 if types differ / not Comparable).
    static <T> Optional<T>
    Optional.empty() shorthand.
    static boolean
    equal(boolean caseInsensitive, String s1, String s2)
    String equality with optional case-insensitivity (null-safe; two null values are equal).
    static <T> boolean
    equal(T o1, T o2)
    Null/array/annotation-aware equality.
    static <T, U> boolean
    equal(T o1, U o2, BiPredicate<T,U> test)
    Null-safe equality via a custom predicate.
    static boolean
    Case-insensitive equality of two objects' string forms (null-safe; two null values are equal, one null is not).
    static <T> boolean
    equalsAny(T o1, T... o2)
    Returns true if the first argument equals any of the varargs.
    static <T extends Comparable<T>>
    boolean
    greaterThan(T o1, T o2)
    Returns true if o1 is greater than o2.
    static <T extends Comparable<T>>
    boolean
    greaterThanOrEqual(T o1, T o2)
    Returns true if o1 is greater than or equal to o2.
    static int
    hash(Object... values)
    Content/annotation/array-aware hash.
    static String
    "SimpleQualifiedName@identityHashCode" for the object (unwrapping Optionals).
    static boolean
    isBetween(int n, int lower, int higher)
    Inclusive between.
    static boolean
    True if the object (String/Collection/Map/array/other) is null or empty.
    static boolean
    isFalse(Object value)
    Null-safe Boolean.FALSE check.
    static boolean
    Inverse of isEmpty(Object).
    static <T extends Number>
    boolean
    isNotMinusOne(T value)
    True if the number is non-null and not -1.
    static boolean
    Returns true if the object is not null.
    static boolean
    isNull(Object... values)
    Returns true if all specified values are null (or the array is null).
    static <T> boolean
    isNull(T value)
    Returns true if the object is null.
    static boolean
    isTrue(Object value)
    Null-safe Boolean.TRUE check.
    static <T extends Comparable<T>>
    boolean
    lessThan(T o1, T o2)
    Returns true if o1 is less than o2.
    static <T extends Comparable<T>>
    boolean
    lessThanOrEqual(T o1, T o2)
    Returns true if o1 is less than or equal to o2.
    static <T, R> R
    mapOrNull(T value, Function<? super T,? extends R> mapper)
    Returns null when the value is null, else the mapper's result; the mapper is never called on null.
    static <T extends Comparable<T>>
    T
    max(T o1, T o2)
    Returns the maximum of two comparable values.
    static <T extends Comparable<T>>
    T
    min(T o1, T o2)
    Returns the minimum of two comparable values.
    static boolean
    negate(boolean value)
    Boolean negation.
    static <T> boolean
    notEqual(T o1, T o2)
    static <T, U> boolean
    notEqual(T o1, U o2, BiPredicate<T,U> test)
    static <T> T
    nullObject(Class<T> type)
    Returns null typed as the specified class (type-inference helper).
    static <T> Optional<T>
    optional(T t)
    static <T> T
    orElse(Optional<T> optional, T defaultValue)
    optional.orElse(defaultValue) shorthand.
    static <T> T
    requireNonNull(T value)
    Asserts non-null and returns the value.
    static int
    Null-safe size (0 for null; length/size for String/Collection/Map/array; else 1).
    static String
    Null-safe toString (returns null for null).
    static boolean
    Coerces an object to a boolean (false if null).
    static Object
    Unwraps a Supplier/Holder/Optional to its inner value.

    Methods inherited from class java.lang.Object

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

    • ObjectUtils

      protected ObjectUtils()
      Constructor — this class is meant to be subclassed.
  • Method Details

    • isNotNull

      public static boolean isNotNull(Object o)
      Returns true if the object is not null.
    • isNull

      public static <T> boolean isNull(T value)
      Returns true if the object is null.
    • isNull

      public static boolean isNull(Object... values)
      Returns true if all specified values are null (or the array is null).
    • nullObject

      public static <T> T nullObject(Class<T> type)
      Returns null typed as the specified class (type-inference helper).
    • equal

      public static <T> boolean equal(T o1, T o2)
      Null/array/annotation-aware equality.
    • equal

      public static boolean equal(boolean caseInsensitive, String s1, String s2)
      String equality with optional case-insensitivity (null-safe; two null values are equal).
    • equal

      public static <T, U> boolean equal(T o1, U o2, BiPredicate<T,U> test)
      Null-safe equality via a custom predicate.
    • equalsAny

      @SafeVarargs public static <T> boolean equalsAny(T o1, T... o2)
      Returns true if the first argument equals any of the varargs.
      Returns false if the varargs array is null or empty.
    • equalIgnoreCase

      public static boolean equalIgnoreCase(Object o1, Object o2)
      Case-insensitive equality of two objects' string forms (null-safe; two null values are equal, one null is not).
    • notEqual

      public static <T> boolean notEqual(T o1, T o2)
    • notEqual

      public static <T, U> boolean notEqual(T o1, U o2, BiPredicate<T,U> test)
    • compare

      public static int compare(Object o1, Object o2)
      Null-tolerant natural-order compare (0 if types differ / not Comparable).
    • lessThan

      public static <T extends Comparable<T>> boolean lessThan(T o1, T o2)
      Returns true if o1 is less than o2.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Object 1. Can be null.
      o2 - Object 2. Can be null.
      Returns:
      true if o1 is less than o2. A null value is considered less than any non-null value.
    • lessThanOrEqual

      public static <T extends Comparable<T>> boolean lessThanOrEqual(T o1, T o2)
      Returns true if o1 is less than or equal to o2.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Object 1. Can be null.
      o2 - Object 2. Can be null.
      Returns:
      true if o1 is less than or equal to o2. A null o1 is considered less than or equal to any value.
    • greaterThan

      public static <T extends Comparable<T>> boolean greaterThan(T o1, T o2)
      Returns true if o1 is greater than o2.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Object 1. Can be null.
      o2 - Object 2. Can be null.
      Returns:
      true if o1 is greater than o2. A null o1 is never considered greater than o2; a non-null o1 is considered greater than a null o2.
    • greaterThanOrEqual

      public static <T extends Comparable<T>> boolean greaterThanOrEqual(T o1, T o2)
      Returns true if o1 is greater than or equal to o2.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Object 1. Can be null.
      o2 - Object 2. Can be null.
      Returns:
      true if o1 is greater than or equal to o2. A non-null o1 is considered greater than or equal to a null o2; a null o1 is greater than or equal only to a null o2.
    • min

      public static <T extends Comparable<T>> T min(T o1, T o2)
      Returns the minimum of two comparable values.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Value 1. Can be null.
      o2 - Value 2. Can be null.
      Returns:
      The minimum value. If one argument is null, the other is returned (which may itself be null if both are null).
    • max

      public static <T extends Comparable<T>> T max(T o1, T o2)
      Returns the maximum of two comparable values.
      Type Parameters:
      T - The comparable type.
      Parameters:
      o1 - Value 1. Can be null.
      o2 - Value 2. Can be null.
      Returns:
      The maximum value. If one argument is null, the other is returned (which may itself be null if both are null).
    • coalesce

      public static <T> T coalesce(T value, T defaultValue)
      Returns the value if non-null, else the default (coalesce).
    • coalesce

      @SafeVarargs public static <T> T coalesce(T... t)
      Returns the first non-null value, or null.
    • mapOrNull

      public static <T, R> R mapOrNull(T value, Function<? super T,? extends R> mapper)
      Returns null when the value is null, else the mapper's result; the mapper is never called on null.
      Type Parameters:
      T - The value type.
      R - The result type.
      Parameters:
      value - The value to map. Can be null.
      mapper - The mapping function.
      Returns:
      The mapped value, or null if value is null or the mapper returns null.
    • allTrue

      public static boolean allTrue(boolean... values)
      Boolean AND-fold (vacuously true when empty).
    • anyTrue

      public static boolean anyTrue(boolean... values)
      Boolean OR-fold (false when empty).
    • negate

      public static boolean negate(boolean value)
      Boolean negation.
    • isTrue

      public static boolean isTrue(Object value)
      Null-safe Boolean.TRUE check.
    • isFalse

      public static boolean isFalse(Object value)
      Null-safe Boolean.FALSE check.
    • toBoolean

      public static boolean toBoolean(Object val)
      Coerces an object to a boolean (false if null).
    • stringify

      public static String stringify(Object val)
      Null-safe toString (returns null for null).
    • identity

      public static String identity(Object o)
      "SimpleQualifiedName@identityHashCode" for the object (unwrapping Optionals).
      Returns null if the argument is null (or an empty Optional).
    • unwrap

      public static Object unwrap(Object o)
      Unwraps a Supplier/Holder/Optional to its inner value.
      Returns null if the argument is null or unwraps to null.
    • abs

      public static <T extends Number> T abs(T value)
      Absolute value across Number subtypes.
      Returns null if the argument is null.
    • isBetween

      public static boolean isBetween(int n, int lower, int higher)
      Inclusive between.
    • isNotMinusOne

      public static <T extends Number> boolean isNotMinusOne(T value)
      True if the number is non-null and not -1.
    • hash

      public static int hash(Object... values)
      Content/annotation/array-aware hash.
      Throws IllegalArgumentException if the array is null.
    • isEmpty

      public static boolean isEmpty(Object o)
      True if the object (String/Collection/Map/array/other) is null or empty.
    • isNotEmpty

      public static boolean isNotEmpty(Object value)
      Inverse of isEmpty(Object).
    • optional

      public static <T> Optional<T> optional(T t)
    • emptyOptional

      public static <T> Optional<T> emptyOptional()
      Optional.empty() shorthand.
    • orElse

      public static <T> T orElse(Optional<T> optional, T defaultValue)
      optional.orElse(defaultValue) shorthand.
    • requireNonNull

      public static <T> T requireNonNull(T value)
      Asserts non-null and returns the value.
    • size

      public static int size(Object o)
      Null-safe size (0 for null; length/size for String/Collection/Map/array; else 1).