See More

package fj.data; import fj.Effect; import fj.F; import static fj.P.p; import fj.Function; import fj.P1; import fj.P2; import static fj.data.List.list; import static fj.data.Option.some; import java.util.ArrayList; import java.util.BitSet; import java.util.EnumSet; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Stack; import java.util.TreeSet; import java.util.Vector; import java.util.Iterator; import java.util.NoSuchElementException; import static java.util.EnumSet.copyOf; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.SynchronousQueue; /** * Functions that convert between types from the core Java API. * * @version %build.number% */ public final class Java { private Java() { throw new UnsupportedOperationException(); } // BEGIN List -> /** * A function that converts lists to array lists. * * @return A function that converts lists to array lists. */ public static F, ArrayList> List_ArrayList() { return new F, ArrayList>() { public ArrayList f(final List as) { return new ArrayList(as.toCollection()); } }; } /** * A function that converts lists to bit sets. */ public static final F, BitSet> List_BitSet = new F, BitSet>() { public BitSet f(final List bs) { final BitSet s = new BitSet(bs.length()); bs.zipIndex().foreach(new Effect>() { public void e(final P2 bi) { s.set(bi._2(), bi._1()); } }); return s; } }; /** * A function that converts lists to array enum sets. * * @return A function that converts lists to enum sets. */ public static > F, EnumSet> List_EnumSet() { return new F, EnumSet>() { public EnumSet f(final List as) { return copyOf(as.toCollection()); } }; } /** * A function that converts lists to hash sets. * * @return A function that converts lists to hash sets. */ public static F, HashSet> List_HashSet() { return new F, HashSet>() { public HashSet f(final List as) { return new HashSet(as.toCollection()); } }; } /** * A function that converts lists to linked hash sets. * * @return A function that converts lists to linked hash sets. */ public static F, LinkedHashSet> List_LinkedHashSet() { return new F, LinkedHashSet>() { public LinkedHashSet f(final List as) { return new LinkedHashSet(as.toCollection()); } }; } /** * A function that converts lists to linked lists. * * @return A function that converts lists to linked lists. */ public static F, LinkedList> List_LinkedList() { return new F, LinkedList>() { public LinkedList f(final List as) { return new LinkedList(as.toCollection()); } }; } /** * A function that converts lists to priority queues. * * @return A function that converts lists to priority queues. */ public static F, PriorityQueue> List_PriorityQueue() { return new F, PriorityQueue>() { public PriorityQueue f(final List as) { return new PriorityQueue(as.toCollection()); } }; } /** * A function that converts lists to stacks. * * @return A function that converts lists to stacks. */ public static F, Stack> List_Stack() { return new F, Stack>() { public Stack f(final List as) { final Stack s = new Stack(); s.addAll(as.toCollection()); return s; } }; } /** * A function that converts lists to stacks. * * @return A function that converts lists to stacks. */ public static F, TreeSet> List_TreeSet() { return new F, TreeSet>() { public TreeSet f(final List as) { return new TreeSet(as.toCollection()); } }; } /** * A function that converts lists to vectors. * * @return A function that converts lists to vectors. */ public static F, Vector> List_Vector() { return new F, Vector>() { @SuppressWarnings({"UseOfObsoleteCollectionType"}) public Vector f(final List as) { return new Vector(as.toCollection()); } }; } /** * A function that converts lists to array blocking queue. * * @param fair The argument to pass to the constructor of the array blocking queue. * @return A function that converts lists to array blocking queue. */ public static F, ArrayBlockingQueue> List_ArrayBlockingQueue(final boolean fair) { return new F, ArrayBlockingQueue>() { public ArrayBlockingQueue f(final List as) { return new ArrayBlockingQueue(as.length(), fair, as.toCollection()); } }; } /** * A function that converts lists to concurrent linked queues. * * @return A function that converts lists to concurrent linked queues. */ public static F, ConcurrentLinkedQueue> List_ConcurrentLinkedQueue() { return new F, ConcurrentLinkedQueue>() { public ConcurrentLinkedQueue f(final List as) { return new ConcurrentLinkedQueue(as.toCollection()); } }; } /** * A function that converts lists to copy on write array lists. * * @return A function that converts lists to copy on write array lists. */ public static F, CopyOnWriteArrayList> List_CopyOnWriteArrayList() { return new F, CopyOnWriteArrayList>() { public CopyOnWriteArrayList f(final List as) { return new CopyOnWriteArrayList(as.toCollection()); } }; } /** * A function that converts lists to copy on write array sets. * * @return A function that converts lists to copy on write array sets. */ public static F, CopyOnWriteArraySet> List_CopyOnWriteArraySet() { return new F, CopyOnWriteArraySet>() { public CopyOnWriteArraySet f(final List as) { return new CopyOnWriteArraySet(as.toCollection()); } }; } /** * A function that converts lists to delay queues. * * @return A function that converts lists to delay queues. */ public static F, DelayQueue> List_DelayQueue() { return new F, DelayQueue>() { public DelayQueue f(final List as) { return new DelayQueue(as.toCollection()); } }; } /** * A function that converts lists to linked blocking queues. * * @return A function that converts lists to linked blocking queues. */ public static F, LinkedBlockingQueue> List_LinkedBlockingQueue() { return new F, LinkedBlockingQueue>() { public LinkedBlockingQueue f(final List as) { return new LinkedBlockingQueue(as.toCollection()); } }; } /** * A function that converts lists to priority blocking queues. * * @return A function that converts lists to priority blocking queues. */ public static F, PriorityBlockingQueue> List_PriorityBlockingQueue() { return new F, PriorityBlockingQueue>() { public PriorityBlockingQueue f(final List as) { return new PriorityBlockingQueue(as.toCollection()); } }; } /** * A function that converts lists to synchronous queues. * * @param fair The argument to pass to the constructor of the synchronous queue. * @return A function that converts lists to synchronous queues. */ public static F, SynchronousQueue> List_SynchronousQueue(final boolean fair) { return new F, SynchronousQueue>() { public SynchronousQueue f(final List as) { final SynchronousQueue q = new SynchronousQueue(fair); q.addAll(as.toCollection()); return q; } }; } // END List -> // BEGIN Array -> /** * A function that converts arrays to array lists. * * @return A function that converts arrays to array lists. */ public static F, ArrayList> Array_ArrayList() { return new F, ArrayList>() { public ArrayList f(final Array as) { return new ArrayList(as.toCollection()); } }; } /** * A function that converts arrays to bit sets. */ public static final F, BitSet> Array_BitSet = new F, BitSet>() { public BitSet f(final Array bs) { final BitSet s = new BitSet(bs.length()); bs.zipIndex().foreach(new Effect>() { public void e(final P2 bi) { s.set(bi._2(), bi._1()); } }); return s; } }; /** * A function that converts arrays to enum sets. * * @return A function that converts arrays to enum sets. */ public static > F, EnumSet> Array_EnumSet() { return new F, EnumSet>() { public EnumSet f(final Array as) { return copyOf(as.toCollection()); } }; } /** * A function that converts arrays to hash sets. * * @return A function that converts arrays to hash sets. */ public static F, HashSet> Array_HashSet() { return new F, HashSet>() { public HashSet f(final Array as) { return new HashSet(as.toCollection()); } }; } /** * A function that converts arrays to linked hash sets. * * @return A function that converts arrays to linked hash sets. */ public static F, LinkedHashSet> Array_LinkedHashSet() { return new F, LinkedHashSet>() { public LinkedHashSet f(final Array as) { return new LinkedHashSet(as.toCollection()); } }; } /** * A function that converts arrays to linked lists. * * @return A function that converts arrays to linked lists. */ public static F, LinkedList> Array_LinkedList() { return new F, LinkedList>() { public LinkedList f(final Array as) { return new LinkedList(as.toCollection()); } }; } /** * A function that converts arrays to priority queues. * * @return A function that converts arrays to priority queues. */ public static F, PriorityQueue> Array_PriorityQueue() { return new F, PriorityQueue>() { public PriorityQueue f(final Array as) { return new PriorityQueue(as.toCollection()); } }; } /** * A function that converts arrays to stacks. * * @return A function that converts arrays to stacks. */ public static F, Stack> Array_Stack() { return new F, Stack>() { public Stack f(final Array as) { final Stack s = new Stack(); s.addAll(as.toCollection()); return s; } }; } /** * A function that converts arrays to tree sets. * * @return A function that converts arrays to tree sets. */ public static F, TreeSet> Array_TreeSet() { return new F, TreeSet>() { public TreeSet f(final Array as) { return new TreeSet(as.toCollection()); } }; } /** * A function that converts arrays to vectors. * * @return A function that converts arrays to vectors. */ public static F, Vector> Array_Vector() { return new F, Vector>() { @SuppressWarnings({"UseOfObsoleteCollectionType"}) public Vector f(final Array as) { return new Vector(as.toCollection()); } }; } /** * A function that converts arrays to array blocking queues. * * @param fair The argument to pass to the constructor of the array blocking queue. * @return A function that converts arrays to array blocking queues. */ public static F, ArrayBlockingQueue> Array_ArrayBlockingQueue(final boolean fair) { return new F, ArrayBlockingQueue>() { public ArrayBlockingQueue f(final Array as) { return new ArrayBlockingQueue(as.length(), fair, as.toCollection()); } }; } /** * A function that converts arrays to concurrent linked queues. * * @return A function that converts arrays to concurrent linked queues. */ public static F, ConcurrentLinkedQueue> Array_ConcurrentLinkedQueue() { return new F, ConcurrentLinkedQueue>() { public ConcurrentLinkedQueue f(final Array as) { return new ConcurrentLinkedQueue(as.toCollection()); } }; } /** * A function that converts arrays to copy on write array lists. * * @return A function that converts arrays to copy on write array lists. */ public static F, CopyOnWriteArrayList> Array_CopyOnWriteArrayList() { return new F, CopyOnWriteArrayList>() { public CopyOnWriteArrayList f(final Array as) { return new CopyOnWriteArrayList(as.toCollection()); } }; } /** * A function that converts arrays to copy on write array sets. * * @return A function that converts arrays to copy on write array sets. */ public static F, CopyOnWriteArraySet> Array_CopyOnWriteArraySet() { return new F, CopyOnWriteArraySet>() { public CopyOnWriteArraySet f(final Array as) { return new CopyOnWriteArraySet(as.toCollection()); } }; } /** * A function that converts arrays to delay queues. * * @return A function that converts arrays to delay queues. */ public static F, DelayQueue> Array_DelayQueue() { return new F, DelayQueue>() { public DelayQueue f(final Array as) { return new DelayQueue(as.toCollection()); } }; } /** * A function that converts arrays to linked blocking queues. * * @return A function that converts arrays to linked blocking queues. */ public static F, LinkedBlockingQueue> Array_LinkedBlockingQueue() { return new F, LinkedBlockingQueue>() { public LinkedBlockingQueue f(final Array as) { return new LinkedBlockingQueue(as.toCollection()); } }; } /** * A function that converts arrays to priority blocking queues. * * @return A function that converts arrays to priority blocking queues. */ public static F, PriorityBlockingQueue> Array_PriorityBlockingQueue() { return new F, PriorityBlockingQueue>() { public PriorityBlockingQueue f(final Array as) { return new PriorityBlockingQueue(as.toCollection()); } }; } /** * A function that converts arrays to synchronous queues. * * @param fair The argument to pass to the constructor of the synchronous queue. * @return A function that converts arrays to synchronous queues. */ public static F, SynchronousQueue> Array_SynchronousQueue(final boolean fair) { return new F, SynchronousQueue>() { public SynchronousQueue f(final Array as) { final SynchronousQueue q = new SynchronousQueue(fair); q.addAll(as.toCollection()); return q; } }; } // END Array -> // BEGIN Stream -> /** * A function that converts streams to iterable. * * @return A function that converts streams to iterable. */ public static F, Iterable> Stream_Iterable() { return new F, Iterable>() { public Iterable f(final Stream as) { return new Iterable() { public Iterator iterator() { return new Iterator() { private Stream x = as; public boolean hasNext() { return x.isNotEmpty(); } public A next() { if (x.isEmpty()) throw new NoSuchElementException("Empty iterator"); else { final A a = x.head(); x = x.tail()._1(); return a; } } public void remove() { throw new UnsupportedOperationException(); } }; } }; } }; } /** * A function that converts streams to array lists. * * @return A function that converts streams to array lists. */ public static F, ArrayList> Stream_ArrayList() { return new F, ArrayList>() { public ArrayList f(final Stream as) { return new ArrayList(as.toCollection()); } }; } /** * A function that converts streams to bit sets. */ public static final F, BitSet> Stream_BitSet = new F, BitSet>() { public BitSet f(final Stream bs) { final BitSet s = new BitSet(bs.length()); bs.zipIndex().foreach(new Effect>() { public void e(final P2 bi) { s.set(bi._2(), bi._1()); } }); return s; } }; /** * A function that converts streams to enum sets. * * @return A function that converts streams to enum sets. */ public static > F, EnumSet> Stream_EnumSet() { return new F, EnumSet>() { public EnumSet f(final Stream as) { return copyOf(as.toCollection()); } }; } /** * A function that converts streams to hash sets. * * @return A function that converts streams to hash sets. */ public static F, HashSet> Stream_HashSet() { return new F, HashSet>() { public HashSet f(final Stream as) { return new HashSet(as.toCollection()); } }; } /** * A function that converts streams to linked hash sets. * * @return A function that converts streams to linked hash sets. */ public static F, LinkedHashSet> Stream_LinkedHashSet() { return new F, LinkedHashSet>() { public LinkedHashSet f(final Stream as) { return new LinkedHashSet(as.toCollection()); } }; } /** * A function that converts streams to linked lists. * * @return A function that converts streams to linked lists. */ public static F, LinkedList> Stream_LinkedList() { return new F, LinkedList>() { public LinkedList f(final Stream as) { return new LinkedList(as.toCollection()); } }; } /** * A function that converts streams to priority queues. * * @return A function that converts streams to priority queues. */ public static F, PriorityQueue> Stream_PriorityQueue() { return new F, PriorityQueue>() { public PriorityQueue f(final Stream as) { return new PriorityQueue(as.toCollection()); } }; } /** * A function that converts streams to stacks. * * @return A function that converts streams to stacks. */ public static F, Stack> Stream_Stack() { return new F, Stack>() { public Stack f(final Stream as) { final Stack s = new Stack(); s.addAll(as.toCollection()); return s; } }; } /** * A function that converts streams to tree sets. * * @return A function that converts streams to tree sets. */ public static F, TreeSet> Stream_TreeSet() { return new F, TreeSet>() { public TreeSet f(final Stream as) { return new TreeSet(as.toCollection()); } }; } /** * A function that converts streams to vectors. * * @return A function that converts streams to vectors. */ public static F, Vector> Stream_Vector() { return new F, Vector>() { @SuppressWarnings({"UseOfObsoleteCollectionType"}) public Vector f(final Stream as) { return new Vector(as.toCollection()); } }; } /** * A function that converts streams to array blocking queues. * * @param fair The argument to pass to the constructor of the array blocking queue. * @return A function that converts streams to array blocking queues. */ public static F, ArrayBlockingQueue> Stream_ArrayBlockingQueue(final boolean fair) { return new F, ArrayBlockingQueue>() { public ArrayBlockingQueue f(final Stream as) { return new ArrayBlockingQueue(as.length(), fair, as.toCollection()); } }; } /** * A function that converts streams to concurrent linked queues. * * @return A function that converts streams to concurrent linked queues. */ public static F, ConcurrentLinkedQueue> Stream_ConcurrentLinkedQueue() { return new F, ConcurrentLinkedQueue>() { public ConcurrentLinkedQueue f(final Stream as) { return new ConcurrentLinkedQueue(as.toCollection()); } }; } /** * A function that converts streams to copy on write array lists. * * @return A function that converts streams to copy on write array lists. */ public static F, CopyOnWriteArrayList> Stream_CopyOnWriteArrayList() { return new F, CopyOnWriteArrayList>() { public CopyOnWriteArrayList f(final Stream as) { return new CopyOnWriteArrayList(as.toCollection()); } }; } /** * A function that converts streams to copy on write array sets. * * @return A function that converts streams to copy on write array sets. */ public static F, CopyOnWriteArraySet> Stream_CopyOnWriteArraySet() { return new F, CopyOnWriteArraySet>() { public CopyOnWriteArraySet f(final Stream as) { return new CopyOnWriteArraySet(as.toCollection()); } }; } /** * A function that converts streams to delay queues. * * @return A function that converts streams to delay queues. */ public static F, DelayQueue> Stream_DelayQueue() { return new F, DelayQueue>() { public DelayQueue f(final Stream as) { return new DelayQueue(as.toCollection()); } }; } /** * A function that converts streams to linked blocking queues. * * @return A function that converts streams to linked blocking queues. */ public static F, LinkedBlockingQueue> Stream_LinkedBlockingQueue() { return new F, LinkedBlockingQueue>() { public LinkedBlockingQueue f(final Stream as) { return new LinkedBlockingQueue(as.toCollection()); } }; } /** * A function that converts streams to priority blocking queues. * * @return A function that converts streams to priority blocking queues. */ public static F, PriorityBlockingQueue> Stream_PriorityBlockingQueue() { return new F, PriorityBlockingQueue>() { public PriorityBlockingQueue f(final Stream as) { return new PriorityBlockingQueue(as.toCollection()); } }; } /** * A function that converts streams to synchronous queues. * * @param fair The argument to pass to the constructor of the synchronous queue. * @return A function that converts streams to synchronous queues. */ public static F, SynchronousQueue> Stream_SynchronousQueue(final boolean fair) { return new F, SynchronousQueue>() { public SynchronousQueue f(final Stream as) { final SynchronousQueue q = new SynchronousQueue(fair); q.addAll(as.toCollection()); return q; } }; } // END Stream -> // BEGIN Option -> /** * A function that converts options to array lists. * * @return A function that converts options to array lists. */ public static F> Option_ArrayList() { return new F>() { public ArrayList f(final Option as) { return new ArrayList(as.toCollection()); } }; } /** * A function that converts options to bit sets. */ public static final F> F> Option_EnumSet() { return new F>() { public EnumSet f(final Option as) { return copyOf(as.toCollection()); } }; } /** * A function that converts options to hash sets. * * @return A function that converts options to hash sets. */ public static F> Option_HashSet() { return new F>() { public HashSet f(final Option as) { return new HashSet(as.toCollection()); } }; } /** * A function that converts options to linked hash sets. * * @return A function that converts options to linked hash sets. */ public static F> Option_LinkedHashSet() { return new F>() { public LinkedHashSet f(final Option as) { return new LinkedHashSet(as.toCollection()); } }; } /** * A function that converts options to linked lists. * * @return A function that converts options to linked lists. */ public static F> Option_LinkedList() { return new F>() { public LinkedList f(final Option as) { return new LinkedList(as.toCollection()); } }; } /** * A function that converts options to priority queues. * * @return A function that converts options to priority queues. */ public static F> Option_PriorityQueue() { return new F