diff --git a/src/org/ggp/base/util/gdl/grammar/GdlPool.java b/src/org/ggp/base/util/gdl/grammar/GdlPool.java index 1e8968cc3..07f91f44f 100644 --- a/src/org/ggp/base/util/gdl/grammar/GdlPool.java +++ b/src/org/ggp/base/util/gdl/grammar/GdlPool.java @@ -4,13 +4,13 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.MapMaker; public final class GdlPool @@ -25,8 +25,8 @@ public final class GdlPool private static final ConcurrentMap variablePool = new ConcurrentHashMap(); private static final ConcurrentMap constantPool = new ConcurrentHashMap(); private static final Map constantCases = new TreeMap(String.CASE_INSENSITIVE_ORDER); - private static final Map variableCases = new TreeMap(String.CASE_INSENSITIVE_ORDER); - + private static final Map variableCases = new TreeMap(String.CASE_INSENSITIVE_ORDER); + // Controls whether we normalize the case of incoming constants and variables. public static boolean caseSensitive = true; @@ -37,8 +37,8 @@ public final class GdlPool // as if one had attempted to create the GdlConstant "true", regardless of whether the // game-specific constants are case-sensitive or not. These special keywords are never // sent over the network in PLAY requests and responses, so this should be safe. - public static final HashSet keywords = new HashSet(Arrays.asList( - new String[] {"init","true","next","role","does","goal","legal","terminal","base","input","_"})); + public static final ImmutableSet KEYWORDS = ImmutableSet.of( + "init","true","next","role","does","goal","legal","terminal","base","input","_"); public static final GdlConstant BASE = getConstant("base"); public static final GdlConstant DOES = getConstant("does"); public static final GdlConstant GOAL = getConstant("goal"); @@ -58,10 +58,10 @@ public final class GdlPool /** * Drains the contents of the GdlPool. Useful to control memory usage * once you have finished playing a large game. - * + * * WARNING: Should only be called *between games*. */ - public static void drainPool() { + public static void drainPool() { distinctPool.clear(); functionPool.clear(); notPool.clear(); @@ -69,9 +69,9 @@ public static void drainPool() { propositionPool.clear(); relationPool.clear(); rulePool.clear(); - variablePool.clear(); + variablePool.clear(); variableCases.clear(); - + // When draining the pool between matches, we still need to preserve the keywords // since there are global references to them. For example, the Prover state machine // has a reference to the GdlConstant "true", and that reference must still point @@ -80,25 +80,25 @@ public static void drainPool() { // are set aside and returned to the pool after all of the other constants (which // were game-specific) have been drained. Map keywordConstants = new HashMap(); - for (String keyword : keywords) { + for (String keyword : KEYWORDS) { keywordConstants.put(keyword, GdlPool.getConstant(keyword)); } constantPool.clear(); - constantCases.clear(); + constantCases.clear(); for (Map.Entry keywordEntry : keywordConstants.entrySet()) { constantCases.put(keywordEntry.getKey(), keywordEntry.getKey()); constantPool.put(keywordEntry.getKey(), keywordEntry.getValue()); } } - + /** * If the pool does not have a mapping for the given key, adds a mapping from key to value * to the pool. - * + * * Note that even if you've checked to make sure that the pool doesn't contain the key, * you still shouldn't assume that this method actually inserts the given value, since * this class is accessed by multiple threads simultaneously. - * + * * @return the value mapped to by key in the pool */ private static V addToPool(K key, V value, ConcurrentMap pool) { @@ -111,7 +111,7 @@ private static V addToPool(K key, V value, ConcurrentMap pool) { public static GdlConstant getConstant(String value) { - if (keywords.contains(value.toLowerCase())) { + if (KEYWORDS.contains(value.toLowerCase())) { value = value.toLowerCase(); } if (!caseSensitive) { @@ -121,13 +121,13 @@ public static GdlConstant getConstant(String value) constantCases.put(value, value); } } - - GdlConstant ret = constantPool.get(value); + + GdlConstant ret = constantPool.get(value); if(ret == null) - ret = addToPool(value, new GdlConstant(value), constantPool); + ret = addToPool(value, new GdlConstant(value), constantPool); return ret; } - + public static GdlVariable getVariable(String name) { if (!caseSensitive) { @@ -136,24 +136,24 @@ public static GdlVariable getVariable(String name) } else { variableCases.put(name, name); } - } - - GdlVariable ret = variablePool.get(name); + } + + GdlVariable ret = variablePool.get(name); if(ret == null) ret = addToPool(name, new GdlVariable(name), variablePool); return ret; - } + } public static GdlDistinct getDistinct(GdlTerm arg1, GdlTerm arg2) { ConcurrentMap bucket = distinctPool.get(arg1); if(bucket == null) bucket = addToPool(arg1, new ConcurrentHashMap(), distinctPool); - + GdlDistinct ret = bucket.get(arg2); if(ret == null) ret = addToPool(arg2, new GdlDistinct(arg1, arg2), bucket); - + return ret; } @@ -181,7 +181,7 @@ public static GdlFunction getFunction(GdlConstant name, List body) body = getImmutableCopy(body); ret = addToPool(body, new GdlFunction(name, body), bucket); } - + return ret; } @@ -190,7 +190,7 @@ public static GdlNot getNot(GdlLiteral body) GdlNot ret = notPool.get(body); if(ret == null) ret = addToPool(body, new GdlNot(body), notPool); - + return ret; } @@ -206,7 +206,7 @@ public static GdlOr getOr(List disjuncts) disjuncts = getImmutableCopy(disjuncts); ret = addToPool(disjuncts, new GdlOr(disjuncts), orPool); } - + return ret; } @@ -215,7 +215,7 @@ public static GdlProposition getProposition(GdlConstant name) GdlProposition ret = propositionPool.get(name); if(ret == null) ret = addToPool(name, new GdlProposition(name), propositionPool); - + return ret; } @@ -243,7 +243,7 @@ public static GdlRelation getRelation(GdlConstant name, List body) body = getImmutableCopy(body); ret = addToPool(body, new GdlRelation(name, body), bucket); } - + return ret; } @@ -263,16 +263,16 @@ public static GdlRule getRule(GdlSentence head, List body) ConcurrentMap, GdlRule> bucket = rulePool.get(head); if(bucket == null) bucket = addToPool(head, new ConcurrentHashMap, GdlRule>(), rulePool); - + GdlRule ret = bucket.get(body); if(ret == null) { body = getImmutableCopy(body); ret = addToPool(body, new GdlRule(head, body), bucket); } - + return ret; } - + private static List getImmutableCopy(List list) { return Collections.unmodifiableList(new ArrayList(list)); } @@ -297,7 +297,7 @@ public static Gdl immerse(Gdl foreignGdl) { List rval = new ArrayList(); for(int i=0; i rval = new ArrayList(); for(int i=0; i rval = new ArrayList(); for(int i=0; i rval = new ArrayList(); for(int i=0; i