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.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 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 final U fold(final Func2 f, final U seed) { return Functional.fold(f, seed, i); }
            public final List toList() { return Functional.toList(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.take(howMany, i));}

            public final Iterable2> zip(final Iterable2 l2) { return create(Functional.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.zip3(i,l2,l3));}

            public final Iterable2 collect(final Func> f){return create(Functional.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 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 final U fold(final Func2 f, final U seed) { return seed; }
        public final List toList() { return Functional.toList(this); }
        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 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)); }
}