me.shaftesbury.utils.functional.IterableHelper Maven / Gradle / Ivy
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 super T,Boolean> f) { return create(Functional.seq.filter(f,i)); }
public final Iterable2 map(final Func super T,? extends U> f) { return create(Functional.seq.map(f, i)); }
public final Iterable2 choose(final Func super T,Option> f) { return create(Functional.seq.choose(f, i)); }
public final boolean exists(final Func super T,Boolean> f) { return Functional.exists(f, i); }
public final boolean forAll(final Func super T,Boolean> f) { return Functional.forAll(f, i); }
public final U fold(final Func2 super U,? super T,? extends U> 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 super T,Boolean> f) { return Functional.find(f,i);}
public int findIndex(final Func super T,Boolean> f) { return Functional.findIndex(f,i);}
public final B pick(final Func super T,Option> 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 extends U> l2) { return create(Functional.zip(i,l2));}
//public final org.javatuples.Pair,List> unzip(){return Functional.unzip(i);}
public final Iterable2> zip3(final Iterable extends U> l2, final Iterable extends V> l3){return create(Functional.zip3(i,l2,l3));}
public final Iterable2 collect(final Func super T,? extends Iterable> f){return create(Functional.collect(f,i));}
public U in(final Func, U> f){ return f.apply(this); }
public Map> groupBy(Func super T, ? extends U> 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 super T,Boolean> f) { return this; }
public final Iterable2 map(final Func super T,? extends U> f) { return new EmptyList(); }
public final Iterable2 choose(final Func super T,Option> f) { return new EmptyList(); }
public final boolean exists(final Func super T,Boolean> f) { return false; }
public final boolean forAll(final Func super T,Boolean> f) { return false; }
public final U fold(final Func2 super U,? super T,? extends U> 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 super T,Boolean> f) { throw new NoSuchElementException();}
public int findIndex(final Func super T,Boolean> f) { throw new NoSuchElementException();}
public final B pick(final Func super T,Option> f){throw new NoSuchElementException();}
public final Iterable2 take(final int howMany){return this;}
public final Iterable2> zip(final Iterable2 extends U> 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 extends U> l2, final Iterable extends V> 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 super T,? extends Iterable> f){return new EmptyList();}
public U in(final Func, U> f){ return f.apply(this); }
public Map> groupBy(Func super T, ? extends U> 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)); }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy