001    package fj.data.fingertrees;
002    
003    import fj.F;
004    import fj.P2;
005    import static fj.Bottom.error;
006    
007    /**
008     * The empty tree.
009     */
010    public final class Empty<V, A> extends FingerTree<V, A> {
011      Empty(final Measured<V, A> m) {
012        super(m);
013      }
014    
015      @Override public FingerTree<V, A> cons(final A a) {
016        return new Single<V, A>(measured(), a);
017      }
018    
019      @Override public FingerTree<V, A> snoc(final A a) {
020        return cons(a);
021      }
022    
023      @Override public FingerTree<V, A> append(final FingerTree<V, A> t) {
024        return t;
025      }
026    
027      @Override public P2<Integer, A> lookup(final F<V, Integer> o, final int i) {
028        throw error("Lookup of empty tree.");
029      }
030    
031      /**
032       * @see fj.data.fingertrees.FingerTree#foldRight(fj.F, Object)
033       */
034      @Override public <B> B foldRight(final F<A, F<B, B>> aff, final B z) {
035        return z;
036      }
037    
038      /**
039       * @see fj.data.fingertrees.FingerTree#reduceRight(fj.F)
040       */
041      public A reduceRight(final F<A, F<A, A>> aff) {
042        throw error("Reduction of empty tree");
043      }
044    
045      @Override public <B> B foldLeft(final F<B, F<A, B>> bff, final B z) {
046        return z;
047      }
048    
049      @Override public A reduceLeft(final F<A, F<A, A>> aff) {
050        throw error("Reduction of empty tree");
051      }
052    
053      @Override public <B> FingerTree<V, B> map(final F<A, B> abf, final Measured<V, B> m) {
054        return new Empty<V, B>(m);
055      }
056    
057      /**
058       * Returns zero.
059       *
060       * @return Zero.
061       */
062      public V measure() {
063        return measured().zero();
064      }
065    
066      /**
067       * Pattern matching on the structure of this tree. Matches the empty tree.
068       */
069      @Override public <B> B match(
070          final F<fj.data.fingertrees.Empty<V, A>, B> empty, final F<Single<V, A>, B> single, final F<Deep<V, A>, B> deep) {
071        return empty.f(this);
072      }
073    
074    
075    }