java bean object property replication framework beanmapping release [0.0.2-annotation support

Keywords: Java github Attribute

BeanMapping

In order to specify the mapping method more flexibly, the @ BeanMapping annotation was introduced in version 0.0.2.

Definition of annotation

Annotation is defined in the bean mapping API module, which is introduced by default by bean mapping core.

package com.github.houbb.bean.mapping.api.annotation;

import com.github.houbb.bean.mapping.api.core.ICondition;
import com.github.houbb.bean.mapping.api.core.IConvert;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p> BeanMapping Annotate </p>
 *
 * <pre> Created: 2019/2/19 10:11 PM  </pre>
 * <pre> Project: bean-mapping  </pre>
 *
 * @author houbinbin
 * @since 0.1.0
 */
@Inherited
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BeanMapping {

    /**
     * Name of the field
     * If not, the name of the field is used by default
     * 1. The value of source is assigned to the object whose target is consistent with the current name attribute.
     * @return Name
     */
    String name() default "";

    /**
     * Effective conditions
     * 1. Effective by default
     * 2. When placed on the source field, indicates whether the value is assigned to the target field
     * When placed on the target field, indicates whether the assignment is accepted.
     * 3. source+target Assignment occurs only when it is also valid.
     * @return Specific effective realization
     */
    Class<? extends ICondition> condition() default ICondition.class;

    /**
     * Type conversion
     * 1. No conversion by default
     * 2. To ensure the certainty + flexibility of the transformation. Specifying this property in the object does not change the property value and type of the object.
     * If you want to change the original value, then the type will be limited a lot, not flexible enough.
     * 3. Only when the value of source can be set to target after conversion, the value of source after conversion will be assigned to the corresponding property of target. Other situations will not affect the value.
     * @return Specific transformation implementation
     */
    Class<? extends IConvert> convert() default IConvert.class;

}

name attribute

Sometimes the field names of source and target may be different. Just use this property to make them consistent.

ICondition interface

It is used to specify whether the assignment is effective or not. It can meet the common requirements that the target object will not be overwritten if it has a value.

public interface ICondition {

    /**
     * Convert original information to target information
     * @param context Current execution context
     * @return Conversion result
     */
    boolean condition(final IContext context);

}

IContext context interface

IContext is the execution context, which is convenient to get the execution related properties. More flexible to specify and implement our functions.

IConvert field conversion interface

Sometimes we want to process the value of the field, such as date / amount format, enumeration value display and so on.

With this interface, you can ensure the elegance of code assignment, improve the reusability of code, and more conform to the open close principle.

/**
 * <p> Adapter</p>
 * 1. All implementations should provide a default constructor
 * <pre> Created: 2019/2/19 10:15 PM  </pre>
 * <pre> Project: bean-mapping  </pre>
 *
 * @param <T> Target generics
 * @author houbinbin
 * @since 0.1.0
 */
public interface IConvert<T> {

    /**
     * Convert original information to target information
     * @param context Current execution context
     * @return Conversion result
     */
    T convert(final IContext context);

}

Expanding reading

Property replication framework-01-assignment of different name fields

Property replication framework-02-conditions for validity of custom assignment

Attribute replication framework-03-implementation of custom field conversion

Posted by tigger on Sun, 01 Dec 2019 00:09:08 -0800