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

com.anaptecs.jeaf.junit.pojo.mapper.ChildPOJOToBChildPOJOConverter Maven / Gradle / Ivy

There is a newer version: 1.24.2
Show newest version
/*
 * anaptecs GmbH, Ricarda-Huch-Str. 71, 72760 Reutlingen, Germany
 *
 * Copyright 2004 - 2019. All rights reserved.
 */
package com.anaptecs.jeaf.junit.pojo.mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.anaptecs.jeaf.core.api.ObjectMapper;
import com.anaptecs.jeaf.junit.pojo.BChildPOJO;
import com.anaptecs.jeaf.junit.pojo.ChildPOJO;
import com.anaptecs.jeaf.xfun.api.checks.Assert;
import com.anaptecs.jeaf.xfun.api.checks.Check;

/**
 * Class implements an bidirectional object mapper between classes {@link ChildPOJO} as source and {@link BChildPOJO} as
 * target.
 */
public class ChildPOJOToBChildPOJOConverter implements ObjectMapper {
  /**
   * Only instance of this class. We can avoid to create multiple instances as object mapper implementations are
   * stateless and thread safe.
   */
  private static final ChildPOJOToBChildPOJOConverter INSTANCE = new ChildPOJOToBChildPOJOConverter();

  /**
   * Constructor is private. In order to avoid that unnecessary instances are created.
   */
  private ChildPOJOToBChildPOJOConverter( ) {
  }

  /**
   * Method returns an instance of this object mapper.
   *
   * @return {@link ObjectMapper} Instance an object mapper that is capable to map between ChildPOJO and BChildPOJO. The
   * method never returns null.
   */
  public static ChildPOJOToBChildPOJOConverter getConverter( ) {
    return INSTANCE;
  }

  /**
   * Method creates a new BChildPOJO object from the passed ChildPOJO object and applies the defined mappings of type
   * {@link MappingType#CREATE} from the UML model.
   *
   * @param pSource ChildPOJO object that should be used to create a new BChildPOJO object. The parameter must not be
   * null.
   * @return {@link BChildPOJO} Created BChildPOJO object. The method never returns null.
   */
  public static BChildPOJO createNewTargetObject( ChildPOJO pSource ) {
    // Check parameter
    Check.checkInvalidParameterNull(pSource, "pSource");
    // Create new target object.
    BChildPOJO lTarget = BChildPOJO.builder().build();
    ChildPOJOToBChildPOJOConverter lObjectMapper = ChildPOJOToBChildPOJOConverter.getConverter();
    // As class ChildPOJO also has a super class we have to retrieve a mapper for its super class as well. The delegate
    // is responsible for applying all mappings of the super classes.
    lObjectMapper.applyParentMappings(pSource, lTarget, MappingType.CREATE);
    // Take value(s) from source, map them to target and return the created target object.
    lObjectMapper.applyMappings(pSource, lTarget, MappingType.CREATE);
    return lTarget;
  }

  /**
   * Method creates new BChildPOJO objects based on the passed ChildPOJO objects. For every ChildPOJO object a new
   * BChildPOJO object will be returned. The mappings for case {@link MappingType#CREATE} will be applied.
   *
   * @param pSourceObjects All ChildPOJO objects that should be used to create BChildPOJO objects. The parameter must
   * not be null.
   * @return {@link List} List with all BChildPOJO objects that were created. The method never returns null.
   */
  public static List createNewTargetObjects( Collection pSourceObjects ) {
    // Check parameter
    Check.checkInvalidParameterNull(pSourceObjects, "pSourceObjects");
    // Create new target object for every passed source object
    List lTargetObjects = new ArrayList(pSourceObjects.size());
    for (ChildPOJO lNext : pSourceObjects) {
      BChildPOJO lNewTargetObject = ChildPOJOToBChildPOJOConverter.createNewTargetObject(lNext);
      lTargetObjects.add(lNewTargetObject);
    }
    // Return all created target objects.
    return lTargetObjects;
  }

  /**
   * Method applies the mappings according to the passed mapping type on the passed source and target object.
   *
   * @param pSource Source object which should be used. The parameter must not be null. The effective type and direction
   * of the object mapping depends on parameter pMappingType.
   * @param pTarget Target object which should be used. The parameter must not be null. The effective type and direction
   * of the object mapping depends on parameter pMappingType.
   * @param pMappingType Parameter defines which type of mapping should be applied.
   */
  @Override
  public void applyMappings( ChildPOJO pSource, BChildPOJO pTarget, ObjectMapper.MappingType pMappingType ) {
    // Check parameters
    Check.checkInvalidParameterNull(pSource, "pSource");
    Check.checkInvalidParameterNull(pTarget, "pTarget");
    // Apply mapping of super classes.
    this.applyParentMappings(pSource, pTarget, pMappingType);
    // Select the type of mapping that should be executed.
    switch (pMappingType) {
      // Mappings for creation of target object.
      case CREATE:
        this.setTargetObject(pSource, pTarget);
        break;
      // Mapping for update of target object
      case UPDATE:
        this.updateTargetObject(pSource, pTarget);
        break;
      // Mapping for reading from target object.
      case READ:
        this.setSourceObject(pSource, pTarget);
        break;
      // Invalid mapping type. Most likely this is because of an outdated version of JEAF Generator that does not match
      // to the used version of JEAF Core.
      default:
        Assert.internalError("Unexpected object mapping type '" + pMappingType.name()
            + ". Please regenerate code with the latest version of JEAF Generator.");
    }
  }

