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

fj.data.fingertrees.Empty 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.P2;
import fj.P3;
import fj.Show;
import fj.data.Stream;

import static fj.Bottom.error;

/**
 * The empty tree.
 */
public final class Empty extends FingerTree {
  Empty(final Measured m) {
    super(m);
  }

  @Override public FingerTree cons(final A a) {
    return new Single<>(measured(), a);
  }

  @Override public FingerTree snoc(final A a) {
    return cons(a);
  }

  @Override public A head() { throw error("Selection of head in empty tree"); }

  @Override public A last() { throw error("Selection of last in empty tree"); }

  @Override public FingerTree tail() { throw error("Selection of tail in empty tree"); }

  @Override public FingerTree init() { throw error("Selection of init in empty tree"); }

  @Override public FingerTree append(final FingerTree t) {
    return t;
  }

  @Override public P2 lookup(final F o, final int i) { throw error("Lookup of empty tree."); }

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

    @Override public  B foldRight(final F> aff, final B z) {
    return z;
  }

  public A reduceRight(final F> aff) {
    throw error("Reduction of empty tree");
  }

  @Override public  B foldLeft(final F> bff, final B z) {
    return z;
  }

  @Override public A reduceLeft(final F> aff) {
    throw error("Reduction of empty tree");
  }

  @Override public  FingerTree map(final F abf, final Measured m) {
    return new Empty<>(m);
  }

  /**
   * Returns zero.
   *
   * @return Zero.
   */
  public V measure() {
    return measured().zero();
  }

  /**
   * Pattern matching on the structure of this tree. Matches the empty tree.
   */
  @Override public  B match(
      final F, B> empty, final F, B> single, final F, B> deep) {
    return empty.f(this);
  }

  @Override P3, A, FingerTree> split1(final F predicate, final V acc) {
    throw error("Splitting an empty tree");
  }

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

  public Stream toStream() {
    return Stream.nil();
  }

}