Class OpenObject
java.lang.Object
cloud.opencode.base.core.OpenObject
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 TypeMethodDescriptionstatic <T> Tclone(T obj) Clones the object if it implements Cloneable, otherwise returns null.static <T> TcloneIfPossible(T obj) Clones the object if possible, otherwise returns the original.static <T extends Comparable<? super T>>
intcompare(T c1, T c2) Compares two Comparable objects, with null treated as less than non-null.static <T extends Comparable<? super T>>
intcompare(T c1, T c2, boolean nullGreater) Compares two Comparable objects with configurable null ordering.static booleandeepEquals(Object obj1, Object obj2) Performs a deep equality comparison of two objects.static <T> TdefaultIfEmpty(T object, T defaultValue) Returns the object if non-empty, otherwise returns the default value.static <T> TdefaultIfNull(T object, Supplier<T> defaultSupplier) Returns the object if non-null, otherwise invokes the supplier for the default value.static <T> TdefaultIfNull(T object, T defaultValue) Returns the object if non-null, otherwise returns the default value.static <T> Tdeserialize(byte[] bytes) static booleanNull-safe equality check. null 安全的对象比较static <T> TfirstNonNull(Supplier<T> defaultSupplier, T... values) Returns the first non-null value, falling back to the supplier if all are null.static <T> TfirstNonNull(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 intComputes the hash code for the given values.static intidentityHashCode(Object obj) Returns the identity hash code of the object.static booleanisAllEmpty(Object... values) Returns true if all of the values are empty.static booleanReturns true if all of the values are null.static booleanisAnyEmpty(Object... values) Returns true if any of the values is empty.static booleanReturns true if any of the values is null.static booleanReturns true if the object is an array.static booleanisBasicType(Object obj) Returns true if the object is a primitive type or its wrapper.static booleanReturns true if the object is considered empty.static booleanisInstance(Object obj, Class<?> clazz) Returns true if the object is an instance of the specified class.static booleanisNotEmpty(Object obj) Returns true if the object is not considered empty.static booleanReturns true if the object is not null.static booleanReturns true if the object is null.static booleanisPrimitiveArray(Object obj) Returns true if the object is a primitive array.static booleanisPrimitiveOrWrapper(Class<?> clazz) Returns true if the class is a primitive type or its wrapper.static booleanisWrapperType(Class<?> clazz) Returns true if the class is a primitive wrapper type.static <T extends Comparable<? super T>>
Tmax(T a, T b) Returns the larger of the two Comparable values.static <T extends Comparable<? super T>>
Tmin(T a, T b) Returns the smaller of the two Comparable values.static booleanReturns 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> TrequireNonNullElseGet(T obj, Supplier<? extends T> supplier) Returns the object if non-null, otherwise invokes the supplier (JDK 9+ style).static byte[]serialize(Serializable obj) Serializes the object to a byte array.static StringtoDebugString(Object obj) 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 StringReturns a string representation of the object, or "null" if the object is null.static StringReturns a string representation of the object, or the specified default if null.
-
Method Details
-
isNull
Returns true if the object is null. 检查对象是否为 null- Parameters:
obj- the object | 对象- Returns:
- true if null | 如果为 null 返回 true
-
isNotNull
Returns true if the object is not null. 检查对象是否非 null- Parameters:
obj- the object | 对象- Returns:
- true if not null | 如果非 null 返回 true
-
isEmpty
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
Returns true if the object is not considered empty. 检查对象是否非空- Parameters:
obj- the object | 对象- Returns:
- true if not empty | 如果非空返回 true
-
isAnyNull
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
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
Returns true if any of the values is empty. 检查是否任意一个为空- Parameters:
values- array of objects | 对象数组- Returns:
- true if any value is empty | 如果任意一个为空返回 true
-
isAllEmpty
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
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
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
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
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
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
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
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
-
notEquals
-
deepEquals
-
compare
Compares two Comparable objects, with null treated as less than non-null. 比较两个 Comparable 对象- Type Parameters:
T- object type | 对象类型- Parameters:
c1- first object | 对象1c2- second object | 对象2- Returns:
- comparison result | 比较结果
-
compare
Compares two Comparable objects with configurable null ordering. 比较两个 Comparable 对象(可指定 null 排序位置)- Type Parameters:
T- object type | 对象类型- Parameters:
c1- first object | 对象1c2- second object | 对象2nullGreater- whether null sorts after non-null values | null 是否排在后面- Returns:
- comparison result | 比较结果
-
max
Returns the larger of the two Comparable values. 返回较大值- Type Parameters:
T- value type | 值类型- Parameters:
a- first value | 值1b- second value | 值2- Returns:
- the larger value | 较大值
-
min
Returns the smaller of the two Comparable values. 返回较小值- Type Parameters:
T- value type | 值类型- Parameters:
a- first value | 值1b- second value | 值2- Returns:
- the smaller value | 较小值
-
isBasicType
Returns true if the object is a primitive type or its wrapper. 检查是否为基本类型或其包装类型- Parameters:
obj- the object | 对象- Returns:
- true if primitive or wrapper | 如果是基本类型或包装类型返回 true
-
isArray
Returns true if the object is an array. 检查是否为数组- Parameters:
obj- the object | 对象- Returns:
- true if it is an array | 如果是数组返回 true
-
isPrimitiveArray
Returns true if the object is a primitive array. 检查是否为原始类型数组- Parameters:
obj- the object | 对象- Returns:
- true if it is a primitive array | 如果是原始类型数组返回 true
-
isInstance
-
getType
-
isWrapperType
Returns true if the class is a primitive wrapper type. 检查是否为包装类型- Parameters:
clazz- the class | 类型- Returns:
- true if it is a wrapper type | 如果是包装类型返回 true
-
isPrimitiveOrWrapper
Returns true if the class is a primitive type or its wrapper. 检查是否为原始类型或包装类型- Parameters:
clazz- the class | 类型- Returns:
- true if primitive or wrapper | 如果是原始类型或包装类型返回 true
-
hashCode
Computes the hash code for the given values. 计算对象的哈希值- Parameters:
values- objects to hash | 对象数组- Returns:
- hash code | 哈希值
-
identityHashCode
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
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
-
toString
-
toDebugString
-
toOptional
Wraps the object in an Optional. 将对象包装为 Optional- Type Parameters:
T- object type | 对象类型- Parameters:
obj- the object | 对象- Returns:
- Optional wrapping | Optional 包装
-