Class OpenClone
java.lang.Object
cloud.opencode.base.deepclone.OpenClone
Facade class for deep cloning operations
深度克隆操作的门面类
Provides a simple, unified API for deep cloning objects using various strategies. This is the main entry point for all cloning operations.
提供简单统一的API,使用各种策略进行对象深度克隆。这是所有克隆操作的主入口。
Features | 主要功能:
- Single-object cloning - 单对象克隆
- Batch cloning - 批量克隆
- Parallel cloning with virtual threads - 虚拟线程并行克隆
- Async cloning - 异步克隆
- Multiple cloning strategies - 多种克隆策略
Usage Examples | 使用示例:
// Simple cloning
User cloned = OpenClone.clone(originalUser);
// Batch cloning
List<User> clonedList = OpenClone.cloneBatch(userList);
// Parallel cloning
List<User> parallel = OpenClone.cloneBatchParallel(userList, 4);
// Async cloning
CompletableFuture<User> future = OpenClone.cloneAsync(user);
// Custom cloner
Cloner custom = OpenClone.builder()
.reflective()
.maxDepth(50)
.build();
User cloned = custom.clone(user);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-deepclone V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ClonerBuilderbuilder()Creates a cloner builder 创建克隆器构建器static <T> Tclone(T original) Deep clones an object using the default reflective strategy 使用默认反射策略深度克隆对象static <T> TDeep clones using a specific cloner 使用指定克隆器深度克隆static <T> CompletableFuture<T> cloneAsync(T original) Asynchronously clones an object 异步克隆对象static <T> List<T> cloneBatch(List<T> originals) Clones a list of objects 克隆对象列表static <T> CompletableFuture<List<T>> cloneBatchAsync(List<T> originals) Asynchronously clones a batch of objects 异步批量克隆对象static <T> List<T> cloneBatchParallel(List<T> originals, int parallelism) Clones a list of objects in parallel using virtual threads 使用虚拟线程并行克隆对象列表static <T> TcloneBySerialization(T original) Deep clones using serialization strategy 使用序列化策略深度克隆static <T> TcloneByUnsafe(T original) Deep clones using Unsafe strategy (high performance) 使用Unsafe策略深度克隆(高性能)static <T> TcloneWith(T original, ClonePolicy policy) Deep clones with a specific clone policy 使用指定策略进行深度克隆static <T> TcopyTo(T source, T target) Copies all non-null fields from source to an existing target object 将源对象的所有非 null 字段复制到已有的目标对象static ClonerGets the default cloner 获取默认克隆器static booleanisImmutable(Class<?> type) Checks if a type is immutable 检查类型是否不可变static voidregisterImmutable(Class<?>... types) Registers types as immutable 注册类型为不可变static <T> TshallowClone(T original) Shallow clones an object (copies field references without deep cloning) 浅拷贝对象(复制字段引用,不进行深度克隆)
-
Method Details
-
clone
public static <T> T clone(T original) Deep clones an object using the default reflective strategy 使用默认反射策略深度克隆对象- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象- Returns:
- the cloned object | 克隆的对象
-
cloneBySerialization
public static <T> T cloneBySerialization(T original) Deep clones using serialization strategy 使用序列化策略深度克隆- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object (must be Serializable) | 原始对象(必须是Serializable)- Returns:
- the cloned object | 克隆的对象
-
cloneByUnsafe
public static <T> T cloneByUnsafe(T original) Deep clones using Unsafe strategy (high performance) 使用Unsafe策略深度克隆(高性能)- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象- Returns:
- the cloned object | 克隆的对象
-
clone
Deep clones using a specific cloner 使用指定克隆器深度克隆- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象cloner- the cloner to use | 要使用的克隆器- Returns:
- the cloned object | 克隆的对象
-
cloneBatch
-
cloneBatchParallel
Clones a list of objects in parallel using virtual threads 使用虚拟线程并行克隆对象列表- Type Parameters:
T- the object type | 对象类型- Parameters:
originals- the original objects | 原始对象列表parallelism- the parallelism level (unused, virtual threads auto-scale) | 并行度(未使用,虚拟线程自动扩展)- Returns:
- the cloned objects | 克隆的对象列表
-
cloneAsync
Asynchronously clones an object 异步克隆对象- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象- Returns:
- a future containing the cloned object | 包含克隆对象的Future
-
cloneBatchAsync
Asynchronously clones a batch of objects 异步批量克隆对象- Type Parameters:
T- the object type | 对象类型- Parameters:
originals- the original objects | 原始对象列表- Returns:
- a future containing the cloned objects | 包含克隆对象的Future
-
shallowClone
public static <T> T shallowClone(T original) Shallow clones an object (copies field references without deep cloning) 浅拷贝对象(复制字段引用,不进行深度克隆)- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象- Returns:
- the shallow cloned object | 浅拷贝的对象
-
copyTo
public static <T> T copyTo(T source, T target) Copies all non-null fields from source to an existing target object 将源对象的所有非 null 字段复制到已有的目标对象- Type Parameters:
T- the object type | 对象类型- Parameters:
source- the source object | 源对象target- the target object | 目标对象- Returns:
- the target object with copied fields | 复制了字段的目标对象
-
cloneWith
Deep clones with a specific clone policy 使用指定策略进行深度克隆- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象policy- the clone policy | 克隆策略- Returns:
- the cloned object | 克隆的对象
-
isImmutable
Checks if a type is immutable 检查类型是否不可变- Parameters:
type- the type | 类型- Returns:
- true if immutable | 如果不可变返回true
-
registerImmutable
Registers types as immutable 注册类型为不可变- Parameters:
types- the types | 类型
-
getDefaultCloner
Gets the default cloner 获取默认克隆器- Returns:
- the default cloner | 默认克隆器
-
builder
-