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

com.github.tonivade.purefun.optics.Iso Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purefun.optics;

import static java.util.Objects.requireNonNull;

import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Operator1;

public final class Iso {

  private final PIso delegate;

  protected Iso(PIso delegate) {
    this.delegate = requireNonNull(delegate);
  }

  public static  Iso of(Function1 get, Function1 reverseGet) {
    return new Iso<>(PIso.of(get, reverseGet));
  }

  public static  Iso identity() {
    return new Iso<>(PIso.identity());
  }

  public Iso reverse() {
    return new Iso<>(delegate.reverse());
  }

  public A get(S target) {
    return delegate.get(target);
  }

  public S set(A value) {
    return delegate.set(value);
  }

  public S modify(S target, Operator1 mapper) {
    return delegate.modify(target, mapper);
  }

  public Operator1 lift(Operator1 mapper) {
    return delegate.lift(mapper)::apply;
  }

  public Lens asLens() {
    return new Lens<>(delegate.asLens());
  }

  public Prism asPrism() {
    return new Prism<>(delegate.asPrism());
  }

  public Optional asOptional() {
    return new Optional<>(delegate.asOptional());
  }

  public  Iso compose(Iso other) {
    return new Iso<>(delegate.compose(other.delegate));
  }

  public  Lens compose(Lens other) {
    return asLens().compose(other);
  }

  public  Prism compose(Prism other) {
    return asPrism().compose(other);
  }

  public  Optional compose(Optional other) {
    return asOptional().compose(other);
  }
}