Class OpenObject

java.lang.Object
cloud.opencode.base.core.OpenObject

public final class OpenObject extends Object
Object Utility Class - Comprehensive object operations 对象工具类 - 全面的对象操作支持

Provides null-safe operations, default value handling, comparison, cloning and type checking.

提供空值安全操作、默认值处理、比较、克隆和类型检查功能。

Features | 主要功能:

  • Null checking (isNull, isEmpty, isAnyNull) - 空值检查
  • Default value handling (defaultIfNull, firstNonNull) - 默认值处理
  • Null-safe property access (nullSafeGet) - 空值安全属性访问
  • Object comparison (equals, deepEquals, compare) - 对象比较
  • Clone and serialization - 克隆和序列化

Usage Examples | 使用示例:

// Null checking | 空值检查
boolean isNull = OpenObject.isNull(obj);
boolean isEmpty = OpenObject.isEmpty(obj);

// Default value | 默认值
String name = OpenObject.defaultIfNull(userName, "Guest");
String first = OpenObject.firstNonNull(a, b, c);

// Null-safe access | 安全获取
String city = OpenObject.nullSafeGet(user, u -> u.getAddress().getCity(), "Unknown");

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    clone(T obj)
    Clones the object if it implements Cloneable, otherwise returns null.
    static <T> T
    Clones the object if possible, otherwise returns the original.
    static <T extends Comparable<? super T>>
    int
    compare(T c1, T c2)
    Compares two Comparable objects, with null treated as less than non-null.
    static <T extends Comparable<? super T>>
    int
    compare(T c1, T c2, boolean nullGreater)
    Compares two Comparable objects with configurable null ordering.
    static boolean
    deepEquals(Object obj1, Object obj2)
    Performs a deep equality comparison of two objects.
    static <T> T
    defaultIfEmpty(T object, T defaultValue)
    Returns the object if non-empty, otherwise returns the default value.
    static <T> T
    defaultIfNull(T object, Supplier<T> defaultSupplier)
    Returns the object if non-null, otherwise invokes the supplier for the default value.
    static <T> T
    defaultIfNull(T object, T defaultValue)
    Returns the object if non-null, otherwise returns the default value.
    static <T> T
    deserialize(byte[] bytes)
     
    static boolean
    equals(Object obj1, Object obj2)
    Null-safe equality check. null 安全的对象比较
    static <T> T
    firstNonNull(Supplier<T> defaultSupplier, T... values)
    Returns the first non-null value, falling back to the supplier if all are null.
    static <T> T
    firstNonNull(T... values)
    Returns the first non-null value from the array, or null if all are null.
    static Class<?>
    Returns the runtime type of the object, or null if the object is null.
    static int
    hashCode(Object... values)
    Computes the hash code for the given values.
    static int
    Returns the identity hash code of the object.
    static boolean
    isAllEmpty(Object... values)
    Returns true if all of the values are empty.
    static boolean
    isAllNull(Object... values)
    Returns true if all of the values are null.
    static boolean
    isAnyEmpty(Object... values)
    Returns true if any of the values is empty.
    static boolean
    isAnyNull(Object... values)
    Returns true if any of the values is null.
    static boolean
    Returns true if the object is an array.
    static boolean
    Returns true if the object is a primitive type or its wrapper.
    static boolean
    Returns true if the object is considered empty.
    static boolean
    isInstance(Object obj, Class<?> clazz)
    Returns true if the object is an instance of the specified class.
    static boolean
    Returns true if the object is not considered empty.
    static boolean
    Returns true if the object is not null.
    static boolean
    Returns true if the object is null.
    static boolean
    Returns true if the object is a primitive array.
    static boolean
    Returns true if the class is a primitive type or its wrapper.
    static boolean
    isWrapperType(Class<?> clazz)
    Returns true if the class is a primitive wrapper type.
    static <T extends Comparable<? super T>>
    T
    max(T a, T b)
    Returns the larger of the two Comparable values.
    static <T extends Comparable<? super T>>
    T
    min(T a, T b)
    Returns the smaller of the two Comparable values.
    static boolean
    notEquals(Object obj1, Object obj2)
    Returns true if the two objects are not equal.
    static <T,R> R
    nullSafeGet(T obj, Function<T,R> getter)
    Null-safe property accessor: applies the getter and returns null if the object is null. null 安全的属性获取
    static <T,R> R
    nullSafeGet(T obj, Function<T,R> getter, R defaultValue)
    Null-safe property accessor with default value. null 安全的属性获取(带默认值)
    static <T,R> Optional<R>
    nullSafeGetOptional(T obj, Function<T,R> getter)
    Null-safe property accessor returning an Optional.
    static <T> T
    requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
    Returns the object if non-null, otherwise invokes the supplier (JDK 9+ style).
    static byte[]
    Serializes the object to a byte array.
    static String
    Returns a debug string with the object's class name, identity hash and value.
    static <T> Optional<T>
    toOptional(T obj)
    Wraps the object in an Optional.
    static String
    Returns a string representation of the object, or "null" if the object is null.
    static String
    toString(Object obj, String nullDefault)
    Returns a string representation of the object, or the specified default if null.

    Methods inherited from class Object

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

    • isNull

      public static boolean isNull(Object obj)
      Returns true if the object is null. 检查对象是否为 null
      Parameters:
      obj - the object | 对象
      Returns:
      true if null | 如果为 null 返回 true
    • isNotNull

      public static boolean isNotNull(Object obj)
      Returns true if the object is not null. 检查对象是否非 null
      Parameters:
      obj - the object | 对象
      Returns:
      true if not null | 如果非 null 返回 true
    • isEmpty

      public static boolean isEmpty(Object obj)
      Returns true if the object is considered empty. 检查对象是否为空

      以下情况视为空:

      • null
      • 空字符串 ""
      • 空数组 (length == 0)
      • 空集合 (isEmpty())
      • 空 Map (isEmpty())
      • 空 Optional (isEmpty())
      Parameters:
      obj - the object | 对象
      Returns:
      true if empty | 如果为空返回 true
    • isNotEmpty

      public static boolean isNotEmpty(Object obj)
      Returns true if the object is not considered empty. 检查对象是否非空
      Parameters:
      obj - the object | 对象
      Returns:
      true if not empty | 如果非空返回 true
    • isAnyNull

      public static boolean isAnyNull(Object... values)
      Returns true if any of the values is null. 检查是否任意一个为 null
      Parameters:
      values - array of objects | 对象数组
      Returns:
      true if any value is null | 如果任意一个为 null 返回 true
    • isAllNull

      public static boolean isAllNull(Object... values)
      Returns true if all of the values are null. 检查是否全部为 null
      Parameters:
      values - array of objects | 对象数组
      Returns:
      true if all values are null | 如果全部为 null 返回 true
    • isAnyEmpty

      public static boolean isAnyEmpty(Object... values)
      Returns true if any of the values is empty. 检查是否任意一个为空
      Parameters:
      values - array of objects | 对象数组
      Returns:
      true if any value is empty | 如果任意一个为空返回 true
    • isAllEmpty

      public static boolean isAllEmpty(Object... values)
      Returns true if all of the values are empty. 检查是否全部为空
      Parameters:
      values - array of objects | 对象数组
      Returns:
      true if all values are empty | 如果全部为空返回 true
    • defaultIfNull

      public static <T> T defaultIfNull(T object, T defaultValue)
      Returns the object if non-null, otherwise returns the default value. 如果对象为 null,返回默认值
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      object - the object | 对象
      defaultValue - default value | 默认值
      Returns:
      object or default value | 对象本身或默认值
    • defaultIfNull

      public static <T> T defaultIfNull(T object, Supplier<T> defaultSupplier)
      Returns the object if non-null, otherwise invokes the supplier for the default value. 如果对象为 null,使用 Supplier 获取默认值
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      object - the object | 对象
      defaultSupplier - default value supplier | 默认值提供者
      Returns:
      object or default value | 对象本身或默认值
    • defaultIfEmpty

      public static <T> T defaultIfEmpty(T object, T defaultValue)
      Returns the object if non-empty, otherwise returns the default value. 如果对象为空,返回默认值
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      object - the object | 对象
      defaultValue - default value | 默认值
      Returns:
      object or default value | 对象本身或默认值
    • firstNonNull

      @SafeVarargs public static <T> T firstNonNull(T... values)
      Returns the first non-null value from the array, or null if all are null. 返回第一个非 null 值
      Type Parameters:
      T - value type | 值类型
      Parameters:
      values - value array | 值数组
      Returns:
      first non-null value, or null if all null | 第一个非 null 值,如果全部为 null 则返回 null
    • firstNonNull

      @SafeVarargs public static <T> T firstNonNull(Supplier<T> defaultSupplier, T... values)
      Returns the first non-null value, falling back to the supplier if all are null. 返回第一个非 null 值,全部为 null 时使用 Supplier
      Type Parameters:
      T - value type | 值类型
      Parameters:
      defaultSupplier - default value supplier | 默认值提供者
      values - value array | 值数组
      Returns:
      first non-null value or default | 第一个非 null 值或默认值
    • requireNonNullElseGet

      public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
      Returns the object if non-null, otherwise invokes the supplier (JDK 9+ style). 对象非 null 则返回,否则使用 Supplier(JDK 9+ 风格)
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      obj - the object | 对象
      supplier - default value supplier | 默认值提供者
      Returns:
      object or default value | 对象本身或默认值
    • nullSafeGet

      public static <T,R> R nullSafeGet(T obj, Function<T,R> getter)
      Null-safe property accessor: applies the getter and returns null if the object is null. null 安全的属性获取
      Type Parameters:
      T - object type | 对象类型
      R - return type | 返回值类型
      Parameters:
      obj - the object | 对象
      getter - property getter function | 属性获取函数
      Returns:
      property value, or null if the object is null or NullPointerException occurs | 属性值,如果对象为 null 或获取过程中出现 null 则返回 null
    • nullSafeGet

      public static <T,R> R nullSafeGet(T obj, Function<T,R> getter, R defaultValue)
      Null-safe property accessor with default value. null 安全的属性获取(带默认值)
      Type Parameters:
      T - object type | 对象类型
      R - return type | 返回值类型
      Parameters:
      obj - the object | 对象
      getter - property getter function | 属性获取函数
      defaultValue - default value | 默认值
      Returns:
      property value or default | 属性值或默认值
    • nullSafeGetOptional

      public static <T,R> Optional<R> nullSafeGetOptional(T obj, Function<T,R> getter)
      Null-safe property accessor returning an Optional. 链式 null 安全获取(返回 Optional)
      Type Parameters:
      T - object type | 对象类型
      R - return type | 返回值类型
      Parameters:
      obj - the object | 对象
      getter - property getter function | 属性获取函数
      Returns:
      Optional wrapping the result | Optional 包装的结果
    • equals

      public static boolean equals(Object obj1, Object obj2)
      Null-safe equality check. null 安全的对象比较
      Parameters:
      obj1 - first object | 对象1
      obj2 - second object | 对象2
      Returns:
      true if equal | 如果相等返回 true
    • notEquals

      public static boolean notEquals(Object obj1, Object obj2)
      Returns true if the two objects are not equal. 检查两个对象是否不相等
      Parameters:
      obj1 - first object | 对象1
      obj2 - second object | 对象2
      Returns:
      true if not equal | 如果不相等返回 true
    • deepEquals

      public static boolean deepEquals(Object obj1, Object obj2)
      Performs a deep equality comparison of two objects. 深度比较两个对象
      Parameters:
      obj1 - first object | 对象1
      obj2 - second object | 对象2
      Returns:
      true if deeply equal | 如果深度相等返回 true
    • compare

      public static <T extends Comparable<? super T>> int compare(T c1, T c2)
      Compares two Comparable objects, with null treated as less than non-null. 比较两个 Comparable 对象
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      c1 - first object | 对象1
      c2 - second object | 对象2
      Returns:
      comparison result | 比较结果
    • compare

      public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater)
      Compares two Comparable objects with configurable null ordering. 比较两个 Comparable 对象(可指定 null 排序位置)
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      c1 - first object | 对象1
      c2 - second object | 对象2
      nullGreater - whether null sorts after non-null values | null 是否排在后面
      Returns:
      comparison result | 比较结果
    • max

      public static <T extends Comparable<? super T>> T max(T a, T b)
      Returns the larger of the two Comparable values. 返回较大值
      Type Parameters:
      T - value type | 值类型
      Parameters:
      a - first value | 值1
      b - second value | 值2
      Returns:
      the larger value | 较大值
    • min

      public static <T extends Comparable<? super T>> T min(T a, T b)
      Returns the smaller of the two Comparable values. 返回较小值
      Type Parameters:
      T - value type | 值类型
      Parameters:
      a - first value | 值1
      b - second value | 值2
      Returns:
      the smaller value | 较小值
    • isBasicType

      public static boolean isBasicType(Object obj)
      Returns true if the object is a primitive type or its wrapper. 检查是否为基本类型或其包装类型
      Parameters:
      obj - the object | 对象
      Returns:
      true if primitive or wrapper | 如果是基本类型或包装类型返回 true
    • isArray

      public static boolean isArray(Object obj)
      Returns true if the object is an array. 检查是否为数组
      Parameters:
      obj - the object | 对象
      Returns:
      true if it is an array | 如果是数组返回 true
    • isPrimitiveArray

      public static boolean isPrimitiveArray(Object obj)
      Returns true if the object is a primitive array. 检查是否为原始类型数组
      Parameters:
      obj - the object | 对象
      Returns:
      true if it is a primitive array | 如果是原始类型数组返回 true
    • isInstance

      public static boolean isInstance(Object obj, Class<?> clazz)
      Returns true if the object is an instance of the specified class. 检查对象是否为指定类型的实例
      Parameters:
      obj - the object | 对象
      clazz - the class | 类型
      Returns:
      true if it is an instance | 如果是实例返回 true
    • getType

      public static Class<?> getType(Object obj)
      Returns the runtime type of the object, or null if the object is null. 获取对象的类型
      Parameters:
      obj - the object | 对象
      Returns:
      the class, or null if object is null | 类型,如果对象为 null 返回 null
    • isWrapperType

      public static boolean isWrapperType(Class<?> clazz)
      Returns true if the class is a primitive wrapper type. 检查是否为包装类型
      Parameters:
      clazz - the class | 类型
      Returns:
      true if it is a wrapper type | 如果是包装类型返回 true
    • isPrimitiveOrWrapper

      public static boolean isPrimitiveOrWrapper(Class<?> clazz)
      Returns true if the class is a primitive type or its wrapper. 检查是否为原始类型或包装类型
      Parameters:
      clazz - the class | 类型
      Returns:
      true if primitive or wrapper | 如果是原始类型或包装类型返回 true
    • hashCode

      public static int hashCode(Object... values)
      Computes the hash code for the given values. 计算对象的哈希值
      Parameters:
      values - objects to hash | 对象数组
      Returns:
      hash code | 哈希值
    • identityHashCode

      public static int identityHashCode(Object obj)
      Returns the identity hash code of the object. 获取对象的身份哈希值
      Parameters:
      obj - the object | 对象
      Returns:
      identity hash code | 身份哈希值
    • clone

      public static <T> T clone(T obj)
      Clones the object if it implements Cloneable, otherwise returns null. 克隆对象

      如果对象实现了 Cloneable 接口,则调用 clone 方法。

      Type Parameters:
      T - object type | 对象类型
      Parameters:
      obj - the object | 对象
      Returns:
      cloned object, or null if unable to clone | 克隆后的对象,如果无法克隆返回 null
    • cloneIfPossible

      public static <T> T cloneIfPossible(T obj)
      Clones the object if possible, otherwise returns the original. 克隆对象,如果无法克隆则返回原对象
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      obj - the object | 对象
      Returns:
      cloned object or original | 克隆后的对象或原对象
    • serialize

      public static byte[] serialize(Serializable obj)
      Serializes the object to a byte array. 序列化对象为字节数组
      Parameters:
      obj - serializable object | 可序列化对象
      Returns:
      byte array | 字节数组
      Throws:
      IllegalStateException - if serialization fails | 如果序列化失败
    • deserialize

      public static <T> T deserialize(byte[] bytes)
    • toString

      public static String toString(Object obj)
      Returns a string representation of the object, or "null" if the object is null. 对象转字符串
      Parameters:
      obj - the object | 对象
      Returns:
      string representation | 字符串表示
    • toString

      public static String toString(Object obj, String nullDefault)
      Returns a string representation of the object, or the specified default if null. 对象转字符串(可指定 null 默认值)
      Parameters:
      obj - the object | 对象
      nullDefault - value to return when object is null | null 时的默认值
      Returns:
      string representation | 字符串表示
    • toDebugString

      public static String toDebugString(Object obj)
      Returns a debug string with the object's class name, identity hash and value. 转为调试字符串(包含类型信息)
      Parameters:
      obj - the object | 对象
      Returns:
      debug string | 调试字符串
    • toOptional

      public static <T> Optional<T> toOptional(T obj)
      Wraps the object in an Optional. 将对象包装为 Optional
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      obj - the object | 对象
      Returns:
      Optional wrapping | Optional 包装