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

fj.data.optic.Iso Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj.data.optic;

import fj.F;
import fj.P2;

/** {@link PIso} when S = T and A = B */
public final class Iso extends PIso {

  final PIso pIso;

  public Iso(final PIso pIso) {
    this.pIso = pIso;
  }

  @Override
  public A get(final S s) {
    return pIso.get(s);
  }

  @Override
  public S reverseGet(final A a) {
    return pIso.reverseGet(a);
  }

  @Override
  public Iso reverse() {
    return new Iso<>(pIso.reverse());
  }

  /** pair two disjoint {@link Iso} */
  public  Iso, P2> product(final Iso other) {
    return new Iso<>(pIso.product(other.pIso));
  }

  @Override
  public  Iso, P2> first() {
    return new Iso<>(pIso.first());
  }

  @Override
  public  Iso, P2> second() {
    return new Iso<>(pIso.second());
  }

  /**********************************************************/
  /** Compose methods between an {@link Iso} and another Optics */
  /**********************************************************/

  /** compose an {@link Iso} with a {@link Setter} */
  public final  Setter composeSetter(final Setter other) {
    return new Setter<>(pIso.composeSetter(other.pSetter));
  }

  /** compose an {@link Iso} with a {@link Traversal} */
  public final  Traversal composeTraversal(final Traversal other) {
    return new Traversal<>(pIso.composeTraversal(other.pTraversal));
  }

  /** compose an {@link Iso} with a {@link Optional} */
  public final  Optional composeOptional(final Optional other) {
    return new Optional<>(pIso.composeOptional(other.pOptional));
  }

  /** compose an {@link Iso} with a {@link Prism} */
  public final  Prism composePrism(final Prism other) {
    return new Prism<>(pIso.composePrism(other.pPrism));
  }

  /** compose an {@link Iso} with a {@link Lens} */
  public final  Lens composeLens(final Lens other) {
    return asLens().composeLens(other);
  }

  /** compose an {@link Iso} with an {@link Iso} */
  public final  Iso composeIso(final Iso other) {
    return new Iso<>(pIso.composeIso(other.pIso));
  }

  /****************************************************************/
  /** Transformation methods to view an {@link Iso} as another Optics */
  /****************************************************************/

  /** view an {@link Iso} as a {@link Setter} */
  @Override
  public final Setter asSetter() {
    return new Setter<>(pIso.asSetter());
  }

  /** view an {@link Iso} as a {@link Traversal} */
  @Override
  public final Traversal asTraversal() {
    return new Traversal<>(pIso.asTraversal());
  }

  /** view an {@link Iso} as a {@link Optional} */
  @Override
  public final Optional asOptional() {
    return new Optional<>(pIso.asOptional());
  }

  /** view an {@link Iso} as a {@link Prism} */
  @Override
  public final Prism asPrism() {
    return new Prism<>(pIso.asPrism());
  }

  /** view an {@link Iso} as a {@link Lens} */
  @Override
  public final Lens asLens() {
    return new Lens<>(pIso.asLens());
  }

  /** create an {@link Iso} using a pair of functions: one to get the target and one to get the source. */
  public static  Iso iso(final F get, final F reverseGet) {
    return new Iso<>(PIso.pIso(get, reverseGet));
  }

  /**
   * create an {@link Iso} between any type and itself. id is the zero element of optics composition, for all optics o of type O
   * (e.g. Lens, Iso, Prism, ...):
   *
   * 
   *  o composeIso Iso.id == o
   *  Iso.id composeO o == o
   * 
* * (replace composeO by composeLens, composeIso, composePrism, ...) */ public static Iso id() { return new Iso<>(PIso.pId()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy