Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright SpringSource Inc 2009.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.springsource.util.common;
 
 import java.util.Map;
 import java.util.Set;
CaseInsensitiveMap is a java.util.Map from String keys to values which is case-insensitive and case-preserving with respect to the keys in the map.

Concurrent Semantics
This class is not necessarily thread safe.

Parameters:
<V> range type parameter
Author(s):
Glyn Normington
Since:
jersey
 
 public class CaseInsensitiveMap<V> extends AbstractMap<String, V> {
 
     private final Map<CaseInsensitiveKey, V> map = new ConcurrentHashMap<CaseInsensitiveKey, V>();
 
     private static final class KeySet extends AbstractSet<String> {
 
         private static final class KeySetIterator implements Iterator<String> {
 
             private Iterator<CaseInsensitiveKeyiterator;
 
             public KeySetIterator(Iterator<CaseInsensitiveKeyiterator) {
                 this. = iterator;
             }

            
 
             public boolean hasNext() {
                 return this..hasNext();
             }

            
 
             public String next() {
                 return this..next().toString();
             }

            
 
             public void remove() {
                 this..remove();
             }
         }
 
         private final Set<CaseInsensitiveKeykeySet;
 
         public KeySet(Set<CaseInsensitiveKeykeySet) {
             this. = keySet;
         }

        
Not supported for sets returned by Map.keySet.
 
         @Override
         public boolean add(String o) {
             throw new UnsupportedOperationException("Map.keySet must return a Set which does not support add");
         }

        
Not supported for sets returned by Map.keySet.
 
         @Override
         public boolean addAll(Collection<? extends Stringc) {
             throw new UnsupportedOperationException("Map.keySet must return a Set which does not support addAll");
         }

        
        @Override
        public void clear() {
            this..clear();
        }

        
        @Override
        public boolean contains(Object o) {
            return o instanceof String ? this..contains(CaseInsensitiveKey.objectToKey(o)) : false;
        }

        
        @Override
        public Iterator<Stringiterator() {
            return new KeySetIterator(this..iterator());
        }

        
        @Override
        public boolean remove(Object o) {
            // The following can throw ClassCastException which conforms to the method specification.
            return this..remove(CaseInsensitiveKey.objectToKey(o));
        }

        
        @Override
        public int size() {
            return this..size();
        }
    }
    private static final class EntrySet<V> extends AbstractSet<Entry<String, V>> {
        private static final class MapEntry<V> implements Entry<String, V> {
            private final Entry<CaseInsensitiveMap.CaseInsensitiveKey, V> entry;
            public MapEntry(Entry<CaseInsensitiveMap.CaseInsensitiveKey, V> entry) {
                this. = entry;
            }

            
            public String getKey() {
                return this..getKey().toString();
            }

            
            public V getValue() {
                return this..getValue();
            }

            
            public V setValue(V value) {
                return this..setValue(value);
            }

            
Get the underlying java.util.Map.Entry.

Returns:
the underlying Entry
            public Entry<CaseInsensitiveMap.CaseInsensitiveKey, V> getEntry() {
                return this.;
            }
        }
        private static final class EntrySetIterator<V> implements Iterator<Entry<String, V>> {
            private final Iterator<Entry<CaseInsensitiveKey, V>> iterator;
            public EntrySetIterator(Iterator<Entry<CaseInsensitiveKey, V>> iterator) {
                this. = iterator;
            }

            
            public boolean hasNext() {
                return this..hasNext();
            }

            
            public Entry<String, V> next() {
                return new MapEntry<V>(this..next());
            }

            
            public void remove() {
                this..remove();
            }
        }
        private final Set<Entry<CaseInsensitiveKey, V>> entrySet;
        
        private final  CaseInsensitiveMap<V> map;
        public EntrySet(Set<Entry<CaseInsensitiveKey, V>> entrySetCaseInsensitiveMap<V> map) {
            this. = entrySet;
            this. = map;
        }

        
Not supported for sets returned by Map.entrySet.
        @Override
        public boolean add(Entry<String, V> o) {
            throw new UnsupportedOperationException("Map.entrySet must return a Set which does not support add");
        }

        
Not supported for sets returned by Map.entrySet.
        @Override
        public boolean addAll(Collection<? extends Entry<String, V>> c) {
            throw new UnsupportedOperationException("Map.entrySet must return a Set which does not support addAll");
        }

        
        @Override
        public void clear() {
            this..clear();
        }

        
        @SuppressWarnings("unchecked")
        @Override
        public boolean contains(Object o) {
            if (o instanceof Entry) {
                Entry<String, V> e = (Entry<String, V>) o;
                V value = this..get(e.getKey());
                return value.equals(e.getValue());
            }
            return false;
        }

        
        @Override
        public Iterator<Entry<String, V>> iterator() {
            return new EntrySetIterator<V>(this..iterator());
        }

        
        @SuppressWarnings("unchecked")
        @Override
        public boolean remove(Object o) {
            try {
                return this..remove(((MapEntry<V>) o).getEntry());
            } catch (ClassCastException e) {
            }
            return false;
        }

        
        @Override
        public int size() {
            return this..size();
        }
    }
    static final class CaseInsensitiveKey {
        private final String key;
        private CaseInsensitiveKey(String key) {
            this. = key;
        }

        
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + .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;
            }
            CaseInsensitiveKey other = (CaseInsensitiveKeyobj;
            if ( == null) {
                if (other.key != null) {
                    return false;
                }
            } else if (!.equalsIgnoreCase(other.key)) {
                return false;
            }
            return true;
        }

        
        @Override
        public String toString() {
            return ;
        }

        
Convert the given key Object to a CaseInsensitiveMap.CaseInsensitiveKey.

Pre-condition: key instanceof String

Parameters:
key the key to be converted
Returns:
the CaseInsensitiveKey corresponding to the given key
        public static CaseInsensitiveKey objectToKey(Object key) {
            return new CaseInsensitiveKey((Stringkey);
        }
    }

    
    @Override
    public void clear() {
        this..clear();
    }

    
    @Override
    public boolean containsKey(Object key) {
        return key instanceof String ? this..containsKey(CaseInsensitiveKey.objectToKey(key)) : false;
    }

    
    @Override
    public boolean containsValue(Object value) {
        return this..containsValue(value);
    }

    
    @Override
    public Set<Entry<String, V>> entrySet() {
        return new EntrySet<V>(this..entrySet(), this);
    }

    
    @Override
    public V get(Object key) {
        return key instanceof String ? this..get(CaseInsensitiveKey.objectToKey(key)) : null;
    }

    
    @Override
    public Set<StringkeySet() {
        return new KeySet(this..keySet());
    }

    
    @Override
    public V put(String key, V value) {
        if (key == null) {
            throw new NullPointerException("CaseInsensitiveMap does not permit null keys");
        }
        return this..put(CaseInsensitiveKey.objectToKey(key), value);
    }

    
    @Override
    public V remove(Object key) {
        return key instanceof String ? this..remove(CaseInsensitiveKey.objectToKey(key)) : null;
    }

    
    @Override
    public int size() {
        return this..size();
    }

    
    @Override
    public Collection<V> values() {
        return this..values();
    }
New to GrepCode? Check out our FAQ X