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

fj.data.optic.Prism 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.data.Either;
import fj.data.Option;

/**
 * {@link PPrism} restricted to monomorphic update
 */
public final class Prism extends PPrism {

  final PPrism pPrism;

  public Prism(final PPrism pPrism) {
    this.pPrism = pPrism;
  }

  @Override
  public Either getOrModify(final S s) {
    return pPrism.getOrModify(s);
  }

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

  @Override
  public Option getOption(final S s) {
    return pPrism.getOption(s);
  }

  /***********************************************************/
  /** Compose methods between a {@link Prism} and another Optics */
  /***********************************************************/

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

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

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

  /** compose a {@link Prism} with a {@link Lens} */
  public  Optional composeLens(final Lens other) {
    return new Optional<>(pPrism.composeLens(other.pLens));
  }

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

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

  /*****************************************************************/
  /** Transformation methods to view a {@link Prism} as another Optics */
  /*****************************************************************/

  /** view a {@link Prism} as a {@link Setter} */
  @Override
  public Setter asSetter() {
    return new Setter<>(pPrism.asSetter());
  }

  /** view a {@link Prism} as a {@link Traversal} */
  @Override
  public Traversal asTraversal() {
    return new Traversal<>(pPrism.asTraversal());
  }

  /** view a {@link Prism} as a {@link Optional} */
  @Override
  public Optional asOptional() {
    return new Optional<>(pPrism.asOptional());
  }

  public static  Prism id() {
    return new Prism<>(PPrism.pId());
  }

  public static  Prism prism(final F> getOption, final F reverseGet) {
    return new Prism<>(new PPrism() {

      @Override
      public Either getOrModify(final S s) {
        return getOption.f(s).option(Either.left(s), Either.right_());
      }

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

      @Override
      public Option getOption(final S s) {
        return getOption.f(s);
      }
    });
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy