javawebparts.core
Class CaseInsensitiveMap

java.lang.Object
  |
  +--javawebparts.core.CaseInsensitiveMap
All Implemented Interfaces:
java.util.Map

public class CaseInsensitiveMap
extends java.lang.Object
implements java.util.Map

A Map implementation that handles its keys in a case insensitive manner. This is just a wrapper class, it uses another Map implementation of your choice (HashMap by default) to actually store the entries. The Map accepts only String objects as keys, it doesn't accept null keys, but it accepts null values (even if the wrapped Map doesn't support null values). The Map implements all the methods defined by the Map interface, and although it handles keys in a case insensitive manner, it maintains the original headers as they were added. This means that if you use the keySet() or the entrSet() methods to get a Collection view of the keys(or Map.Entry objects respectively) you'll find the keys with the same casing you added them (ie. they will not be converted to all lower-case or upper-case characters).

Author:
Tamas Szabo

Nested Class Summary
private  class CaseInsensitiveMap.AbstractCollectionView
          Abstract implementation having all the common functionality used by the collection view classes returned by keySet(), entrySet() and values().
private  class CaseInsensitiveMap.EntrySet
          Private Set implementation used to return a Set of entries in the map.
private static class CaseInsensitiveMap.FakeEntry
          Our Map.Entry implementation used in the Set returned by the entrySet() method.
private static class CaseInsensitiveMap.KeyAndValue
          Inner class used to store a key-value pair.
private  class CaseInsensitiveMap.KeySet
          Private Set implementation used to return a set of the keys in the map.
private  class CaseInsensitiveMap.ValuesCollection
          Private Collection implementation used to return a Collection of the values in the map.
 
Field Summary
private  java.util.Map map
          Internal Map used to store the Entries.
 
Constructor Summary
CaseInsensitiveMap()
          Creates a new CaseInsensitiveMap that will use a HashMap internally to store the mappings.
CaseInsensitiveMap(java.util.Map someOtherMap)
          Creates a new CaseInsensitiveMap and adds the mappings defined in the passed in map.
 
Method Summary
 void clear()
          Removes all the mappings from this map.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps at least one key to the specified value.
 java.util.Set entrySet()
          Returns a set view of the mappings contained in this map.
 boolean equals(java.lang.Object obj)
          Compares this map with the passed in object for equality.
 java.lang.Object get(java.lang.Object key)
          Returns the value mapped to the specified key or null if the specified key isn't mapped to any value.
 int hashCode()
          Returns the hash code of this map.
 boolean isEmpty()
          Return true if this map contains no entries.
 java.util.Set keySet()
          Returns a set view of the keys contained in this map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key.
 void putAll(java.util.Map otherMap)
          Iterates over all the mappings of the specified map and adds them with put(key, value) to this map.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from this map if present.
 int size()
          Returns the number of entries in this map.
 java.lang.String toString()
          Returns the string representation of this map.
 java.util.Collection values()
          Returns a collection view of the values contained in this map.
static CaseInsensitiveMap wrapExistingMap(java.util.Map someMap)
          Wrap an existing map and make it case-insensitive.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

map

private java.util.Map map
Internal Map used to store the Entries.

Constructor Detail

CaseInsensitiveMap

public CaseInsensitiveMap()
Creates a new CaseInsensitiveMap that will use a HashMap internally to store the mappings. The HashMap is created with the default no-arg constructor. If a default HashMap doesn't meet your needs please create a HashMap (or any other Map implementation) for and use the wrapExistingMap(Map) method to add the case-insensitive functionality to the Map you created.

See Also:
wrapExistingMap(Map)

CaseInsensitiveMap

public CaseInsensitiveMap(java.util.Map someOtherMap)
Creates a new CaseInsensitiveMap and adds the mappings defined in the passed in map. The functionality is the same as you would create the CaseInsensitiveMap with the no-arg constructor, and then you would invoke the putAll(Map) method on it, passing in the Map you pass in to this constructor.

Parameters:
someOtherMap - a map containing the mappings to be added
See Also:
CaseInsensitiveMap(), putAll(Map)
Method Detail

wrapExistingMap

