incubator-wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From el...@apache.org
Subject svn commit: r789612 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/internal/ test/java/org/apache/wink/common/internal/
Date Tue, 30 Jun 2009 08:29:14 GMT
Author: elman
Date: Tue Jun 30 08:29:13 2009
New Revision: 789612

URL: http://svn.apache.org/viewvc?rev=789612&view=rev
Log:
https://issues.apache.org/jira/browse/WINK-18

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMap.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/MultivaluedMapImpl.java
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMapTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMap.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMap.java?rev=789612&r1=789611&r2=789612&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMap.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMap.java
Tue Jun 30 08:29:13 2009
@@ -17,25 +17,38 @@
  *  under the License.
  *  
  *******************************************************************************/
- 
 
 package org.apache.wink.common.internal;
 
 import java.util.Collection;
-import java.util.LinkedHashSet;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import javax.ws.rs.core.MultivaluedMap;
 
+public class CaseInsensitiveMultivaluedMap<V> implements MultivaluedMap<String,
V> {
 
-public class CaseInsensitiveMultivaluedMap<V> implements MultivaluedMap<String,V>
{
+    private MultivaluedMap<String, V> map = new MultivaluedMapImpl<String, V>(
+                                              new Comparator<String>() {
 
-    private MultivaluedMap<CaseInsensitiveString,V> map = new MultivaluedMapImpl<CaseInsensitiveString,V>();
+                                                  public int compare(String o1, String o2)
{
+                                                      if (o1 == o2) {
+                                                          return 0;
+                                                      }
+                                                      if (o1 == null) {
+                                                          return -1;
+                                                      }
+                                                      if (o2 == null) {
+                                                          return 1;
+                                                      }
+                                                      return o1.compareToIgnoreCase(o2);
+                                                  }
+                                              });
 
     public void add(String key, V value) {
-        map.add(new CaseInsensitiveString(key), value);
+        map.add(key, value);
     }
 
     public void clear() {
@@ -43,19 +56,15 @@
     }
 
     public boolean containsKey(Object key) {
-        return map.containsKey(new CaseInsensitiveString((String)key));
+        return map.containsKey(key);
     }
 
     public boolean containsValue(Object value) {
         return map.containsValue(value);
     }
 
-    public Set<java.util.Map.Entry<String,List<V>>> entrySet() {
-        Set<java.util.Map.Entry<String,List<V>>> set = new LinkedHashSet<java.util.Map.Entry<String,List<V>>>();
-        for (java.util.Map.Entry<CaseInsensitiveString,List<V>> entry : map.entrySet())
{
-            set.add(new Entry<V>(entry.getKey().toString(), entry.getValue()));
-        }
-        return set;
+    public Set<java.util.Map.Entry<String, List<V>>> entrySet() {
+        return map.entrySet();
     }
 
     public boolean equals(Object o) {
@@ -63,11 +72,11 @@
     }
 
     public List<V> get(Object key) {
-        return map.get(new CaseInsensitiveString((String)key));
+        return map.get(key);
     }
 
     public V getFirst(String key) {
-        return map.getFirst(new CaseInsensitiveString(key));
+        return map.getFirst(key);
     }
 
     public int hashCode() {
@@ -79,29 +88,23 @@
     }
 
     public Set<String> keySet() {
-        Set<String> stringSet = new LinkedHashSet<String>();
-        for (CaseInsensitiveString cis : map.keySet()) {
-            stringSet.add(cis.toString());
-        }
-        return stringSet;
+        return map.keySet();
     }
 
     public List<V> put(String key, List<V> value) {
-        return map.put(new CaseInsensitiveString(key), value);
+        return map.put(key, value);
     }
 
-    public void putAll(Map<? extends String,? extends List<V>> t) {
-        for (String key : t.keySet()) {
-            map.put(new CaseInsensitiveString(key), t.get(key));
-        }
+    public void putAll(Map<? extends String, ? extends List<V>> t) {
+        map.putAll(t);
     }
 
     public void putSingle(String key, V value) {
-        map.putSingle(new CaseInsensitiveString(key), value);
+        map.putSingle(key, value);
     }
 
     public List<V> remove(Object key) {
-        return map.remove(new CaseInsensitiveString((String)key));
+        return map.remove(key);
     }
 
     public int size() {
@@ -111,63 +114,5 @@
     public Collection<List<V>> values() {
         return map.values();
     }
-    
-    private static class Entry<V> implements java.util.Map.Entry<String,List<V>>
{
-        private String key;
-        private List<V> value;
-        public Entry(String key, List<V> value) {
-            this.key = key;
-            this.value = value;
-        }
-        public String getKey() {
-            return this.key;
-        }
-
-        public List<V> getValue() {
-            return this.value;
-        }
-
-        public List<V> setValue(List<V> value) {
-            throw new UnsupportedOperationException();
-        }
-    }
-    
-    private static class CaseInsensitiveString {
-        private String string;
-        
-        public CaseInsensitiveString(String string) {
-            this.string = string;
-        }
-        
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((string == null) ? 0 : string.toLowerCase().hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj)
-                return true;
-            if (obj == null)
-                return false;
-            if (getClass() != obj.getClass())
-                return false;
-            CaseInsensitiveString other = (CaseInsensitiveString)obj;
-            if (string == null) {
-                if (other.string != null)
-                    return false;
-            } else if (!string.equalsIgnoreCase(other.string))
-                return false;
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            return string;
-        }
-    }
 
 }
\ No newline at end of file

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/MultivaluedMapImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/MultivaluedMapImpl.java?rev=789612&r1=789611&r2=789612&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/MultivaluedMapImpl.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/MultivaluedMapImpl.java
Tue Jun 30 08:29:13 2009
@@ -17,37 +17,51 @@
  *  under the License.
  *  
  *******************************************************************************/
- 
-
 package org.apache.wink.common.internal;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 
 import javax.ws.rs.core.MultivaluedMap;
 
-public class MultivaluedMapImpl<K,V> extends LinkedHashMap<K,List<V>> 
-    implements MultivaluedMap<K,V>, Cloneable {
+public class MultivaluedMapImpl<K, V> //extends LinkedHashMap<K,List<V>>

+    implements MultivaluedMap<K, V>, Cloneable {
 
     private static final long serialVersionUID = -1942980976209902832L;
 
+    private final Map<K, List<V>>   map;
+
     public MultivaluedMapImpl() {
+        map = new LinkedHashMap<K, List<V>>();
     }
     
-    public MultivaluedMapImpl(Map<K,V> map) {
+    /**
+     * Used to create a CaseInsensitiveMultivaluedMap
+     * @param keyComparator
+     */
+    MultivaluedMapImpl(Comparator<K> keyComparator) {
+        map = new TreeMap<K, List<V>>(keyComparator);
+    }
+
+    public MultivaluedMapImpl(Map<K, V> map) {
+        this();
         for (K key : map.keySet()) {
             add(key, map.get(key));
         }
     }
-    
+
     public void add(K key, V value) {
         List<V> list = getOrCreate(key);
         list.add(value);
     }
 
-     public V getFirst(K key) {
+    public V getFirst(K key) {
         List<V> list = get(key);
         if (list == null || list.size() == 0) {
             return null;
@@ -73,18 +87,18 @@
     private List<V> createValueList(K key) {
         return new ArrayList<V>();
     }
-    
-    public MultivaluedMapImpl<K,V> clone() {
+
+    public MultivaluedMapImpl<K, V> clone() {
         return clone(this);
     }
-    
-    public static <K,V> MultivaluedMapImpl<K,V> clone(MultivaluedMap<K,V>
src) {
-        MultivaluedMapImpl<K,V> clone = new MultivaluedMapImpl<K,V>();
+
+    public static <K, V> MultivaluedMapImpl<K, V> clone(MultivaluedMap<K,
V> src) {
+        MultivaluedMapImpl<K, V> clone = new MultivaluedMapImpl<K, V>();
         copy(src, clone);
         return clone;
     }
 
-    public static <K,V> void copy(MultivaluedMap<K,V> src, MultivaluedMap<K,V>
dest) {
+    public static <K, V> void copy(MultivaluedMap<K, V> src, MultivaluedMap<K,
V> dest) {
         for (K key : src.keySet()) {
             List<V> value = src.get(key);
             List<V> newValue = new ArrayList<V>();
@@ -93,7 +107,7 @@
         }
     }
 
-    public static <K,V> void addAll(MultivaluedMap<K,V> src, MultivaluedMap<K,V>
dest) {
+    public static <K, V> void addAll(MultivaluedMap<K, V> src, MultivaluedMap<K,
V> dest) {
         for (K key : src.keySet()) {
             List<V> srcList = src.get(key);
             List<V> destList = dest.get(key);
@@ -109,10 +123,10 @@
     public String toString() {
         return "[" + toString(this, ",") + "]";
     }
-    
-    public static String toString(MultivaluedMap<?,?> map, String delimiter) {
+
+    public static String toString(MultivaluedMap<?, ?> map, String delimiter) {
         StringBuilder result = new StringBuilder();
-        MultivaluedMap<?,?> params = map;
+        MultivaluedMap<?, ?> params = map;
         String delim = "";
         for (Object name : params.keySet()) {
             for (Object value : params.get(name)) {
@@ -127,23 +141,24 @@
         }
         return result.toString();
     }
-    
-    public static MultivaluedMap<String,String> toMultivaluedMapString(Map<String,?
extends Object> values) {
-        MultivaluedMap<String,String> mValues = new MultivaluedMapImpl<String,String>();
+
+    public static MultivaluedMap<String, String> toMultivaluedMapString(
+        Map<String, ? extends Object> values) {
+        MultivaluedMap<String, String> mValues = new MultivaluedMapImpl<String,
String>();
         for (String key : values.keySet()) {
             Object value = values.get(key);
             if (value == null) {
                 mValues.add(key, null);
             } else if (value instanceof Object[]) {
-                for (Object obj : (Object[])value) {
+                for (Object obj : (Object[]) value) {
                     if (obj == null) {
                         mValues.add(key, null);
                     } else {
                         mValues.add(key, obj.toString());
                     }
                 }
-            } else if (value instanceof List) {
-                for (Object obj : (List<?>)value) {
+            } else if (value instanceof List<?>) {
+                for (Object obj : (List<?>) value) {
                     if (obj == null) {
                         mValues.add(key, null);
                     } else {
@@ -157,5 +172,59 @@
         return mValues;
     }
 
+    public void clear() {
+        map.clear();
+    }
+
+    public boolean containsKey(Object key) {
+        return map.containsKey(key);
+    }
+
+    public boolean containsValue(Object value) {
+        return map.containsValue(value);
+    }
+
+    public Set<java.util.Map.Entry<K, List<V>>> entrySet() {
+        return map.entrySet();
+    }
+
+    public boolean equals(Object o) {
+        return map.equals(o);
+    }
+
+    public List<V> get(Object key) {
+        return map.get(key);
+    }
+
+    public int hashCode() {
+        return map.hashCode();
+    }
+
+    public boolean isEmpty() {
+        return map.isEmpty();
+    }
+
+    public Set<K> keySet() {
+        return map.keySet();
+    }
+
+    public List<V> put(K key, List<V> value) {
+        return map.put(key, value);
+    }
+
+    public void putAll(Map<? extends K, ? extends List<V>> t) {
+        map.putAll(t);
+    }
 
+    public List<V> remove(Object key) {
+        return map.remove(key);
+    }
+
+    public int size() {
+        return map.size();
+    }
+
+    public Collection<List<V>> values() {
+        return map.values();
+    }
 }

Modified: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMapTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMapTest.java?rev=789612&r1=789611&r2=789612&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMapTest.java
(original)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/CaseInsensitiveMultivaluedMapTest.java
Tue Jun 30 08:29:13 2009
@@ -117,8 +117,10 @@
         
         Set<String> keySet = map.keySet();
         assertTrue(keySet.contains("a"));
-        assertFalse(keySet.contains("A"));
+        assertTrue(keySet.contains("A"));
         assertTrue(keySet.contains("B"));
-        assertFalse(keySet.contains("b"));
+        assertTrue(keySet.contains("b"));
+        assertFalse(keySet.contains("c"));
+        assertFalse(keySet.contains("C"));
     }
 }



Mime
View raw message