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

fj.data.fingertrees.One 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.fingertrees;

import fj.F;
import fj.P;
import fj.P2;
import fj.P3;
import fj.Show;
import fj.data.Option;
import fj.data.Stream;

import static fj.data.Option.none;

/**
 * A single-element prefix or suffix of a finger tree.
 */
public final class One extends Digit {
  private final A a;

  One(final Measured m, final A a) {
    super(m);
    this.a = a;
  }

  public  B foldRight(final F> aff, final B z) {
    return aff.f(a).f(z);
  }

  public  B foldLeft(final F> bff, final B z) {
    return bff.f(z).f(a);
  }

  @Override public  B match(
      final F, B> one, final F, B> two, final F, B> three,
      final F, B> four) {
    return one.f(this);
  }

  /**
   * Returns the single element in this digit.
   *
   * @return the single element in this digit.
   */
  public A value() {
    return a;
  }

  @Override P3>, A, Option>> split1(final F predicate, final V acc) {
    return P.p(none(), a, none());
  }

  @Override public P2 lookup(final F o, final int i) {
    return P.p(i, a);
  }

    @Override
    public int length() {
        return 1;
    }

  public String toString() {
    return Show.digitShow(Show.anyShow(), Show.anyShow()).showS(this);
  }

  public Stream toStream() {
    return Stream.single(a);
  }

}