All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.remondis.remap.RestructureBuilder Maven / Gradle / Ivy

package com.remondis.remap;

import static com.remondis.remap.Lang.denyNull;

import java.util.Map;
import java.util.function.Consumer;

/**
 * A builder for restructuring a field in the destination type.
 *
 * @param  The source type of mapping.
 * @param  The destination type of mapping.
 * @param  The type of the destination field.
 *
 * @author schuettec
 *
 */
public class RestructureBuilder {

  private MappingConfiguration mappingConfiguration;
  private TypedPropertyDescriptor typedPropertyDescriptor;

  RestructureBuilder(MappingConfiguration mappingConfiguration,
      TypedPropertyDescriptor typedPropertyDescriptor) {
    this.mappingConfiguration = mappingConfiguration;
    this.typedPropertyDescriptor = typedPropertyDescriptor;
  }

  /**
   * Tells the mapping, that the destination object can be restructured by simple implicit mappings. Use this
   * method if no custom mappings are required to build the destination object.
   *
   * @return Returns a {@link MappingConfiguration} for further configuration.
   */
  public MappingConfiguration implicitly() {
    return createRestructure(conf -> {
    }, false);
  }

  /**
   * Adds further mapping configurations to the mapping that is used to restructure the destination object. Use this
   * method, if custom mappings are required to build the destination object.
   *
   * @param restructureMappingConfiguration A {@link Consumer} that receives a {@link MappingConfiguration} and applies
   *        further mapping configurations.
   * @return Returns a {@link MappingConfiguration} for further configuration.
   */
  public MappingConfiguration applying(Consumer> restructureMappingConfiguration) {
    denyNull("restructureMappingConfiguration", restructureMappingConfiguration);
    boolean applyingSpecificConfiguration = true;
    return createRestructure(restructureMappingConfiguration, applyingSpecificConfiguration);
  }

  private MappingConfiguration createRestructure(
      Consumer> restructureMappingConfiguration, boolean applyingSpecificConfiguration) {
    Map, InternalMapper> mappers = mappingConfiguration.getMappers();
    @SuppressWarnings("unchecked")
    MappingConfiguration config = Mapping.from(mappingConfiguration.getSource())
        .to((Class) typedPropertyDescriptor.property.getPropertyType());
    // Do not make all source properties mandatory
    config.omitOtherSourceProperties();
    // Inherit registered mappers.
    mappers.entrySet()
        .stream()
        .forEach(entry -> {
          InternalMapper mapper = entry.getValue();
          config.useInternalMapper(mapper);
        });
    restructureMappingConfiguration.accept(config);
    Transformation restructureTransformation = new RestructureTransformation<>(config, null,
        typedPropertyDescriptor.property, null, applyingSpecificConfiguration);
    mappingConfiguration.addDestinationMapping(typedPropertyDescriptor.property, restructureTransformation);
    return mappingConfiguration;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy