edu.stanford.nlp.util.MapFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-parser Show documentation
Show all versions of stanford-parser Show documentation
Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.
package edu.stanford.nlp.util;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* A factory class for vending different sorts of Maps.
*
* @author Dan Klein ([email protected])
* @author Kayur Patel (kdpatel@cs)
*/
public abstract class MapFactory implements Serializable {
// allow people to write subclasses
protected MapFactory() {
}
private static final long serialVersionUID = 4529666940763477360L;
@SuppressWarnings("unchecked")
public static final MapFactory HASH_MAP_FACTORY = new HashMapFactory();
@SuppressWarnings("unchecked")
public static final MapFactory IDENTITY_HASH_MAP_FACTORY = new IdentityHashMapFactory();
@SuppressWarnings("unchecked")
private static final MapFactory WEAK_HASH_MAP_FACTORY = new WeakHashMapFactory();
@SuppressWarnings("unchecked")
private static final MapFactory TREE_MAP_FACTORY = new TreeMapFactory();
@SuppressWarnings("unchecked")
private static final MapFactory LINKED_HASH_MAP_FACTORY = new LinkedHashMapFactory();
@SuppressWarnings("unchecked")
private static final MapFactory ARRAY_MAP_FACTORY = new ArrayMapFactory();
public static final MapFactory CONCURRENT_MAP_FACTORY = new ConcurrentMapFactory();
/** Return a MapFactory that returns a HashMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes a HashMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory hashMapFactory() {
return HASH_MAP_FACTORY;
}
/** Return a MapFactory that returns an IdentityHashMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes a HashMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory identityHashMapFactory() {
return IDENTITY_HASH_MAP_FACTORY;
}
/** Return a MapFactory that returns a WeakHashMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes a WeakHashMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory weakHashMapFactory() {
return WEAK_HASH_MAP_FACTORY;
}
/** Return a MapFactory that returns a TreeMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes an TreeMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory treeMapFactory() {
return TREE_MAP_FACTORY;
}
/**
* Return a MapFactory that returns a TreeMap with the given Comparator.
*/
public static MapFactory treeMapFactory(Comparator super K> comparator) {
return new TreeMapFactory(comparator);
}
/** Return a MapFactory that returns an LinkedHashMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes an LinkedHashMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory linkedHashMapFactory() {
return LINKED_HASH_MAP_FACTORY;
}
/** Return a MapFactory that returns an ArrayMap.
* Implementation note: This method uses the same trick as the methods
* like emptyMap() introduced in the Collections class in JDK1.5 where
* callers can call this method with apparent type safety because this
* method takes the hit for the cast.
*
* @return A MapFactory that makes an ArrayMap.
*/
@SuppressWarnings("unchecked")
public static MapFactory arrayMapFactory() {
return ARRAY_MAP_FACTORY;
}
private static class HashMapFactory extends MapFactory {
private static final long serialVersionUID = -9222344631596580863L;
@Override
public Map newMap() {
return Generics.newHashMap();
}
@Override
public Map newMap(int initCapacity) {
return Generics.newHashMap(initCapacity);
}
@Override
public Set newSet() {
return Generics.newHashSet();
}
@Override
public Map setMap(Map map) {
map = Generics.newHashMap();
return map;
}
@Override
public Map setMap(Map map, int initCapacity) {
map = Generics.newHashMap(initCapacity);
return map;
}
} // end class HashMapFactory
private static class IdentityHashMapFactory extends MapFactory {
private static final long serialVersionUID = -9222344631596580863L;
@Override
public Map newMap() {
return new IdentityHashMap();
}
@Override
public Map newMap(int initCapacity) {
return new IdentityHashMap(initCapacity);
}
@Override
public Set newSet() {
return Collections.newSetFromMap(new IdentityHashMap());
}
@Override
public Map setMap(Map map) {
map = new IdentityHashMap();
return map;
}
@Override
public Map setMap(Map map, int initCapacity) {
map = new IdentityHashMap(initCapacity);
return map;
}
} // end class IdentityHashMapFactory
private static class WeakHashMapFactory extends MapFactory {
private static final long serialVersionUID = 4790014244304941000L;
@Override
public Map newMap() {
return new WeakHashMap();
}
@Override
public Map newMap(int initCapacity) {
return new WeakHashMap(initCapacity);
}
@Override
public Set newSet() {
return Collections.newSetFromMap(new WeakHashMap());
}
@Override
public Map setMap(Map map) {
map = new WeakHashMap();
return map;
}
@Override
public Map setMap(Map map, int initCapacity) {
map = new WeakHashMap(initCapacity);
return map;
}
} // end class WeakHashMapFactory
private static class TreeMapFactory extends MapFactory {
private static final long serialVersionUID = -9138736068025818670L;
private final Comparator super K> comparator;
public TreeMapFactory() {
this.comparator = null;
}
public TreeMapFactory(Comparator super K> comparator) {
this.comparator = comparator;
}
@Override
public Map newMap() {
return comparator == null ? new TreeMap() : new TreeMap(comparator);
}
@Override
public Map newMap(int initCapacity) {
return newMap();
}
@Override
public Set newSet() {
return comparator == null ? new TreeSet() : new TreeSet(comparator);
}
@Override
public Map setMap(Map map) {
if (comparator == null) {
throw new UnsupportedOperationException();
}
map = new TreeMap();
return map;
}
@Override
public Map setMap(Map map, int initCapacity) {
if (comparator == null) {
throw new UnsupportedOperationException();
}
map = new TreeMap();
return map;
}
} // end class TreeMapFactory
private static class LinkedHashMapFactory extends MapFactory {
private static final long serialVersionUID = -9138736068025818671L;
@Override
public Map newMap() {
return new LinkedHashMap();
}
@Override
public Map newMap(int initCapacity) {
return newMap();
}
@Override
public Set newSet() {
return new LinkedHashSet();
}
@Override
public Map setMap(Map map) {
map = new LinkedHashMap();
return map;
}
@Override
public Map setMap(Map map, int initCapacity) {
map = new LinkedHashMap();
return map;
}
} // end class LinkedHashMapFactory
private static class ArrayMapFactory extends MapFactory {
private static final long serialVersionUID = -5855812734715185523L;
@Override
public Map newMap() {
return new ArrayMap();
}
@Override
public Map newMap(int initCapacity) {
return new ArrayMap(initCapacity);
}
@Override
public Set newSet() {
return new ArraySet();
}
@Override
public Map setMap(Map map) {
return new ArrayMap();
}
@Override
public Map setMap(Map map, int initCapacity) {
map = new ArrayMap(initCapacity);
return map;
}
} // end class ArrayMapFactory
private static class ConcurrentMapFactory extends MapFactory {
private static final long serialVersionUID = -5855812734715185523L;
@Override
public Map newMap() {
return new ConcurrentHashMap();
}
@Override
public Map newMap(int initCapacity) {
return new ConcurrentHashMap(initCapacity);
}
@Override
public Set newSet() {
return Collections.newSetFromMap(new ConcurrentHashMap());
}
@Override
public Map setMap(Map map) {
return new ConcurrentHashMap();
}
@Override
public Map setMap(Map map, int initCapacity) {
map = new ConcurrentHashMap(initCapacity);
return map;
}
} // end class ConcurrentMapFactory
/**
* Returns a new non-parameterized map of a particular sort.
*
* @return A new non-parameterized map of a particular sort
*/
public abstract Map newMap();
/**
* Returns a new non-parameterized map of a particular sort with an initial capacity.
*
* @param initCapacity initial capacity of the map
* @return A new non-parameterized map of a particular sort with an initial capacity
*/
public abstract Map newMap(int initCapacity);
/**
* A set with the same K
parameterization of the Maps.
*/
public abstract Set newSet();
/**
* A method to get a parameterized (genericized) map out.
*
* @param map A type-parameterized {@link Map} argument
* @return A {@link Map} with type-parameterization identical to that of
* the argument.
*/
public abstract Map setMap(Map map);
public abstract Map setMap(Map map, int initCapacity);
}