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

    Constructors
    Constructor
    Description
    TypeCloneStrategy(Class<T> type, UnaryOperator<T> cloner, boolean deepClone)
    Creates a TypeCloneStrategy 创建类型克隆策略
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(T original)
    Clones an object using this strategy 使用此策略克隆对象
    Returns the value of the cloner record component.
    static <T> TypeCloneStrategy<T>
    deep(Class<T> type, UnaryOperator<T> cloner)
    Creates a deep clone strategy with a custom cloner 使用自定义克隆器创建深度克隆策略
    boolean
    Returns the value of the deepClone record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    static <T> TypeCloneStrategy<T>
    immutable(Class<T> type)
    Creates an immutable strategy (returns same reference) 创建不可变策略(返回相同引用)
    static <T> TypeCloneStrategy<T>
    shallow(Class<T> type)
    Creates a shallow copy strategy (returns same reference) 创建浅拷贝策略(返回相同引用)
    final String
    Returns a string representation of this record class.
    Returns the value of the type record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypeCloneStrategy

      public TypeCloneStrategy(Class<T> type, UnaryOperator<T> cloner, boolean deepClone)
      Creates a TypeCloneStrategy 创建类型克隆策略
      Parameters:
      type - the type | 类型
      cloner - the cloning function | 克隆函数
      deepClone - whether this is a deep clone | 是否为深度克隆
  • Method Details

    • deep

      public static <T> TypeCloneStrategy<T> deep(Class<T> type, UnaryOperator<T> cloner)
      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

      public static <T> TypeCloneStrategy<T> shallow(Class<T> type)
      Creates a shallow copy strategy (returns same reference) 创建浅拷贝策略(返回相同引用)
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      Returns:
      the strategy | 策略
    • immutable

      public static <T> TypeCloneStrategy<T> immutable(Class<T> type)
      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

      public T apply(T original)
      Clones an object using this strategy 使用此策略克隆对象
      Parameters:
      original - the original object | 原始对象
      Returns:
      the cloned object | 克隆的对象
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • type

      public Class<T> type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • cloner

      public UnaryOperator<T> cloner()
      Returns the value of the cloner record component.
      Returns:
      the value of the cloner record component
    • deepClone

      public boolean deepClone()
      Returns the value of the deepClone record component.
      Returns:
      the value of the deepClone record component