Record Class TypeCloneStrategy<T>
java.lang.Object
java.lang.Record
cloud.opencode.base.deepclone.strategy.TypeCloneStrategy<T>
- Type Parameters:
T- the type this strategy applies to | 此策略适用的类型
public record TypeCloneStrategy<T>(Class<T> type, UnaryOperator<T> cloner, boolean deepClone)
extends Record
Type-specific clone strategy configuration
特定类型的克隆策略配置
Defines how a specific type should be cloned, allowing custom cloning functions for types that require special handling.
定义特定类型应如何克隆,允许为需要特殊处理的类型提供自定义克隆函数。
Usage Examples | 使用示例:
// Register a custom cloner for Money type
TypeCloneStrategy<Money> strategy = TypeCloneStrategy.deep(
Money.class,
m -> new Money(m.getAmount(), m.getCurrency())
);
// Mark a type as immutable (no cloning needed)
TypeCloneStrategy<LocalDate> immutable = TypeCloneStrategy.immutable(LocalDate.class);
// Use shallow copy for a type
TypeCloneStrategy<SharedResource> shallow = TypeCloneStrategy.shallow(SharedResource.class);
Features | 主要功能:
- Type-specific clone functions - 特定类型的克隆函数
- Immutable type support - 不可变类型支持
- Deep and shallow clone modes - 深拷贝和浅拷贝模式
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Since:
- JDK 25, opencode-base-deepclone V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTypeCloneStrategy(Class<T> type, UnaryOperator<T> cloner, boolean deepClone) Creates a TypeCloneStrategy 创建类型克隆策略 -
Method Summary
Modifier and TypeMethodDescriptionClones an object using this strategy 使用此策略克隆对象cloner()Returns the value of theclonerrecord component.static <T> TypeCloneStrategy<T> deep(Class<T> type, UnaryOperator<T> cloner) Creates a deep clone strategy with a custom cloner 使用自定义克隆器创建深度克隆策略booleanReturns the value of thedeepClonerecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.static <T> TypeCloneStrategy<T> Creates an immutable strategy (returns same reference) 创建不可变策略(返回相同引用)static <T> TypeCloneStrategy<T> Creates a shallow copy strategy (returns same reference) 创建浅拷贝策略(返回相同引用)final StringtoString()Returns a string representation of this record class.type()Returns the value of thetyperecord component.
-
Constructor Details
-
TypeCloneStrategy
Creates a TypeCloneStrategy 创建类型克隆策略- Parameters:
type- the type | 类型cloner- the cloning function | 克隆函数deepClone- whether this is a deep clone | 是否为深度克隆
-
-
Method Details
-
deep
Creates a deep clone strategy with a custom cloner 使用自定义克隆器创建深度克隆策略- Type Parameters:
T- the type parameter | 类型参数- Parameters:
type- the type | 类型cloner- the cloning function | 克隆函数- Returns:
- the strategy | 策略
-
shallow
Creates a shallow copy strategy (returns same reference) 创建浅拷贝策略(返回相同引用)- Type Parameters:
T- the type parameter | 类型参数- Parameters:
type- the type | 类型- Returns:
- the strategy | 策略
-
immutable
Creates an immutable strategy (returns same reference) 创建不可变策略(返回相同引用)Same as shallow(), used for semantic clarity when dealing with immutable types.
与shallow()相同,用于处理不可变类型时语义更清晰。
- Type Parameters:
T- the type parameter | 类型参数- Parameters:
type- the type | 类型- Returns:
- the strategy | 策略
-
apply
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
type
-
cloner
Returns the value of theclonerrecord component.- Returns:
- the value of the
clonerrecord component
-
deepClone
public boolean deepClone()Returns the value of thedeepClonerecord component.- Returns:
- the value of the
deepClonerecord component
-