All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.lang.collection.CaseInsensitiveMap Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.lang.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Implementation of a Map that ignores the case of the
 * keys. if you do
 * 
*
* put("test", "1"); *
* put("TEST", "2"); *
*
* the second put overwrites the value of the first one, * because the keys are considered to be equal. With *
*
* get("TesT"); *
*
* you'll get the result "2". * If you iterate through the keys (using either keySet or * entrySet), you'll get the first added version of the key, * in the above case, you'll get "test". * It is allowed to use non-strings as keys. In this case the Map * behaves like a usual HashMap.
* Note: This class is similar to a TreeMap(String.CASE_INSENSITIVE_ORDER) * except that non-strings do not throw a ClassCastException * and that keys are not sorted. * * @param the type of keys maintained by this map * @param the type of mapped values * */ public class CaseInsensitiveMap implements Map, Cloneable { private transient Map keys; private transient Map cmap; public CaseInsensitiveMap() { this.keys = new HashMap(); this.cmap = init(); } public CaseInsensitiveMap(Map map) { this(); putAll(map); } protected Map init() { return new HashMap(); } protected Object toCompareKey(Object key) { return key instanceof String ? ((String)key).toLowerCase() : key; } @Override public void clear() { keys.clear(); cmap.clear(); } @Override public boolean containsKey(Object key) { Object ck = toCompareKey(key); if (keys.containsKey(ck)) { K mk = keys.get(ck); if (cmap.containsKey(mk)) { return true; } keys.remove(ck); } return false; } @Override public boolean containsValue(Object value) { return cmap.containsValue(value); } @Override public Set> entrySet() { return cmap.entrySet(); } @Override public V get(Object key) { Object ck = toCompareKey(key); if (keys.containsKey(ck)) { K mk = keys.get(ck); return cmap.get(mk); } return null; } @Override public boolean isEmpty() { return cmap.isEmpty(); } @Override public Set keySet() { return new Set() { @Override public int size() { return cmap.keySet().size(); } @Override public boolean isEmpty() { return cmap.keySet().isEmpty(); } @Override public boolean contains(Object o) { return containsKey(o); } @Override public Iterator iterator() { return cmap.keySet().iterator(); } @Override public Object[] toArray() { return cmap.keySet().toArray(); } @Override public T[] toArray(T[] a) { return cmap.keySet().toArray(a); } @Override public boolean add(K e) { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { Object ck = toCompareKey(o); if (keys.containsKey(ck)) { K mk = keys.remove(ck); cmap.remove(mk); return true; } return false; } @Override public boolean containsAll(Collection c) { for (Object e : c) { if (!containsKey(e)) { return false; } } return true; } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { boolean modified = false; Set s = new CaseInsensitiveSet(c); Iterator it = iterator(); while (it.hasNext()) { if (!s.contains(it.next())) { it.remove(); modified = true; } } return modified; } @Override public boolean removeAll(Collection c) { boolean modified = false; for (Object o : c) { if (remove(o)) { modified = true; } } return modified; } @Override public void clear() { keys.clear(); cmap.clear(); } @Override public int hashCode() { return cmap.keySet().hashCode(); } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Set)) return false; return cmap.keySet().equals(o); } }; } @SuppressWarnings("unchecked") @Override public V put(K key, V value) { Object ck = toCompareKey(key); if (keys.containsKey(ck)) { K mk = keys.get(ck); return cmap.put(mk, value); } keys.put((K)ck, key); return cmap.put(key, value); } @Override public void putAll(Map m) { for (Entry en : m.entrySet()) { put(en.getKey(), en.getValue()); } } @Override public V remove(Object key) { Object ck = toCompareKey(key); if (keys.containsKey(ck)) { K mk = keys.remove(ck); return cmap.remove(mk); } return null; } @Override public int size() { return cmap.size(); } @Override public Collection values() { return cmap.values(); } @Override public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Map)) { return false; } return cmap.equals(o); } @Override public int hashCode() { return cmap.hashCode(); } @Override public String toString() { return cmap.toString(); } @Override public CaseInsensitiveMap clone() { CaseInsensitiveMap m = new CaseInsensitiveMap(); m.putAll(this); return m; } }