001    package fj.data.fingertrees;
002    
003    import fj.data.vector.V3;
004    import fj.F;
005    
006    /**
007     * A three-element prefix or suffix of a finger tree.
008     */
009    public final class Three<V, A> extends Digit<V, A> {
010      private final V3<A> as;
011    
012      Three(final Measured<V, A> m, final V3<A> as) {
013        super(m);
014        this.as = as;
015      }
016    
017      /**
018       * @see fj.data.fingertrees.Digit#foldRight(fj.F, Object)
019       */
020      public <B> B foldRight(final F<A, F<B, B>> aff, final B z) {
021        return aff.f(as._1()).f(aff.f(as._2()).f(aff.f(as._3()).f(z)));
022      }
023    
024      /**
025       * @see fj.data.fingertrees.Digit#foldLeft(fj.F, Object)
026       */
027      public <B> B foldLeft(final F<B, F<A, B>> bff, final B z) {
028        return as.toStream().foldLeft(bff, z);
029      }
030    
031      /**
032       * @see fj.data.fingertrees.Digit#match(fj.F, fj.F, fj.F, fj.F)
033       */
034      @Override public <B> B match(
035          final F<One<V, A>, B> one, final F<Two<V, A>, B> two, final F<Three<V, A>, B> three,
036          final F<Four<V, A>, B> four) {
037        return three.f(this);
038      }
039    
040      /**
041       * Returns the elements of this digit as a vector.
042       *
043       * @return the elements of this digit as a vector.
044       */
045      public V3<A> values() {
046        return as;
047      }
048    }