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

de.alpharogroup.db.entitymapper.EntityDOMapper Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package de.alpharogroup.db.entitymapper;

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

import org.dozer.Mapper;
import org.dozer.MappingException;

import de.alpharogroup.db.entity.BaseEntity;
import de.alpharogroup.domain.DomainObject;

/**
 * The Interface {@link EntityDOMapper} provides the methods for mapping
 * entities to domain objects and back.
 *
 * @param 
 *            the element type of the entity object
 * @param 
 *            the generic type of the domain object
 */
public interface EntityDOMapper, DO extends DomainObject> {

	/**
	 * Gets the mapper.
	 *
	 * @return the mapper
	 */
	Mapper getMapper();

	/**
	 * Gets the domain object class.
	 *
	 * @return the domain object class
	 */
	Class getDomainObjectClass();

	/**
	 * Gets the entity class.
	 *
	 * @return the entity class
	 */
	Class getEntityClass();

	/**
	 * Maps the given entity object to a domain object.
	 *
	 * @param entity
	 *            the entity
	 * @return the domain object
	 */
	default DO toDomainObject(final E entity) {
		if (entity != null) {
			return getMapper().map(entity, getDomainObjectClass());
		}
		return null;
	};

	/**
	 * Maps the given list of entity objects to a list of domain objects.
	 *
	 * @param entities
	 *            the entities
	 * @return the list of domain objects.
	 */
	default List toDomainObjects(final Collection entities) {
		final List domainObjects = new ArrayList<>();
		if ((entities != null) && !entities.isEmpty()) {
			for (final E entity : entities) {
				domainObjects.add(toDomainObject(entity));
			}
		}
		return domainObjects;
	};

	/**
	 * Maps the given list of domain objects to a list of entity objects.
	 *
	 * @param domainObjects
	 *            the list of domain objects
	 * @return the list of entity objects.
	 */
	default List toEntities(final Collection domainObjects) {
		final List entities = new ArrayList<>();
		if ((domainObjects != null) && !domainObjects.isEmpty()) {
			for (final DO domainObject : domainObjects) {
				entities.add(toEntity(domainObject));
			}
		}
		return entities;
	};

	/**
	 * Maps the given domain object to a entity object.
	 *
	 * @param domainObject
	 *            the domain object
	 * @return the entity object
	 */
	default E toEntity(final DO domainObject) {
		if(domainObject != null) {
			return getMapper().map(domainObject, getEntityClass());
		}
		return null;
	};

	/**
	 * Constructs new instance of destinationClass and performs mapping between
	 * from source.
	 *
	 * @param             the generic type of the destinationClass
	 * @param             the generic type of the source
	 * @param source            the source
	 * @param destinationClass            the destination class
	 * @return the new instance of destinationClass mapped to source object.
	 * @throws MappingException             is thrown if something goes wrong with the mapping process.
	 */
	default  T map(final S source, final Class destinationClass) throws MappingException {
		return MapperExtensions.map(getMapper(), source, destinationClass);
	};

	/**
	 * Constructs new instances of destinationClass and performs mapping between
	 * from source.
	 *
	 * @param             the generic type of the destinationClass
	 * @param             the generic type of the source
	 * @param sources            the collection of source objects
	 * @param destinationClass            the destination class
	 * @return the new instance of destinationClass mapped to source object.
	 * @throws MappingException             is thrown if something goes wrong with the mapping process.
	 */
	default  List map(final Collection sources, final Class destinationClass) throws MappingException {
		return MapperExtensions.map(getMapper(), sources, destinationClass);
	};

}