edu.stanford.nlp.util.Generics Maven / Gradle / Ivy
package edu.stanford.nlp.util;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import edu.stanford.nlp.util.concurrent.SynchronizedInterner;
/**
* A collection of utilities to make dealing with Java generics less
* painful and verbose. For example, rather than declaring
*
*
* {@code Map>> = new HashMap>>()}
*
*
* you just call Generics.newHashMap()
:
*
*
* {@code Map>> = Generics.newHashMap()}
*
*
* Java type-inference will almost always just do the right thing
* (every once in a while, the compiler will get confused before you do,
* so you might still occasionally have to specify the appropriate types).
*
* This class is based on the examples in Brian Goetz's article
* Java
* theory and practice: The pseudo-typedef antipattern.
*
* @author Ilya Sherman
*/
public class Generics {
private Generics() {} // static class
/* Collections */
public static ArrayList newArrayList() {
return new ArrayList();
}
public static ArrayList newArrayList(int size) {
return new ArrayList(size);
}
public static ArrayList newArrayList(Collection c) {
return new ArrayList(c);
}
public static LinkedList newLinkedList() {
return new LinkedList();
}
public static LinkedList newLinkedList(Collection c) {
return new LinkedList(c);
}
public static HashSet newHashSet() {
return new HashSet();
}
public static HashSet newHashSet(int initialCapacity) {
return new HashSet(initialCapacity);
}
public static HashSet newHashSet(Collection c) {
return new HashSet(c);
}
public static TreeSet newTreeSet() {
return new TreeSet();
}
public static TreeSet newTreeSet(Comparator comparator) {
return new TreeSet(comparator);
}
public static TreeSet newTreeSet(SortedSet s) {
return new TreeSet(s);
}
public static Stack newStack() {
return new Stack();
}
public static BinaryHeapPriorityQueue newBinaryHeapPriorityQueue() {
return new BinaryHeapPriorityQueue();
}
/* Maps */
public static HashMap newHashMap() {
return new HashMap();
}
public static HashMap newHashMap(int initialCapacity) {
return new HashMap(initialCapacity);
}
public static HashMap newHashMap(Map m) {
return new HashMap(m);
}
public static WeakHashMap newWeakHashMap() {
return new WeakHashMap();
}
public static ConcurrentHashMap newConcurrentHashMap() {
return new ConcurrentHashMap();
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity) {
return new ConcurrentHashMap(initialCapacity);
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
return new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel);
}
public static TreeMap newTreeMap() {
return new TreeMap();
}
public static Index newIndex() {
return new HashIndex();
}
/* Other */
public static Pair newPair(T1 first, T2 second) {
return new Pair(first, second);
}
public static Triple newTriple(T1 first, T2 second, T3 third) {
return new Triple(first, second, third);
}
public static Interner newInterner() {
return new Interner();
}
public static SynchronizedInterner newSynchronizedInterner(Interner interner) {
return new SynchronizedInterner(interner);
}
public static SynchronizedInterner newSynchronizedInterner(Interner interner,
Object mutex) {
return new SynchronizedInterner(interner, mutex);
}
public static WeakReference newWeakReference(T referent) {
return new WeakReference(referent);
}
}