public static CaseInsensitiveMap wrapExistingMap(java.util.Map someMap)
Wrap an existing map and make it case-insensitive. The passed in map is used to store the elements internally. Use this method if the HashMap used by default, doesn't meet your needs. For example you might want to specify another initial size or load factor, or another Map implementation. If the map contains any elements they will be clearedit and re-added again using the put method. This way null and non-string keys are ignored, and entries are overwritten if their keys are equal (case-insensitive check). So you could end up with less entries than in the passed in map.

Parameters:
someMap - the map to wrap
Returns:
a CaseInsensitiveMap wraping the passed in map

size

public int size()
Returns the number of entries in this map.

Specified by:
size in interface java.util.Map
Returns:
the number of entries in this map.

isEmpty

public boolean isEmpty()
Return true if this map contains no entries.

Specified by:
isEmpty in interface java.util.Map
Returns:
true if this map contains no entries.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key. The passed in key and the keys in the map are compared in a case-insensitive way.

Specified by:
containsKey in interface java.util.Map
Parameters:
key - the key to check if it exists in this Map.
Returns:
true if this map contains a mapping for the specified key.
Throws:
java.lang.ClassCastException - if the passed in key isn't a String

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map maps at least one key to the specified value.

Specified by:
containsValue in interface java.util.Map
Parameters:
value - the value to check if it exists in this Map
Returns:
true if this map maps at least one key to the specified value.

get

public java.lang.Object get(java.lang.Object key)
Returns the value mapped to the specified key or null if the specified key isn't mapped to any value. A return value of null does not necessarily indicate that the map contains no mapping for the key; it is also possible that the map explicitly maps the key to null. The containsKey method may be used to distinguish these two cases.

Specified by:
get in interface java.util.Map
Parameters:
key - the key whose associated value is to be returned
Returns:
the value mapped to the specified key or null if the specified key isn't mapped to any value.
Throws:
java.lang.ClassCastException - if the passed in key isn't a String

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key. If the map previously contained a mapping with a key that is equal (case-insensitive comparition) to the passed in key, the old mapping is removed and the old value is returned from the method.

Specified by:
put in interface java.util.Map
Parameters:
key - the key to be associated with the value
value - the value to be associated with the key
Returns:
the value that was previously associated with the specified key or null if no previous association existed. Null can also be returned if the specified key was previously associated with the null value.
Throws:
java.lang.NullPointerException - if the passed in key is null
java.lang.ClassCastException - if the passed in key isn't a String

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this map if present. The specified key and the keys from the mapping are compared in a case-insensitive manner.

Specified by:
remove in interface java.util.Map
Parameters:
key - the key whose mapping is to be removed from the map
Returns:
the value that was associated with the key or null if there was no mapping for the specified key. A value of null can also be returned if the specified key was associated with the null value.
Throws:
java.lang.ClassCastException - if the passed in key isn't a String

putAll

public void putAll(java.util.Map otherMap)
Iterates over all the mappings of the specified map and adds them with put(key, value) to this map. The method ignores (doesn't add) null and non-string keys.

Specified by:
putAll in interface java.util.Map
Parameters:
otherMap - Map containing the mappings to be added to this map

clear

public void clear()
Removes all the mappings from this map.

Specified by:
clear in interface java.util.Map

keySet

public java.util.Set keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
keySet in interface java.util.Map
Returns:
a set view of the keys contained in this map

values

public java.util.Collection values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface java.util.Map
Returns:
a collection view of the values contained in this map.

entrySet

public java.util.Set entrySet()
Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map
Returns:
a set view of the mappings contained in this map.

equals

public boolean equals(java.lang.Object obj)
Compares this map with the passed in object for equality. Returns true if the passed in object is also a CaseInsensitiveMap and the entrySets of the two maps are equal.

Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare with
Returns:
true if the passed in object and this map are equal

hashCode

public int hashCode()
Returns the hash code of this map.

Specified by:
hashCode in interface java.util.Map
Overrides:
hashCode in class java.lang.Object
Returns:
the hash code of this map.

toString

public java.lang.String toString()
Returns the string representation of this map.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this map.
See Also:
Object.toString()


Copyright © 2005 Frank W. Zammetti