fj.data.Java Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.data;
import fj.*;
import static fj.P.p;
import fj.function.Effect1;
import static fj.data.List.list;
import static fj.data.Option.some;
import java.util.Arrays;
import java.util.Collection;
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 as -> new ArrayList(as.toCollection());
}
/**
* A function that converts lists to bit sets.
*/
public static final F, BitSet> List_BitSet = bs -> {
final BitSet s = new BitSet(bs.length());
bs.zipIndex().foreachDoEffect(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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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 as -> new ArrayList(as.toCollection());
}
/**
* A function that converts arrays to bit sets.
*/
public static final F, BitSet> Array_BitSet = bs -> {
final BitSet s = new BitSet(bs.length());
bs.zipIndex().foreachDoEffect(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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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 as -> () -> 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 as -> new ArrayList(as.toCollection());
}
/**
* A function that converts streams to bit sets.
*/
public static final F, BitSet> Stream_BitSet = bs -> {
final BitSet s = new BitSet(bs.length());
bs.zipIndex().foreachDoEffect(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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 as -> 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 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