Package com.twelvemonkeys.util
Class IgnoreCaseMap<V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.twelvemonkeys.util.IgnoreCaseMap<V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<java.lang.String,V>
public class IgnoreCaseMap<V> extends java.util.AbstractMap<K,V> implements java.io.Serializable, java.lang.CloneableAMapdecorator that makes the mappings in the backing map case insensitive (this is implemented by converting all keys to uppercase), if the keys used areStrings. If the keys used are notStrings, it wil work as a normaljava.util.Map.- Author:
- Harald Kuhr
- See Also:
Map, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description IgnoreCaseMap()Constructs a new emptyMap.IgnoreCaseMap(java.util.Map<java.lang.String,? extends V> pMap)Constructs a newMapwith the same key-value mappings as the givenMap.IgnoreCaseMap(java.util.Map pBacking, java.util.Map<java.lang.String,? extends V> pContents)Constructs a newMapwith the same key-value mappings as the givenMap.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()protected java.lang.Objectclone()Returns a shallow copy of thisAbstractMapinstance: the keys and values themselves are not cloned.booleancontainsKey(java.lang.Object pKey)Tests if the specified object is a key in this map.booleancontainsValue(java.lang.Object pValue)Returnstrueif this map maps one or more keys to the specified pValue.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Vget(java.lang.Object pKey)Returns the value to which the specified key is mapped in this map.protected voidinit()Default implementation, does nothing.booleanisEmpty()java.util.Set<K>keySet()protected java.util.Iterator<java.util.Map.Entry<java.lang.String,V>>newEntryIterator()protected java.util.Iterator<java.lang.String>newKeyIterator()protected java.util.Iterator<V>newValueIterator()Vput(java.lang.String pKey, V pValue)Maps the specified key to the specified value in this map.Vremove(java.lang.Object pKey)Removes the key (and its corresponding value) from this map.protected java.util.Map.Entry<K,V>removeEntry(java.util.Map.Entry<K,V> pEntry)Removes the given entry from the Map.intsize()protected static java.lang.ObjecttoUpper(java.lang.Object pObject)Converts the parameter to uppercase, if it's a String.java.util.Collection<V>values()
-
-
-
Constructor Detail
-
IgnoreCaseMap
public IgnoreCaseMap()
Constructs a new emptyMap. The backing map will be aHashMap
-
IgnoreCaseMap
public IgnoreCaseMap(java.util.Map<java.lang.String,? extends V> pMap)
Constructs a newMapwith the same key-value mappings as the givenMap. The backing map will be aHashMapNOTE: As the keys in the given map parameter will be converted to uppercase (if they are strings), any duplicate key/value pair where
key instanceof String && key.equalsIgnoreCase(otherKey)is true, will be lost.- Parameters:
pMap- the map whose mappings are to be placed in this map.
-
IgnoreCaseMap
public IgnoreCaseMap(java.util.Map pBacking, java.util.Map<java.lang.String,? extends V> pContents)Constructs a newMapwith the same key-value mappings as the givenMap.NOTE: The backing map is structuraly cahnged, and it should NOT be accessed directly, after the wrapped map is created.
NOTE: As the keys in the given map parameter will be converted to uppercase (if they are strings), any duplicate key/value pair where
key instanceof String && key.equalsIgnoreCase(otherKey)is true, will be lost.- Parameters:
pBacking- the backing map of this map. Must be either empty, or the same map aspContents.pContents- the map whose mappings are to be placed in this map. May benull- Throws:
java.lang.IllegalArgumentException- ifpBackingisnulljava.lang.IllegalArgumentException- ifpBackingdiffers frompContentand is not empty.
-
-
Method Detail
-
put
public V put(java.lang.String pKey, V pValue)
Maps the specified key to the specified value in this map. Note: If the key used is a string, the key will not be case-sensitive.- Specified by:
putin interfacejava.util.Map<java.lang.String,V>- Parameters:
pKey- the map key.pValue- the value.- Returns:
- the previous value of the specified key in this map, or null if it did not have one.
-
get
public V get(java.lang.Object pKey)
Returns the value to which the specified key is mapped in this map. Note: If the key used is a string, the key will not be case-sensitive.- Specified by:
getin interfacejava.util.Map<java.lang.String,V>- Parameters:
pKey- a key in the map- Returns:
- the value to which the key is mapped in this map; null if the key is not mapped to any value in this map.
-
remove
public V remove(java.lang.Object pKey)
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map. Note: If the key used is a string, the key will not be case-sensitive.- Specified by:
removein interfacejava.util.Map<java.lang.String,V>- Parameters:
pKey- the key that needs to be removed.- Returns:
- the value to which the key had been mapped in this map, or null if the key did not have a mapping.
-
containsKey
public boolean containsKey(java.lang.Object pKey)
Tests if the specified object is a key in this map. Note: If the key used is a string, the key will not be case-sensitive.- Specified by:
containsKeyin interfacejava.util.Map<java.lang.String,V>- Parameters:
pKey- possible key.- Returns:
- true if and only if the specified object is a key in this map, as determined by the equals method; false otherwise.
-
toUpper
protected static java.lang.Object toUpper(java.lang.Object pObject)
Converts the parameter to uppercase, if it's a String.
-
newEntryIterator
protected java.util.Iterator<java.util.Map.Entry<java.lang.String,V>> newEntryIterator()
-
newKeyIterator
protected java.util.Iterator<java.lang.String> newKeyIterator()
-
newValueIterator
protected java.util.Iterator<V> newValueIterator()
-
init
protected void init()
Default implementation, does nothing.
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<K,V>- Overrides:
sizein classjava.util.AbstractMap<K,V>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<K,V>- Overrides:
clearin classjava.util.AbstractMap<K,V>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<K,V>- Overrides:
isEmptyin classjava.util.AbstractMap<K,V>
-
containsValue
public boolean containsValue(java.lang.Object pValue)
Returnstrueif this map maps one or more keys to the specified pValue. More formally, returnstrueif and only if this map contains at least one mapping to a pValuevsuch that(pValue==null ? v==null : pValue.equals(v)).This implementation requires time linear in the map size for this operation.
- Specified by:
containsValuein interfacejava.util.Map<K,V>- Overrides:
containsValuein classjava.util.AbstractMap<K,V>- Parameters:
pValue- pValue whose presence in this map is to be tested.- Returns:
trueif this map maps one or more keys to the specified pValue.
-
values
public java.util.Collection<V> values()
- Specified by:
valuesin interfacejava.util.Map<K,V>- Overrides:
valuesin classjava.util.AbstractMap<K,V>
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
- Specified by:
entrySetin interfacejava.util.Map<K,V>- Specified by:
entrySetin classjava.util.AbstractMap<K,V>
-
keySet
public java.util.Set<K> keySet()
- Specified by:
keySetin interfacejava.util.Map<K,V>- Overrides:
keySetin classjava.util.AbstractMap<K,V>
-
clone
protected java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionReturns a shallow copy of thisAbstractMapinstance: the keys and values themselves are not cloned.- Overrides:
clonein classjava.util.AbstractMap<K,V>- Returns:
- a shallow copy of this map.
- Throws:
java.lang.CloneNotSupportedException
-
removeEntry
protected java.util.Map.Entry<K,V> removeEntry(java.util.Map.Entry<K,V> pEntry)
Removes the given entry from the Map.- Parameters:
pEntry- the entry to be removed- Returns:
- the removed entry, or
nullif nothing was removed.
-
-