  /**
   * Method applies the mappings according to the passed mapping type on the passed source and target objects. The
   * entries in the list of source and target objects are matched according to their index. This also means that both
   * list must have the same amount of entries.
   *
   * @param pSources Source objects which should be used. The parameter must not be null. The effective type and
   * direction of the object mapping depends on parameter pMappingType.
   * @param pTargets Target objects which should be used. The parameter must not be null. The effective type and
   * direction of the object mapping depends on parameter pMappingType.
   * @param pMappingType Parameter defines which type of mapping should be applied.
   */
  @Override
  public void applyMappings( List pSources, List pTargets,
      ObjectMapper.MappingType pMappingType ) {
    // Check parameters
    Check.checkInvalidParameterNull(pSources, "pSources");
    Check.checkInvalidParameterNull(pTargets, "pTargets");
    // Resolve iterators for sources and targets.
    Iterator lSourceIterator = pSources.iterator();
    Iterator lTargetIterator = pTargets.iterator();
    // Apply mappings for each pair of source and target.
    while (lSourceIterator.hasNext()) {
      this.applyMappings(lSourceIterator.next(), lTargetIterator.next(), pMappingType);
    }
  }

  /**
   * Method creates a new ChildPOJO object and applies the defined mappings of type {@link MappingType#READ} from the
   * UML model.
   *
   * @param pTarget BChildPOJO object that should be used to create a new ChildPOJO object. The parameter must not be
   * null.
   * @return {@link ChildPOJO} Created ChildPOJO object. The method never returns null.
   */
  public static ChildPOJO createNewSourceObject( BChildPOJO pTarget ) {
    // Check parameter
    Check.checkInvalidParameterNull(pTarget, "pTarget");
    // Create new source object, apply mappings and return created object.
    ChildPOJO lNewSourceObject = ChildPOJO.builder().build();
    ChildPOJOToBChildPOJOConverter lObjectMapper = ChildPOJOToBChildPOJOConverter.getConverter();
    lObjectMapper.applyMappings(lNewSourceObject, pTarget, MappingType.READ);
    return lNewSourceObject;
  }

  /**
   * Method creates new ChildPOJO objects based on the passed BChildPOJO objects. For every BChildPOJO object a new
   * ChildPOJO object will be returned. The mappings for case {@link MappingType#READ} will be applied.
   *
   * @param pTargetObjects All target objects that should be used to create source objects. The parameter must not be
   * null.
   * @return {@link List} List with all source objects that were created. The method never returns null.
   */
  public static List createNewSourceObjects( Collection pTargetObjects ) {
    // Check parameter
    Check.checkInvalidParameterNull(pTargetObjects, "pTargetObjects");
    // Create new source object for every passed target object.
    List lNewObjects = new ArrayList(pTargetObjects.size());
    for (BChildPOJO lNext : pTargetObjects) {
      ChildPOJO lNewSourceObject = ChildPOJOToBChildPOJOConverter.createNewSourceObject(lNext);
      lNewObjects.add(lNewSourceObject);
    }
    return lNewObjects;
  }

  /**
   * Method maps all attributes from ChildPOJO to BChildPOJO. Mappings are applied only for case
   * {@link MappingType#READ}.
   *
   * @param pSource Source object for the mapping. The parameter must not be null.
   * @param pTarget Target object for the mapping. The parameter must not be null.
   */
  private void setTargetObject( ChildPOJO pSource, BChildPOJO pTarget ) {
    pTarget.setChildAttribute(pSource.getChildAttribute());
  }

  /**
   * Method maps all attributes from ChildPOJO to BChildPOJO. Mappings are applied only for case
   * {@link MappingType#UPDATE}.
   *
   * @param pSource Source object for the mapping. The parameter must not be null.
   * @param pTarget Target object for the mapping. The parameter must not be null.
   */
  private void updateTargetObject( ChildPOJO pSource, BChildPOJO pTarget ) {
    // Update target object
    pTarget.setChildAttribute(pSource.getChildAttribute());
  }

  /**
   * Method maps all attributes from BChildPOJO to ChildPOJO. Mappings are applied only for case
   * {@link MappingType#READ}.
   *
   * @param pSource Source object for the mapping. The parameter must not be null.
   * @param pTarget Target object for the mapping. The parameter must not be null.
   */
  private void setSourceObject( ChildPOJO pSource, BChildPOJO pTarget ) {
    pSource.setChildAttribute(pTarget.getChildAttribute());
  }

  /**
   * Method applies the object mappings for all combinations of the source's parent class with the target class and all
   * of its super classes.
   *
   * @param pSource Source object for the mapping. The parameter must not be null.
   * @param pTarget Target object for the mapping. The parameter must not be null.
   * @param pMappingType Parameter defines which type of mapping should be applied.
   */
  private void applyParentMappings( ChildPOJO pSource, BChildPOJO pTarget, ObjectMapper.MappingType pMappingType ) {
    ParentPOJOToBChildPOJOConverter.getConverter().applyMappings(pSource, pTarget, pMappingType);
    ParentPOJOToBParentPOJOConverter.getConverter().applyMappings(pSource, pTarget, pMappingType);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy