[jboss-svn-commits] JBoss Common SVN: r3618 - common-core/trunk/src/main/java/org/jboss/util/collection.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Oct 19 16:58:52 EDT 2009


Author: alesj
Date: 2009-10-19 16:58:52 -0400 (Mon, 19 Oct 2009)
New Revision: 3618

Modified:
   common-core/trunk/src/main/java/org/jboss/util/collection/WeakClassCache.java
Log:
[JBCOMMON-97]; generify class.

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/WeakClassCache.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/WeakClassCache.java	2009-10-19 16:31:10 UTC (rev 3617)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/WeakClassCache.java	2009-10-19 20:58:52 UTC (rev 3618)
@@ -35,13 +35,14 @@
  * instantiate - creates the data<br>
  * generate - fills in the details
  *
+ * @param <T> exact value type
  * @author <a href="mailto:adrian at jboss.org">Adrian Brock</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
- at SuppressWarnings("unchecked")
-public abstract class WeakClassCache
+public abstract class WeakClassCache<T>
 {
    /** The cache */
-   protected Map cache = new WeakHashMap(); 
+   protected final Map<ClassLoader, Map<String, WeakReference<T>>> cache = new WeakHashMap<ClassLoader, Map<String, WeakReference<T>>>();
 
    /**
     * Get the information for a class
@@ -49,24 +50,24 @@
     * @param clazz the class
     * @return the info
     */
-   public Object get(Class clazz)
+   public T get(Class<?> clazz)
    {
       if (clazz == null)
          throw new IllegalArgumentException("Null class");
       
-      Map classLoaderCache = getClassLoaderCache(clazz.getClassLoader());
+      Map<String, WeakReference<T>> classLoaderCache = getClassLoaderCache(clazz.getClassLoader());
 
-      WeakReference weak = (WeakReference) classLoaderCache.get(clazz.getName());
+      WeakReference<T> weak = classLoaderCache.get(clazz.getName());
       if (weak != null)
       {
-         Object result = weak.get();
+         T result = weak.get();
          if (result != null)
             return result;
       }
 
-      Object result = instantiate(clazz);
+      T result = instantiate(clazz);
 
-      weak = new WeakReference(result);
+      weak = new WeakReference<T>(result);
       classLoaderCache.put(clazz.getName(), weak);
       
       generate(clazz, result);
@@ -82,13 +83,14 @@
     * @return the info
     * @throws ClassNotFoundException when the class cannot be found
     */
-   public Object get(String name, ClassLoader cl) throws ClassNotFoundException
+   public T get(String name, ClassLoader cl) throws ClassNotFoundException
    {
       if (name == null)
          throw new IllegalArgumentException("Null name");
       if (cl == null)
          throw new IllegalArgumentException("Null classloader");
-      Class clazz = cl.loadClass(name);
+
+      Class<?> clazz = cl.loadClass(name);
       return get(clazz);
    }
    
@@ -98,7 +100,7 @@
     * @param clazz the class
     * @return the result
     */
-   protected abstract Object instantiate(Class clazz);
+   protected abstract T instantiate(Class<?> clazz);
    
    /**
     * Fill in the result
@@ -106,7 +108,7 @@
     * @param clazz the class
     * @param result the result
     */
-   protected abstract void generate(Class clazz, Object result);
+   protected abstract void generate(Class<?> clazz, T result);
    
    /**
     * Get the cache for the classloader
@@ -114,11 +116,11 @@
     * @param cl the classloader
     * @return the map
     */
-   protected Map getClassLoaderCache(ClassLoader cl)
+   protected Map<String, WeakReference<T>> getClassLoaderCache(ClassLoader cl)
    {
       synchronized (cache)
       {
-         Map result = (Map) cache.get(cl);
+         Map<String, WeakReference<T>> result = cache.get(cl);
          if (result == null)
          {
             result = CollectionsFactory.createConcurrentReaderMap();



More information about the jboss-svn-commits mailing list