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

me.shaftesbury.utils.functional.IterableHelper Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
package me.shaftesbury.utils.functional;

import org.javatuples.Pair;
import org.javatuples.Triplet;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Bob
 * Date: 29/11/13
 * Time: 17:03
 * To change this template use File | Settings | File Templates.
 */
public class IterableHelper
{
    public static final Iterable2 create(final java.lang.Iterable it)
    {
        return new Iterable2()
        {
            private final java.lang.Iterable i = it;
            @Override
            public Iterator iterator() { return i.iterator(); }

            public final Iterable2 filter(final Func f) { return create(Functional.seq.filter(f,i)); }
            public final Iterable2 map(final Func f) { return create(Functional.seq.map(f, i)); }
            public  Iterable2 mapi(Func2 f) { return create(Functional.seq.mapi(f, i)); }
            public final Iterable2 choose(final Func> f) { return create(Functional.seq.choose(f, i)); }
            public final boolean exists(final Func f) { return Functional.exists(f, i); }
            public final boolean forAll(final Func f) { return Functional.forAll(f, i); }
            public  boolean forAll2(Func2 f, Iterable j) { return Functional.forAll2(f,j,i); }
            public final U fold(final Func2 f, final U seed) { return Functional.fold(f, seed, i); }
            public final List toList() { return Functional.toList(i); }
            public Object[] toArray() { return Functional.toArray(i); }
            public Set toSet() { return Functional.toSet(i); }
            public  Map toDictionary(Func keyFn, Func valueFn) { return Functional.toDictionary(keyFn,valueFn,i); }
            public T last() { return Functional.last(i); }
            public final Iterable2 sortWith(final Comparator f) { return create(Functional.sortWith(f,toList())); }
            public final Iterable2 concat(final Iterable2 list2) { return create(Functional.seq.concat(i,list2));}
            public final T find(final Func f) { return Functional.find(f,i);}
            public int findIndex(final Func f) { return Functional.findIndex(f,i);}
            public final B pick(final Func> f){return Functional.pick(f,i);}
            public final Iterable2 take(final int howMany){return create(Functional.seq.take(howMany, i));}
            public Iterable2 skip(int howMany) { return create(Functional.seq.skip(howMany, i)); }
            public String join(String delimiter) { return Functional.join(delimiter,i); }
            public T findLast(Func f) { return Functional.findLast(f,i); }
            public Pair, List> partition(Func f) { return Functional.partition(f, i); }
            public final Iterable2> zip(final Iterable2 l2) { return create(Functional.seq.zip(i, l2));}
            //public final org.javatuples.Pair,List> unzip(){return Functional.unzip(i);}
            public final Iterable2> zip3(final Iterable l2, final Iterable l3){return create(Functional.seq.zip3(i, l2, l3));}

            public final Iterable2 collect(final Func> f){return create(Functional.seq.collect(f, i));}
            public U in(final Func, U> f){ return f.apply(this); }

            public  Map> groupBy(Func keyFn) { return Functional.groupBy(keyFn,i); }
        };
    }

    public static final Iterable2 createEmpty()
    {
        return new EmptyList();
    }

    private static final class EmptyList implements Iterable2
    {
        public Iterator iterator() { return new Iterator() {
            @Override public boolean hasNext() { return false; }
            @Override public T next() { throw new java.util.NoSuchElementException(); }
            @Override public void remove() { }
        }; }

        public final Iterable2 filter(final Func f) { return this; }
        public final Iterable2 map(final Func f) { return new EmptyList(); }
        public  Iterable2 mapi(Func2 f) { return new EmptyList();}
        public final Iterable2 choose(final Func> f) { return new EmptyList(); }
        public final boolean exists(final Func f) { return false; }
        public final boolean forAll(final Func f) { return false; }
        public  boolean forAll2(Func2 f, Iterable input1) { return false; }
        public final U fold(final Func2 f, final U seed) { return seed; }
        public final List toList() { return Functional.toList(this); }
        public Object[] toArray() { return Functional.toArray(this); }
        public Set toSet() { return Functional.toSet(this); }
        public  Map toDictionary(Func keyFn, Func valueFn) { return Functional.toDictionary(keyFn,valueFn,this); }
        public T last() { throw new NoSuchElementException(); }
        public final Iterable2 sortWith(final Comparator f) { return this; }
        public final Iterable2 concat(final Iterable2 list2) { return list2;}
        public final T find(final Func f) { throw new NoSuchElementException();}
        public int findIndex(final Func f) { throw new NoSuchElementException();}
        public final B pick(final Func> f){throw new NoSuchElementException();}
        public final Iterable2 take(final int howMany){return this;}
        public Iterable2 skip(int howMany) { return this; }
        public String join(String delimiter) { return Functional.join(delimiter,this); }
        public T findLast(Func f) { throw new NoSuchElementException(); }
        public Pair, List> partition(Func f) { return Functional.partition(f,this); }

        public final Iterable2> zip(final Iterable2 l2) { throw new IllegalArgumentException("Iterable2.zip: It is not possible to zip an empty list with a non-empty list");}
        //public final org.javatuples.Pair,List> unzip(){return Functional.unzip(i);}
        public final Iterable2> zip3(final Iterable l2, final Iterable l3){throw new IllegalArgumentException("Iterable2.zip3: It is not possible to zip an empty list with a non-empty list");}

        public final Iterable2 collect(final Func> f){return new EmptyList();}
        public U in(final Func, U> f){ return f.apply(this); }

        public  Map> groupBy(Func keyFn) { return Collections.EMPTY_MAP; }

        public boolean equals(Object o) { return false; }

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

        @Override
        public String toString() { return "()"; }
    }

    public static final Iterable2 init(Func f, int howMany) { return create(Functional.seq.init(f, howMany)); }
    public final static Iterable2 init(final Func f) { return create(Functional.seq.init(f));}
    @SafeVarargs
    public static Iterable2 asList(T... a) { return create(Arrays.asList(a)); }
}