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

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

There is a newer version: 4.3.7
Show newest version
package com.remondis.remap;

import static com.remondis.remap.Lang.denyNull;
import static java.util.Objects.nonNull;

import java.util.Optional;
import java.util.function.Consumer;

/**
 * Builder to assert a set operation on a {@link Mapper} object using {@link AssertConfiguration}.
 *
 * @param  The source type
 * @param  The destination type
 * @param  The type of the destination field
 * @author schuettec
 */
public class RestructureAssertBuilder {

  private TypedPropertyDescriptor destProperty;
  private AssertConfiguration asserts;

  RestructureAssertBuilder(TypedPropertyDescriptor destProperty, AssertConfiguration asserts) {
    super();
    this.destProperty = destProperty;
    this.asserts = asserts;
  }

  /**
   * Expects a restructure mapping using an implicit mapping.
   *
   * @return Returns the {@link AssertConfiguration} for further configuration.
   */
  public AssertConfiguration implicitly() {
    return _assertRestructure(null);
  }

  /**
   * Expects a restructure-mapping with the specified mapping configuration.
   *
   * @param restructureMappingAssertions The assertions made about the mapper used for restructuring.
   * @return Returns the {@link AssertConfiguration} for further configuration.
   */
  public AssertConfiguration applying(
      Consumer> restructureMappingAssertions) {
    denyNull("restructureMappingAssertions", restructureMappingAssertions);
    return _assertRestructure(restructureMappingAssertions);
  }

  @SuppressWarnings({
      "rawtypes", "unchecked"
  })
  private AssertConfiguration _assertRestructure(
      Consumer> restructureMappingAssertions) {
    Optional restructureTransformation = asserts.getMapping()
        .getMappings()
        .stream()
        .filter(t -> t instanceof RestructureTransformation)
        .map(t -> (RestructureTransformation) t)
        .filter(t -> t.getDestinationProperty()
            .equals(destProperty.property))
        // RestructureTransformation cannot appear more than once,
        // because we are streaming over a set and RestructureTransformation
        // inherits equals/hashCode from Transformation.
        .findFirst();
    if (restructureTransformation.isPresent()) {
      asserts
          .addVerification(new RestructureVerification(restructureTransformation.get(), restructureMappingAssertions));
    }
    asserts.addAssertion(new RestructureTransformation<>(asserts.getMapping(), null, destProperty.property, null,
        nonNull(restructureMappingAssertions)));
    return asserts;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy