Author: Thomas Broyer <t.broyer@gmail.com>

Replace uses of Apache Commons with Guava

This is in preparation of the removal of GWTCompiler/GWTShell and their
dependency on Tomcat. With this change, we will then be able to remove
the dependency on Apache Commons altogether.

Change-Id: I40ccea274148ca13624ed0b122eca4bf4018bddc
Review-Link: https://gwt-review.googlesource.com/#/c/1050/

Review by: skybrian@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11342 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/cfg/ModuleDefLoader.java b/dev/core/src/com/google/gwt/dev/cfg/ModuleDefLoader.java
index f7021d3..166723b 100644
--- a/dev/core/src/com/google/gwt/dev/cfg/ModuleDefLoader.java
+++ b/dev/core/src/com/google/gwt/dev/cfg/ModuleDefLoader.java
@@ -22,11 +22,9 @@
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger;
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
 import com.google.gwt.dev.util.xml.ReflectiveParser;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 import com.google.gwt.util.tools.Utility;
 
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceMap;
-
 import java.io.File;
 import java.io.Reader;
 import java.net.URISyntaxException;
@@ -57,9 +55,8 @@
    * tight. The current context class loader used as a key for modules cache.
    * The module's physical name is used as a key inside the cache.
    */
-  @SuppressWarnings("unchecked")
-  private static final Map<ClassLoader, Map<String, ModuleDef>> loadedModulesCaches = new ReferenceMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD);
+  private static final Map<ClassLoader, Map<String, ModuleDef>> loadedModulesCaches =
+      new MapMaker().weakKeys().makeMap();
 
   /**
    * A mapping from effective to physical module names.
@@ -210,12 +207,11 @@
     return moduleDef;
   }
 
-  @SuppressWarnings("unchecked")
   private static Map<String, ModuleDef> getModulesCache() {
     ClassLoader keyClassLoader = Thread.currentThread().getContextClassLoader();
     Map<String, ModuleDef> cache = loadedModulesCaches.get(keyClassLoader);
     if (cache == null) {
-      cache = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT);
+      cache = new MapMaker().softValues().makeMap();
       loadedModulesCaches.put(keyClassLoader, cache);
     }
     return cache;
diff --git a/dev/core/src/com/google/gwt/dev/javac/MemoryUnitCache.java b/dev/core/src/com/google/gwt/dev/javac/MemoryUnitCache.java
index 971a1d8..875ce26 100644
--- a/dev/core/src/com/google/gwt/dev/javac/MemoryUnitCache.java
+++ b/dev/core/src/com/google/gwt/dev/javac/MemoryUnitCache.java
@@ -16,11 +16,8 @@
 package com.google.gwt.dev.javac;
 
 import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceMap;
-
-import java.util.Collections;
 import java.util.Map;
 
 /**
@@ -79,17 +76,13 @@
    * 
    * The key is resource path.
    */
-  @SuppressWarnings("unchecked")
-  protected final Map<String, UnitCacheEntry> unitMap = Collections
-      .synchronizedMap(new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT));
+  protected final Map<String, UnitCacheEntry> unitMap = new MapMaker().softValues().makeMap();
 
   /**
    * References {@link CompilationUnit} objects by {@link ContentId}, which is
    * composed of the type name and a hash on the source code contents.
    */
-  @SuppressWarnings("unchecked")
-  protected final Map<ContentId, UnitCacheEntry> unitMapByContentId = Collections
-      .synchronizedMap(new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT));
+  protected final Map<ContentId, UnitCacheEntry> unitMapByContentId = new MapMaker().softValues().makeMap();
 
   /**
    * Adds a new entry into the cache.
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/TypeOracle.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/TypeOracle.java
index 334fcd0..de16ed7 100644
--- a/dev/core/src/com/google/gwt/dev/javac/typemodel/TypeOracle.java
+++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/TypeOracle.java
@@ -28,10 +28,7 @@
 import com.google.gwt.dev.util.Name;
 import com.google.gwt.dev.util.collect.HashMap;
 import com.google.gwt.dev.util.collect.IdentityHashMap;
-
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceIdentityMap;
-import org.apache.commons.collections.map.ReferenceMap;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
@@ -283,9 +280,7 @@
    * Cached types that represent Arrays of other types. These types are created
    * as needed.
    */
-  @SuppressWarnings("unchecked")
-  private final Map<JType, JArrayType> arrayTypes = new ReferenceIdentityMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true);
+  private final Map<JType, JArrayType> arrayTypes = new MapMaker().weakKeys().weakValues().makeMap();
 
   /**
    * Cached singleton type representing <code>java.lang.Object</code>.
@@ -309,9 +304,8 @@
    * Subclasses of generic types that have type parameters filled in. These
    * types are created as needed.
    */
-  @SuppressWarnings("unchecked")
   private final Map<ParameterizedTypeKey, JParameterizedType> parameterizedTypes =
-      new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK, true);
+      new MapMaker().weakValues().makeMap();
 
   /**
    * A list of recently-added types that will be fully initialized on the next
@@ -321,9 +315,7 @@
 
   private JWildcardType unboundWildCardType;
 
-  @SuppressWarnings("unchecked")
-  private final Map<WildCardKey, JWildcardType> wildcardTypes = new ReferenceMap(
-      AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK, true);
+  private final Map<WildCardKey, JWildcardType> wildcardTypes = new MapMaker().weakValues().makeMap();
 
   public TypeOracle() {
     // Always create the default package.
diff --git a/dev/core/src/com/google/gwt/dev/jjs/CorrelationFactory.java b/dev/core/src/com/google/gwt/dev/jjs/CorrelationFactory.java
index c8b99cb..8e13492 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/CorrelationFactory.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/CorrelationFactory.java
@@ -21,11 +21,9 @@
 import com.google.gwt.dev.jjs.ast.JField;
 import com.google.gwt.dev.jjs.ast.JMethod;
 import com.google.gwt.dev.jjs.ast.JType;
-
-import org.apache.commons.collections.map.ReferenceMap;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
 import java.io.Serializable;
-import java.util.Collections;
 import java.util.EnumMap;
 import java.util.Map;
 
@@ -112,9 +110,7 @@
     /**
      * This cuts down on the total number of Correlation objects allocated.
      */
-    @SuppressWarnings("unchecked")
-    private final Map<Object, Correlation> canonicalMap = Collections
-        .synchronizedMap(new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK));
+    private final Map<Object, Correlation> canonicalMap = new MapMaker().weakKeys().weakValues().makeMap();
 
     private RealCorrelationFactory() {
     }
diff --git a/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java b/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java
index 5d598e1..daaffc5 100644
--- a/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java
+++ b/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java
@@ -25,9 +25,7 @@
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
 import com.google.gwt.dev.util.msg.Message0;
 import com.google.gwt.dev.util.msg.Message1String;
-
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceMap;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
 import java.io.File;
 import java.io.IOException;
@@ -153,9 +151,8 @@
     }
   }
 
-  @SuppressWarnings("unchecked")
-  private static final Map<ResourceLoader, List<ClassPathEntry>> classPathCache = new ReferenceMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD);
+  private static final Map<ResourceLoader, List<ClassPathEntry>> classPathCache =
+      new MapMaker().weakKeys().makeMap();
   
   public static void clearCache() {
     classPathCache.clear();
diff --git a/dev/core/src/com/google/gwt/dev/resource/impl/ZipFileClassPathEntry.java b/dev/core/src/com/google/gwt/dev/resource/impl/ZipFileClassPathEntry.java
index 17a4144..110de6a 100644
--- a/dev/core/src/com/google/gwt/dev/resource/impl/ZipFileClassPathEntry.java
+++ b/dev/core/src/com/google/gwt/dev/resource/impl/ZipFileClassPathEntry.java
@@ -21,10 +21,7 @@
 import com.google.gwt.dev.util.collect.IdentityMaps;
 import com.google.gwt.dev.util.collect.Sets;
 import com.google.gwt.dev.util.msg.Message1String;
-
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceIdentityMap;
-import org.apache.commons.collections.map.ReferenceMap;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
 import java.io.File;
 import java.io.IOException;
@@ -75,9 +72,7 @@
    * not referenced anywhere else, so we use hard reference, and soft reference on
    * {@link ZipFileClassPathEntry} allows its clearing in response to memory demand.
    */
-  @SuppressWarnings("unchecked")
-  private static final Map<String, ZipFileClassPathEntry> entryCache = new ReferenceMap(
-      AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT);
+  private static final Map<String, ZipFileClassPathEntry> entryCache = new MapMaker().softValues().makeMap();
 
   public static void clearCache() {
     entryCache.clear();
@@ -105,9 +100,7 @@
    * and {@link ZipFileSnapshot} is not referenced anywhere outside of {@link ZipFileClassPathEntry}
    * . When the module dies, the {@link ZipFileSnapshot} needs to die also.
    */
-  @SuppressWarnings("unchecked")
-  private final Map<PathPrefixSet, ZipFileSnapshot> cachedSnapshots = new ReferenceIdentityMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD, true);
+  private final Map<PathPrefixSet, ZipFileSnapshot> cachedSnapshots = new MapMaker().weakKeys().makeMap();
 
   private final long lastModified;
   private final String location;
diff --git a/dev/core/src/com/google/gwt/dev/shell/CompilingClassLoader.java b/dev/core/src/com/google/gwt/dev/shell/CompilingClassLoader.java
index dd6d468..dd13008 100644
--- a/dev/core/src/com/google/gwt/dev/shell/CompilingClassLoader.java
+++ b/dev/core/src/com/google/gwt/dev/shell/CompilingClassLoader.java
@@ -45,12 +45,9 @@
 import com.google.gwt.dev.util.log.speedtracer.DevModeEventType;
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger;
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 import com.google.gwt.util.tools.Utility;
 
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceIdentityMap;
-import org.apache.commons.collections.map.ReferenceMap;
-
 import java.beans.Beans;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -940,13 +937,9 @@
 
   private final TypeOracle typeOracle;
 
-  @SuppressWarnings("unchecked")
-  private final Map<Object, Object> weakJavaWrapperCache = new ReferenceIdentityMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK);
+  private final Map<Object, Object> weakJavaWrapperCache = new MapMaker().weakKeys().weakValues().makeMap();
 
-  @SuppressWarnings("unchecked")
-  private final Map<Integer, Object> weakJsoCache = new ReferenceMap(
-      AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK);
+  private final Map<Integer, Object> weakJsoCache = new MapMaker().weakValues().makeMap();
 
   public CompilingClassLoader(TreeLogger logger,
       CompilationState compilationState, ShellJavaScriptHost javaScriptHost)
diff --git a/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java b/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
index 77944a6..9e7ead7 100644
--- a/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
+++ b/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
@@ -27,12 +27,9 @@
 import com.google.gwt.dev.shell.log.ServletContextTreeLogger;
 import com.google.gwt.dev.util.HttpHeaders;
 import com.google.gwt.dev.util.Util;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 import com.google.gwt.util.tools.Utility;
 
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceIdentityMap;
-import org.apache.commons.collections.map.ReferenceMap;
-
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -93,27 +90,22 @@
   /**
    * Must keep only weak references to ModuleDefs else we permanently pin them.
    */
-  @SuppressWarnings("unchecked")
-  private final Map<String, ModuleDef> loadedModulesByName = new ReferenceMap(
-      AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK);
+  private final Map<String, ModuleDef> loadedModulesByName = new MapMaker().weakValues().makeMap();
 
   /**
    * The lifetime of the module pins the lifetime of the associated servlet;
    * this is because the loaded servlet has a weak backRef to its live module
    * through its context. When the module dies, the servlet needs to die also.
    */
-  @SuppressWarnings("unchecked")
-  private final Map<ModuleDef, Map<String, HttpServlet>> loadedServletsByModuleAndClassName = new ReferenceIdentityMap(
-      AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD, true);
+  private final Map<ModuleDef, Map<String, HttpServlet>> loadedServletsByModuleAndClassName =
+      new MapMaker().weakKeys().makeMap();
 
   private final Map<String, String> mimeTypes = new HashMap<String, String>();
 
   /**
    * Only for backwards compatibility. Shouldn't we remove this now?
    */
-  @SuppressWarnings("unchecked")
-  private final Map<String, ModuleDef> modulesByServletPath = new ReferenceMap(
-      AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK);
+  private final Map<String, ModuleDef> modulesByServletPath = new MapMaker().weakValues().makeMap();
 
   private int nextRequestId;
 
diff --git a/dev/core/test/com/google/gwt/dev/javac/TypeOracleMediatorTestBase.java b/dev/core/test/com/google/gwt/dev/javac/TypeOracleMediatorTestBase.java
index 86c5580..759bf3d 100644
--- a/dev/core/test/com/google/gwt/dev/javac/TypeOracleMediatorTestBase.java
+++ b/dev/core/test/com/google/gwt/dev/javac/TypeOracleMediatorTestBase.java
@@ -64,12 +64,10 @@
 import com.google.gwt.dev.util.Util;
 import com.google.gwt.dev.util.log.AbstractTreeLogger;
 import com.google.gwt.dev.util.log.PrintWriterTreeLogger;
+import com.google.gwt.thirdparty.guava.common.collect.MapMaker;
 
 import junit.framework.TestCase;
 
-import org.apache.commons.collections.map.AbstractReferenceMap;
-import org.apache.commons.collections.map.ReferenceMap;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -860,15 +858,13 @@
    * parameterizedTypes, arrayTypes, and wildCardTypes in TypeOracle. Note: this
    * test is manual because gc can be unreliable.
    */
-  @SuppressWarnings("unchecked")
   public void manualTestAbstractRefrenceMap() {
 
     /*
      * with a HARD -> WEAK map, verify that the entry remains if there is no
      * reference to key, but is deleted when the reference to value is gone
      */
-    Map<Integer, Integer> simpleMap =
-        new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK, true);
+    Map<Integer, Integer> simpleMap = new MapMaker().weakValues().makeMap();
     Integer bar = new Integer(42);
     simpleMap.put(new Integer(32), bar);
     Runtime.getRuntime().gc();
@@ -881,9 +877,8 @@
      * with a WEAK -> WEAK map, verify that the entry is gone if there are no
      * references to either the key or the value.
      */
-    simpleMap = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true);
-    Map<Integer, Integer> reverseMap =
-        new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true);
+    simpleMap = new MapMaker().weakKeys().weakValues().makeMap();
+    Map<Integer, Integer> reverseMap = new MapMaker().weakKeys().weakValues().makeMap();
     Integer foo = new Integer(32);
     bar = new Integer(42);
     simpleMap.put(foo, bar);