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

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

package com.remondis.remap;

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

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * This class can be used to manage bidirectional mappings. The configuration of mappers for both directions is required
 * to build a bidirectional mapping.
 *
 * @deprecated The intent of this class was to shorten the access to bidirectional mappings. Actually this class
 *             introduces overhead for creation and the access to the different mapping directions is confusing in most
 *             cases.
 *
 * @param  Source type
 * @param  Destination type
 */

@Deprecated
public class BidirectionalMapper {

  Mapper to;
  Mapper from;

  private BidirectionalMapper(Mapper to, Mapper from) {
    super();
    this.to = to;
    this.from = from;
  }

  /**
   * Creates a {@link BidirectionalMapper} for the specified {@link Mapper} objects that represent the two
   * uni-directional mappings..
   *
   * @param to Valid {@link Mapper} for mapping source to destination type.
   * @param from Valid {@link Mapper} for mapping destination back to source type.
   * @return Returns a {@link BidirectionalMapper}.
   */
  public static  BidirectionalMapper of(Mapper to, Mapper from) {
    denyNull("to", to);
    denyNull("from", from);
    return new BidirectionalMapper<>(to, from);
  }

  /**
   * Performs the mapping from the source to destination type.
   *
   * @param source The source object to map to a new destination object.
   * @return Returns a newly created destination object.
   */
  public D map(S source) {
    return to.map(source);
  }

  /**
   * Performs the mapping from the source to destination type for the specified {@link Collection}.
   *
   * @param source The source collection to map to a new collection of destination objects.
   * @return Returns a newly created collection of destination objects. The type of the resulting collection is either
   *         {@link List} or {@link Set} depending on the specified type.
   */
  public Collection map(Collection source) {
    return to.map(source);
  }

  /**
   * Performs the mapping from the source to destination type for the specified {@link List}.
   *
   * @param source The source collection to map to a new collection of destination objects.
   * @return Returns a newly created list of destination objects.
   */
  public List map(List source) {
    return to.map(source);
  }

  /**
   * Performs the mapping from the source to destination type for the specified {@link Set}.
   *
   * @param source The source collection to map to a new collection of destination objects.
   * @return Returns a newly created set of destination objects.
   */
  public Set map(Set source) {
    return to.map(source);
  }

  /**
   * Performs the mapping from the source to destination type for the elements provided by the specified
   * {@link Iterable} .
   *
   * @param iterable The source iterable to be mapped to a new {@link List} of destination objects.
   * @return Returns a newly created list of destination objects.
   */
  public List map(Iterable iterable) {
    return to.map(iterable);
  }

  /**
   * Performs the mapping from the destination to source type.
   *
   * @param destination The destination object to map to a new source object.
   * @return Returns a newly created source object.
   */
  public  S mapFrom(Dest destination) {
    return from.map(destination);
  }

  /**
   * Performs the mapping from the destination to source type for the specified {@link Collection}.
   *
   * @param destination The destination collection to map to a new collection of source objects.
   * @return Returns a newly created collection of source objects. The type of the resulting collection is either
   *         {@link List} or {@link Set} depending on the specified type.
   */
  public Collection mapFrom(Collection destination) {
    return from.map(destination);
  }

  /**
   * Performs the mapping from the destination to source type for the specified {@link List}.
   *
   * @param destination The destination collection to map to a new collection of source objects.
   * @return Returns a newly created list of source objects.
   */
  public List mapFrom(List destination) {
    return from.map(destination);
  }

  /**
   * Performs the mapping from the destination to source type for the specified {@link Set}.
   *
   * @param destination The destination collection to map to a new collection of source objects.
   * @return Returns a newly created set of source objects.
   */
  public Set mapFrom(Set destination) {
    return from.map(destination);
  }

  /**
   * Performs the mapping from the destination to source type for the elements provided by the specified
   * {@link Iterable} .
   *
   * @param iterable The destination iterable to be mapped to a new {@link List} of source objects.
   * @return Returns a newly created list of source objects.
   */
  public List mapFrom(Iterable iterable) {
    return from.map(iterable);
  }

  /**
   * Returns the {@link Mapper} object for mapping source to destination type.
   *
   * @return {@link Mapper}
   */
  public Mapper getMapper() {
    return to;
  }

  /**
   * Returns the {@link Mapper} object for mapping destination to source type.
   *
   * @return {@link Mapper}
   */
  public Mapper getFromMapper() {
    return from;
  }

  @Override
  public String toString() {
    return "BidirectionalMapper [to=" + to + ", from=" + from + "]";
  }

}