![JAR search and dependency download from the Maven repository](/logo.png)
edu.stanford.nlp.util.Generics Maven / Gradle / Ivy
package edu.stanford.nlp.util;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
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 extends E> c) {
return new ArrayList(c);
}
public static LinkedList newLinkedList() {
return new LinkedList();
}
public static LinkedList newLinkedList(Collection extends E> c) {
return new LinkedList(c);
}
public static Stack newStack() {
return new Stack();
}
public static BinaryHeapPriorityQueue newBinaryHeapPriorityQueue() {
return new BinaryHeapPriorityQueue();
}
public static TreeSet newTreeSet() {
return new TreeSet();
}
public static TreeSet newTreeSet(Comparator super E> comparator) {
return new TreeSet(comparator);
}
public static TreeSet newTreeSet(SortedSet s) {
return new TreeSet(s);
}
public static final String HASH_SET_PROPERTY = "edu.stanford.nlp.hashset.impl";
public static final String HASH_SET_CLASSNAME = System.getProperty(HASH_SET_PROPERTY);
private static final Class> HASH_SET_CLASS = getHashSetClass();
private static final Constructor HASH_SET_SIZE_CONSTRUCTOR = getHashSetSizeConstructor();
private static final Constructor HASH_SET_COLLECTION_CONSTRUCTOR = getHashSetCollectionConstructor();
private static Class getHashSetClass() {
try {
if (HASH_SET_CLASSNAME == null) {
return HashSet.class;
} else {
return Class.forName(HASH_SET_CLASSNAME);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// must be called after HASH_SET_CLASS is defined
private static Constructor getHashSetSizeConstructor() {
try {
return HASH_SET_CLASS.getConstructor(Integer.TYPE);
} catch (Exception e) {
System.err.println("Warning: could not find a constructor for objects of " + HASH_SET_CLASS + " which takes an integer argument. Will use the no argument constructor instead.");
}
return null;
}
// must be called after HASH_SET_CLASS is defined
private static Constructor getHashSetCollectionConstructor() {
try {
return HASH_SET_CLASS.getConstructor(Collection.class);
} catch (Exception e) {
throw new RuntimeException("Error: could not find a constructor for objects of " + HASH_SET_CLASS + " which takes an existing collection argument.", e);
}
}
public static Set newHashSet() {
try {
return ErasureUtils.uncheckedCast(HASH_SET_CLASS.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Set newHashSet(int initialCapacity) {
if (HASH_SET_SIZE_CONSTRUCTOR == null) {
return newHashSet();
}
try {
return ErasureUtils.uncheckedCast(HASH_SET_SIZE_CONSTRUCTOR.newInstance(initialCapacity));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Set newHashSet(Collection extends E> c) {
try {
return ErasureUtils.uncheckedCast(HASH_SET_COLLECTION_CONSTRUCTOR.newInstance(c));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static final String HASH_MAP_PROPERTY = "edu.stanford.nlp.hashmap.impl";
public static final String HASH_MAP_CLASSNAME = System.getProperty(HASH_MAP_PROPERTY);
private static final Class> HASH_MAP_CLASS = getHashMapClass();
private static final Constructor HASH_MAP_SIZE_CONSTRUCTOR = getHashMapSizeConstructor();
private static final Constructor HASH_MAP_FROM_MAP_CONSTRUCTOR = getHashMapFromMapConstructor();
private static Class getHashMapClass() {
try {
if (HASH_MAP_CLASSNAME == null) {
return HashMap.class;
} else {
return Class.forName(HASH_MAP_CLASSNAME);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// must be called after HASH_MAP_CLASS is defined
private static Constructor getHashMapSizeConstructor() {
try {
return HASH_MAP_CLASS.getConstructor(Integer.TYPE);
} catch (Exception e) {
System.err.println("Warning: could not find a constructor for objects of " + HASH_MAP_CLASS + " which takes an integer argument. Will use the no argument constructor instead.");
}
return null;
}
// must be called after HASH_MAP_CLASS is defined
private static Constructor getHashMapFromMapConstructor() {
try {
return HASH_MAP_CLASS.getConstructor(Map.class);
} catch (Exception e) {
throw new RuntimeException("Error: could not find a constructor for objects of " + HASH_MAP_CLASS + " which takes an existing Map argument.", e);
}
}
/* Maps */
public static Map newHashMap() {
try {
return ErasureUtils.uncheckedCast(HASH_MAP_CLASS.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Map newHashMap(int initialCapacity) {
if (HASH_MAP_SIZE_CONSTRUCTOR == null) {
return newHashMap();
}
try {
return ErasureUtils.uncheckedCast(HASH_MAP_SIZE_CONSTRUCTOR.newInstance(initialCapacity));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Map newHashMap(Map extends K,? extends V> m) {
try {
return ErasureUtils.uncheckedCast(HASH_MAP_FROM_MAP_CONSTRUCTOR.newInstance(m));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static IdentityHashMap newIdentityHashMap() {
return new IdentityHashMap();
}
public static Set newIdentityHashSet() {
return Collections.newSetFromMap(Generics.newIdentityHashMap());
}
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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy