Class IgnoreCaseMap<V>

java.lang.Object
java.util.AbstractMap<K,V>
com.twelvemonkeys.util.IgnoreCaseMap<V>
All Implemented Interfaces:
Serializable, Cloneable, Map<String,V>

public class IgnoreCaseMap<V> extends AbstractMap<K,V> implements Serializable, Cloneable
A Map decorator that makes the mappings in the backing map case insensitive (this is implemented by converting all keys to uppercase), if the keys used are Strings. If the keys used are not Strings, it wil work as a normal java.util.Map.
Author:
Harald Kuhr
See Also:
  • Field Details

  • Constructor Details

    • IgnoreCaseMap

      public IgnoreCaseMap()
      Constructs a new empty Map. The backing map will be a HashMap
    • IgnoreCaseMap

      public IgnoreCaseMap(Map<String,? extends V> pMap)
      Constructs a new Map with the same key-value mappings as the given Map. The backing map will be a HashMap

      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:
      pMap - the map whose mappings are to be placed in this map.
    • IgnoreCaseMap

      public IgnoreCaseMap(Map pBacking, Map<String,? extends V> pContents)
      Constructs a new Map with the same key-value mappings as the given Map.

      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 as pContents.
      pContents - the map whose mappings are to be placed in this map. May be null
      Throws:
      IllegalArgumentException - if pBacking is null
      IllegalArgumentException - if pBacking differs from pContent and is not empty.
  • Method Details

    • put

      public V put(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:
      put in interface Map<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(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:
      get in interface Map<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(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:
      remove in interface Map<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(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:
      containsKey in interface Map<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 Object toUpper(Object pObject)
      Converts the parameter to uppercase, if it's a String.
    • newEntryIterator

      protected Iterator<Map.Entry<String,V>> newEntryIterator()
    • newKeyIterator

      protected Iterator<String> newKeyIterator()
    • newValueIterator

      protected Iterator<V> newValueIterator()
    • init

      protected void init()
      Default implementation, does nothing.
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object pValue)
      Returns true if this map maps one or more keys to the specified pValue. More formally, returns true if and only if this map contains at least one mapping to a pValue v such that (pValue==null ? v==null : pValue.equals(v)).

      This implementation requires time linear in the map size for this operation.

      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Parameters:
      pValue - pValue whose presence in this map is to be tested.
      Returns:
      true if this map maps one or more keys to the specified pValue.
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<String,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • clone

      protected Object clone() throws CloneNotSupportedException
      Returns a shallow copy of this AbstractMap instance: the keys and values themselves are not cloned.
      Overrides:
      clone in class AbstractMap<K,V>
      Returns:
      a shallow copy of this map.
      Throws:
      CloneNotSupportedException
    • removeEntry

      protected Map.Entry<String,V> removeEntry(Map.Entry<String,V> pEntry)
      Removes the given entry from the Map.
      Parameters:
      pEntry - the entry to be removed
      Returns:
      the removed entry, or null if nothing was removed.