Class CopyOptions

java.lang.Object
org.aoju.bus.core.beans.copier.CopyOptions
All Implemented Interfaces:
Serializable

public class CopyOptions extends Object implements Serializable
属性拷贝选项 包括: 1、限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类 2、是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null 3、忽略的属性列表,设置一个属性列表,不拷贝这些属性值
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • editable

      protected Class<?> editable
      限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类 如果目标对象是Map,源对象是Bean,则作用于源对象上
    • ignoreNullValue

      protected boolean ignoreNullValue
      是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
    • ignoreError

      protected boolean ignoreError
      是否忽略字段注入错误
    • ignoreCase

      protected boolean ignoreCase
      是否忽略字段大小写
    • fieldValueEditor

      protected BiFunction<String,Object,Object> fieldValueEditor
      字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
    • transientSupport

      protected boolean transientSupport
      是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略
    • override

      protected boolean override
      是否覆盖目标值,如果不覆盖,会先读取目标对象的值,非null则写,否则忽略如果覆盖,则不判断直接写
    • converter

      protected TypeConverter converter
      自定义类型转换器,默认使用全局万能转换器转换
  • Constructor Details

    • CopyOptions

      public CopyOptions()
      构造拷贝选项
    • CopyOptions

      public CopyOptions(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties)
      构造拷贝选项
      Parameters:
      editable - 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
      ignoreNullValue - 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
      ignoreProperties - 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
  • Method Details

    • create

      public static CopyOptions create()
      创建拷贝选项
      Returns:
      拷贝选项
    • create

      public static CopyOptions create(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties)
      创建拷贝选项
      Parameters:
      editable - 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
      ignoreNullValue - 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
      ignoreProperties - 忽略的属性列表,设置一个属性列表,不拷贝这些属性值
      Returns:
      拷贝选项
    • setEditable

      public CopyOptions setEditable(Class<?> editable)
      设置限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
      Parameters:
      editable - 限制的类或接口
      Returns:
      this
    • setIgnoreNullValue

      public CopyOptions setIgnoreNullValue(boolean ignoreNullVall)
      设置是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
      Parameters:
      ignoreNullVall - 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
      Returns:
      this
    • ignoreNullValue

      public CopyOptions ignoreNullValue()
      设置忽略空值,当源对象的值为null时,忽略而不注入此值
      Returns:
      this
    • setPropertiesFilter

      public CopyOptions setPropertiesFilter(BiPredicate<Field,Object> propertiesFilter)
      属性过滤器,断言通过的属性才会被复制 BiPredicate.test(Object, Object)返回true则属性通过,false不通过,抛弃之
      Parameters:
      propertiesFilter - 属性过滤器
      Returns:
      this
    • setIgnoreProperties

      public CopyOptions setIgnoreProperties(String... ignoreProperties)
      设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
      Parameters:
      ignoreProperties - 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
      Returns:
      this
    • setIgnoreProperties

      public <P, R> CopyOptions setIgnoreProperties(Func1<P,R>... funcs)
      设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值,Lambda方式
      Type Parameters:
      P - 参数类型
      R - 返回值类型
      Parameters:
      funcs - 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
      Returns:
      this
    • setIgnoreError

      public CopyOptions setIgnoreError(boolean ignoreError)
      设置是否忽略字段的注入错误
      Parameters:
      ignoreError - 是否忽略注入错误
      Returns:
      this
    • ignoreError

      public CopyOptions ignoreError()
      设置忽略字段的注入错误
      Returns:
      this
    • setIgnoreCase

      public CopyOptions setIgnoreCase(boolean ignoreCase)
      设置是否忽略字段的大小写
      Parameters:
      ignoreCase - 是否忽略大小写
      Returns:
      this
    • ignoreCase

      public CopyOptions ignoreCase()
      设置忽略字段的大小写
      Returns:
      this
    • setFieldMapping

      public CopyOptions setFieldMapping(Map<String,String> fieldMapping)
      设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
      Parameters:
      fieldMapping - 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
      Returns:
      this
    • setFieldNameEditor

      public CopyOptions setFieldNameEditor(Editor<String> fieldNameEditor)
      设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等 此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致 当转换后的字段名为null时忽略这个字段
      Parameters:
      fieldNameEditor - 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
      Returns:
      this
    • setFieldValueEditor

      public CopyOptions setFieldValueEditor(BiFunction<String,Object,Object> fieldValueEditor)
      设置字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
      Parameters:
      fieldValueEditor - 字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
      Returns:
      this
    • editFieldValue

      protected Object editFieldValue(String fieldName, Object fieldValue)
      编辑字段值
      Parameters:
      fieldName - 字段名
      fieldValue - 字段值
      Returns:
      编辑后的字段值
    • setTransientSupport

      public CopyOptions setTransientSupport(boolean transientSupport)
      设置是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略
      Parameters:
      transientSupport - 是否支持
      Returns:
      this
    • setOverride

      public CopyOptions setOverride(boolean override)
      设置是否覆盖目标值,如果不覆盖,会先读取目标对象的值,非null则写,否则忽略如果覆盖,则不判断直接写
      Parameters:
      override - 是否覆盖目标值
      Returns:
      this
    • setConverter

      public CopyOptions setConverter(TypeConverter converter)
      设置自定义类型转换器,默认使用全局万能转换器转换
      Parameters:
      converter - 转换器
      Returns:
      this
    • convertField

      protected Object convertField(Type targetType, Object fieldValue)
      使用自定义转换器转换字段值 如果自定义转换器为null,则返回原值
      Parameters:
      targetType - 目标类型
      fieldValue - 字段值
      Returns:
      编辑后的字段值
    • editFieldName

      protected String editFieldName(String fieldName)
      转换字段名为编辑后的字段名
      Parameters:
      fieldName - 字段名
      Returns:
      编辑后的字段名
    • testPropertyFilter

      protected boolean testPropertyFilter(Field field, Object value)
      测试是否保留字段,true保留,false不保留
      Parameters:
      field - 字段
      value - 值
      Returns:
      是否保留