You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by bc...@apache.org on 2010/06/18 20:37:12 UTC

svn commit: r956086 - in /click/trunk/click/framework/src/org/apache/click/util: MessagesMap.java SessionMap.java

Author: bckfnn
Date: Fri Jun 18 18:37:12 2010
New Revision: 956086

URL: http://svn.apache.org/viewvc?rev=956086&view=rev
Log:
generics. CLK-696
also fix a wrong values in SessionMap.entrySet()

Modified:
    click/trunk/click/framework/src/org/apache/click/util/MessagesMap.java
    click/trunk/click/framework/src/org/apache/click/util/SessionMap.java

Modified: click/trunk/click/framework/src/org/apache/click/util/MessagesMap.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/util/MessagesMap.java?rev=956086&r1=956085&r2=956086&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/util/MessagesMap.java (original)
+++ click/trunk/click/framework/src/org/apache/click/util/MessagesMap.java Fri Jun 18 18:37:12 2010
@@ -69,11 +69,12 @@ import org.apache.click.service.ConfigSe
 public class MessagesMap implements Map<String, String> {
 
     /** Cache of resource bundle and locales which were not found. */
-    protected static final Set NOT_FOUND_CACHE =
-        Collections.synchronizedSet(new HashSet());
+    protected static final Set<String> NOT_FOUND_CACHE =
+        Collections.synchronizedSet(new HashSet<String>());
 
     /** Cache of messages keyed by bundleName + Locale name. */
-    protected static final Map MESSAGES_CACHE = new HashMap();
+    protected static final Map<CacheKey, Map<String, String>> MESSAGES_CACHE = 
+        new HashMap<CacheKey, Map<String, String>>();
 
     /** The cache key set load lock. */
     protected static final Object CACHE_LOAD_LOCK = new Object();
@@ -81,13 +82,13 @@ public class MessagesMap implements Map<
     // ----------------------------------------------------- Instance Variables
 
     /** The base class. */
-    protected final Class baseClass;
+    protected final Class<?> baseClass;
 
     /** The class global resource bundle base name. */
     protected final String globalBaseName;
 
     /** The map of localized messages. */
-    protected Map messages;
+    protected Map<String, String> messages;
 
     /** The resource bundle locale. */
     protected final Locale locale;
@@ -105,7 +106,7 @@ public class MessagesMap implements Map<
      * @param baseClass the target class
      * @param globalResource the global resource bundle name
      */
-    public MessagesMap(Class baseClass, String globalResource) {
+    public MessagesMap(Class<?> baseClass, String globalResource) {
         Validate.notNull(baseClass, "Null object parameter");
 
         this.baseClass = baseClass;
@@ -163,7 +164,7 @@ public class MessagesMap implements Map<
         String value = null;
         if (key != null) {
             ensureInitialized();
-            value = (String) messages.get(key);
+            value = messages.get(key);
         }
 
         if (value == null) {
@@ -204,7 +205,7 @@ public class MessagesMap implements Map<
      *
      * @see java.util.Map#putAll(Map)
      */
-    public void putAll(Map map) {
+    public void putAll(Map<? extends String, ? extends String> map) {
         throw new UnsupportedOperationException();
     }
 
@@ -221,7 +222,7 @@ public class MessagesMap implements Map<
     /**
      * @see java.util.Map#keySet()
      */
-    public Set keySet() {
+    public Set<String> keySet() {
         ensureInitialized();
         return messages.keySet();
     }
@@ -229,7 +230,7 @@ public class MessagesMap implements Map<
     /**
      * @see java.util.Map#values()
      */
-    public Collection values() {
+    public Collection<String> values() {
         ensureInitialized();
         return messages.values();
     }
@@ -237,7 +238,7 @@ public class MessagesMap implements Map<
     /**
      * @see java.util.Map#entrySet()
      */
-    public Set entrySet() {
+    public Set<Map.Entry<String, String>> entrySet() {
         ensureInitialized();
         return messages.entrySet();
     }
@@ -245,6 +246,7 @@ public class MessagesMap implements Map<
     /**
      * @see #toString()
      */
+    @Override
     public String toString() {
         ensureInitialized();
         return messages.toString();
@@ -258,22 +260,22 @@ public class MessagesMap implements Map<
             CacheKey resourceKey = new CacheKey(globalBaseName,
                 baseClass.getName(), locale.toString());
 
-            messages = (Map) MESSAGES_CACHE.get(resourceKey);
+            messages = MESSAGES_CACHE.get(resourceKey);
 
             if (messages != null) {
                 return;
             }
 
-            messages = new HashMap();
+            messages = new HashMap<String, String>();
 
             synchronized (CACHE_LOAD_LOCK) {
 
                 loadResourceValuesIntoMap(globalBaseName, messages);
 
-                List classnameList = new ArrayList();
+                List<String> classnameList = new ArrayList<String>();
 
                 // Build class list
-                Class aClass = baseClass;
+                Class<?> aClass = baseClass;
                 while (!aClass.getName().equals("java.lang.Object")) {
                     classnameList.add(aClass.getName());
                     aClass = aClass.getSuperclass();
@@ -282,7 +284,7 @@ public class MessagesMap implements Map<
                 // Load messages from parent to child order, so that child
                 // class messages override parent messages.
                 for (int i = classnameList.size() - 1; i >= 0; i--) {
-                    String className = (String) classnameList.get(i);
+                    String className = classnameList.get(i);
                     loadResourceValuesIntoMap(className, messages);
                 }
 
@@ -297,7 +299,7 @@ public class MessagesMap implements Map<
         }
     }
 
-    private void loadResourceValuesIntoMap(String resourceName, Map map) {
+    private void loadResourceValuesIntoMap(String resourceName, Map<String, String> map) {
         if (resourceName == null) {
             return;
         }
@@ -308,9 +310,9 @@ public class MessagesMap implements Map<
             try {
                 ResourceBundle resources = ClickUtils.getBundle(resourceName, locale);
 
-                Enumeration e = resources.getKeys();
+                Enumeration<String> e = resources.getKeys();
                 while (e.hasMoreElements()) {
-                    String name = e.nextElement().toString();
+                    String name = e.nextElement();
                     String value = resources.getString(name);
                     map.put(name, value);
                 }

Modified: click/trunk/click/framework/src/org/apache/click/util/SessionMap.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/util/SessionMap.java?rev=956086&r1=956085&r2=956086&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/util/SessionMap.java (original)
+++ click/trunk/click/framework/src/org/apache/click/util/SessionMap.java Fri Jun 18 18:37:12 2010
@@ -22,7 +22,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
@@ -49,7 +48,7 @@ import javax.servlet.http.HttpSession;
  * The SessionMap supports {@link FlashAttribute} which when accessed via
  * {@link #get(Object)} are removed from the session.
  */
-public class SessionMap implements Map {
+public class SessionMap implements Map<String, Object> {
 
     /** The internal session attribute. */
     protected HttpSession session;
@@ -69,7 +68,7 @@ public class SessionMap implements Map {
     public int size() {
         if (session != null) {
             int size = 0;
-            Enumeration enumeration = session.getAttributeNames();
+            Enumeration<?> enumeration = session.getAttributeNames();
             while (enumeration.hasMoreElements()) {
                 enumeration.nextElement();
                 size++;
@@ -134,7 +133,7 @@ public class SessionMap implements Map {
     /**
      * @see java.util.Map#put(Object, Object)
      */
-    public Object put(Object key, Object value) {
+    public Object put(String key, Object value) {
         if (session != null && key != null) {
             Object out = session.getAttribute(key.toString());
 
@@ -165,11 +164,10 @@ public class SessionMap implements Map {
     /**
      * @see java.util.Map#putAll(Map)
      */
-    public void putAll(Map map) {
+    public void putAll(Map<? extends String, ?> map) {
         if (session != null && map != null) {
-            for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
-                Map.Entry entry = (Map.Entry) i.next();
-                String key = entry.getKey().toString();
+            for (Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) {
+                String key = entry.getKey();
                 Object value = entry.getValue();
                 session.setAttribute(key, value);
             }
@@ -181,7 +179,7 @@ public class SessionMap implements Map {
      */
     public void clear() {
         if (session != null) {
-            Enumeration enumeration = session.getAttributeNames();
+            Enumeration<?> enumeration = session.getAttributeNames();
             while (enumeration.hasMoreElements()) {
                 String name = enumeration.nextElement().toString();
                 session.removeAttribute(name);
@@ -192,19 +190,19 @@ public class SessionMap implements Map {
     /**
      * @see java.util.Map#keySet()
      */
-    public Set keySet() {
+    public Set<String> keySet() {
         if (session != null) {
-            Set keySet = new HashSet();
+            Set<String> keySet = new HashSet<String>();
 
-            Enumeration enumeration = session.getAttributeNames();
+            Enumeration<?> enumeration = session.getAttributeNames();
             while (enumeration.hasMoreElements()) {
-                keySet.add(enumeration.nextElement());
+                keySet.add(enumeration.nextElement().toString());
             }
 
             return keySet;
 
         } else {
-            return Collections.EMPTY_SET;
+            return Collections.emptySet();
         }
     }
 
@@ -214,29 +212,71 @@ public class SessionMap implements Map {
      *
      * @see java.util.Map#values()
      */
-    public Collection values() {
+    public Collection<Object> values() {
         throw new UnsupportedOperationException();
     }
 
     /**
      * @see java.util.Map#entrySet()
      */
-    public Set entrySet() {
+    public Set<Map.Entry<String, Object>> entrySet() {
         if (session != null) {
-            Set entrySet = new HashSet();
+            Set<Map.Entry<String, Object>> entrySet = new HashSet<Map.Entry<String, Object>>();
 
-            Enumeration enumeration = session.getAttributeNames();
+            Enumeration<?> enumeration = session.getAttributeNames();
             while (enumeration.hasMoreElements()) {
                 String name = enumeration.nextElement().toString();
                 Object value = session.getAttribute(name);
-                entrySet.add(value);
+                entrySet.add(new Entry(name, value));
             }
 
             return entrySet;
 
         } else {
-            return Collections.EMPTY_SET;
+            return Collections.emptySet();
         }
     }
+    
+    static class Entry implements Map.Entry<String, Object> {
+        final String key;
+        Object value;
+
+        /**
+         * Creates new entry.
+         */
+        Entry(String k, Object v) {
+            value = v;
+            key = k;
+        }
+
+        public final String getKey() {
+            return key;
+        }
 
+        public final Object getValue() {
+            return value;
+        }
+
+        public final Object setValue(Object newValue) {
+            Object oldValue = value;
+            value = newValue;
+            return oldValue;
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (!(o instanceof Entry))
+                return false;
+            Entry e = (Entry)o;
+            Object k1 = getKey();
+            Object k2 = e.getKey();
+            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
+                Object v1 = getValue();
+                Object v2 = e.getValue();
+                if (v1 == v2 || (v1 != null && v1.equals(v2)))
+                    return true;
+            }
+            return false;
+        }
+    }
 }



Re: svn commit: r956086 - in /click/trunk/click/framework/src/org/apache/click/util: MessagesMap.java SessionMap.java

Posted by Bob Schellink <sa...@gmail.com>.
On 21/06/2010 06:52, Finn Bock wrote:
> No, it is ok. HashMap.Entry is defined the same way. As long as the
> object is only used as a Map.Entry, not as a SessionMap.Entry
> instance, then all is fine. I have added a test for this.


Appreciate the tests you've been adding. I'm starting to feel guilty for not contributing more in
this area.

Kind regards

Bob



Re: svn commit: r956086 - in /click/trunk/click/framework/src/org/apache/click/util: MessagesMap.java SessionMap.java

Posted by Finn Bock <bc...@gmail.com>.
2010/6/19 Bob Schellink
> On 19/06/2010 04:37, bckfnn@apache.org wrote:
>
> Seems SessionMap#entrySet should have been Map#values? Guess we can move that code under #values
> instead of throwing exception.

Done.

> I think the nested Entry class will give issues inside Velocity since it's not public. Can you confirm?

No, it is ok. HashMap.Entry is defined the same way. As long as the
object is only used as a Map.Entry, not as a SessionMap.Entry
instance, then all is fine. I have added a test for this.

regards,
Finn

Re: svn commit: r956086 - in /click/trunk/click/framework/src/org/apache/click/util: MessagesMap.java SessionMap.java

Posted by Bob Schellink <sa...@gmail.com>.
On 19/06/2010 04:37, bckfnn@apache.org wrote:

> also fix a wrong values in SessionMap.entrySet()


Seems SessionMap#entrySet should have been Map#values? Guess we can move that code under #values
instead of throwing exception.

I think the nested Entry class will give issues inside Velocity since it's not public. Can you confirm?

regards

Bob