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 final  Setter composeSetter(final Setter other) {
    return new Setter<>(pPrism.composeSetter(other.pSetter));
  }

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

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

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

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

  /** compose a {@link Prism} with an {@link Iso} */
  public final  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 final Setter asSetter() {
    return new Setter<>(pPrism.asSetter());
  }

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

  /** view a {@link Prism} as a {@link Optional} */
  @Override
  public final 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