Class StringMap

java.lang.Object
java.util.Dictionary
sunlabs.brazil.util.StringMap
Direct Known Subclasses:
AsteriskHandler.AmiStringMap, MimeHeaders

public class StringMap extends Dictionary
The StringMap class is a substitute for the Hashtable. The StringMap has the following properties:
  • Maps case-insensitive string keys to string values.
  • The case of the keys is preserved.
  • Values may be null.
  • Preserves the relative order of the data.
  • The same key may appear multiple times in a single map.
  • This map is implemented via a Vector, and as such, as the number of keys increases, the time required to search will go up.
Version:
2.5
Author:
Colin Stevens (colin.stevens@sun.com)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty StringMap.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(String key, String value)
    Maps the given case-insensitive key to the specified value in this StringMap.
    void
    append(StringMap other, boolean noReplace)
    Append another Stringmap onto this one.
    void
    Removes all the keys and values from this StringMap.
    Returns an enumeration of the values in this StringMap.
    get(int index)
    Returns the value at the specified index.
    get(Object key)
    Performs the same job as get(String).
    get(String key)
    Returns the value that the specified case-insensitive key maps to in this StringMap.
    get(String key, String dflt)
    Returns the value that the specified case-insensitive key maps to in this StringMap.
    getKey(int index)
    Returns the key at the specified index.
    boolean
    Tests if there are any elements in this StringMap.
    Returns an enumeration of the keys in this StringMap.
    void
    put(int index, String value)
    Maps the key at the given index to the specified value in this StringMap.
    put(Object key, Object value)
    Performs the same job as put(String, String).
    void
    put(String key, String value)
    Maps the given case-insensitive key to the specified value in this StringMap.
    void
    remove(int i)
     
    Performs the same job as remove(String).
    void
    Removes the given case-insensitive key and its corresponding value from this StringMap.
    int
    Returns the number of elements in this StringMap.
    Returns a string representation of this StringMap in the form of a set of entries, enclosed in braces and separated by the characters ", ".

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • StringMap

      public StringMap()
      Creates an empty StringMap.
  • Method Details

    • size

      public int size()
      Returns the number of elements in this StringMap. Every occurrence of keys that appear multiple times is counted.
      Specified by:
      size in class Dictionary
      Returns:
      The number of elements in this StringMap.
      See Also:
      • keys
    • isEmpty

      public boolean isEmpty()
      Tests if there are any elements in this StringMap.
      Specified by:
      isEmpty in class Dictionary
      Returns:
      Returns true if there are no elements, false otherwise.
    • keys

      public Enumeration keys()
      Returns an enumeration of the keys in this StringMap. The elements of the enumeration are strings.

      The same key may appear multiple times in the enumeration, not necessarily consecutively. Since get always returns the value associated with the first occurrence of a given key, a StringMap cannot be enumerated in the same fashion as a Hashtable. Instead, the caller should use:

      Enumeration keys = map.keys();
      Enumeration values = map.elements();
      while (keys.hasMoreElements()) {
          String key = (String) keys.nextElement();
          String value = (String) values.nextElement();
      }
      
      or:
      for (int i = 0; i < map.size(); i++) {
          String key = map.getKey(i);
          String value = map.get(i);
      }
      
      Specified by:
      keys in class Dictionary
      Returns:
      An enumeration of the keys.
      See Also:
    • elements

      public Enumeration elements()
      Returns an enumeration of the values in this StringMap. The elements of the enumeration are strings.
      Specified by:
      elements in class Dictionary
      Returns:
      An enumeration of the values.
      See Also:
      • keys
    • getKey

      public String getKey(int index) throws IndexOutOfBoundsException
      Returns the key at the specified index. The index ranges from 0 to size() - 1.

      This method can be used to iterate over all the keys in this StringMap in the order in which they were inserted, subject to any intervening deletions.

      Parameters:
      index - The index of the key.
      Returns:
      The key at the specified index.
      Throws:
      IndexOutOfBoundsException - if the index is out of the allowed range.
    • get

      public String get(int index) throws IndexOutOfBoundsException
      Returns the value at the specified index. The index ranges from 0 to size() - 1.

      This method can be used to iterate over all the values in this StringMap in the order in which they were inserted, subject to any intervening deletions.

      Parameters:
      index - The index of the key.
      Returns:
      The value at the specified index.
      Throws:
      IndexOutOfBoundsException - if the index is out of the allowed range.
    • get

      public String get(String key)
      Returns the value that the specified case-insensitive key maps to in this StringMap.

      The same key may appear multiple times in the enumeration; this method always returns the value associated with the first occurrence of the specified key. In order to get all the values, it is necessary to iterate over the entire StringMap to retrieve all the values associated with a given key.

      Parameters:
      key - A key in this StringMap. May not be null.
      Returns:
      The value to which the specified key is mapped, or null if the key is not in the StringMap.
      See Also:
      • keys
    • get

      public String get(String key, String dflt)
      Returns the value that the specified case-insensitive key maps to in this StringMap.
      Parameters:
      key - A key in this StringMap. May not be null.
      dflt - A default value if the entry for key is not found.
      Returns:
      The value to which the specified key is mapped, or dflt if the key is not in the StringMap.
    • get

      public Object get(Object key)
      Performs the same job as get(String). It exists so this class can extend the Dictionary class.
      Specified by:
      get in class Dictionary
      Parameters:
      key - Must be a String.
      Returns:
      A String value.
      Throws:
      ClassCastException - if the key is not a String.
      See Also:
    • put

      public void put(int index, String value)
      Maps the key at the given index to the specified value in this StringMap. The index ranges from 0 to size() - 1.
      Parameters:
      index - The index of the key.
      Throws:
      IndexOutOfBoundsException - if the index is out of the allowed range.
    • put

      public void put(String key, String value)
      Maps the given case-insensitive key to the specified value in this StringMap.

      The value can be retrieved by calling get with a key that is case-insensitive equal to the given key.

      If this StringMap already contained a mapping for the given key, the old value is forgotten and the new specified value is used. The case of the prior key is retained in that case. Otherwise the case of the new key is used.

      Parameters:
      key - The new key. May not be null.
      value - The new value. May be null.
    • put

      public Object put(Object key, Object value)
      Performs the same job as put(String, String). It exists so this class can extend the Dictionary class.
      Specified by:
      put in class Dictionary
      Parameters:
      key - Must be a String.
      value - Must be a String.
      Returns:
      The previous value to which key was mapped, or null if the the key did not map to any value.
      Throws:
      ClassCastException - if the key or value is not a String.
      See Also:
    • add

      public void add(String key, String value)
      Maps the given case-insensitive key to the specified value in this StringMap.

      The new mapping is added to this StringMap even if the given key already has a mapping. In this way it is possible to create a key that maps to two or more values.

      Since the same key may appear multiple times in this StringMap, it is necessary to iterate over the entire StringMap to retrieve all values associated with a given key.

      Parameters:
      key - The new key. May not be null.
      value - The new value. May be null.
      See Also:
    • remove

      public void remove(String key)
      Removes the given case-insensitive key and its corresponding value from this StringMap. This method does nothing if the key is not in this StringMap.

      The same key may appear in multiple times in this StringMap; this method only removes the first occurrence of the key.

      Parameters:
      key - The key that needs to be removed. Must not be null.
    • remove

      public void remove(int i)
    • remove

      public Object remove(Object key)
      Performs the same job as remove(String). It exists so this class can extend the Dictionary class.
      Specified by:
      remove in class Dictionary
      Parameters:
      key - Must be a String.
      Returns:
      The string value to which the key had been mapped, or null if the key did not have a mapping.
      Throws:
      ClassCastException - if the key is not a String.
    • clear

      public void clear()
      Removes all the keys and values from this StringMap.
    • append

      public void append(StringMap other, boolean noReplace)
      Append another Stringmap onto this one.
      Parameters:
      other - the map to append to this one
      noReplace - should existing values be replaced?
    • toString

      public String toString()
      Returns a string representation of this StringMap in the form of a set of entries, enclosed in braces and separated by the characters ", ". Each entry is rendered as the key, an equals sign "=", and the associated value.
      Overrides:
      toString in class Object
      Returns:
      The string representation of this StringMap.