String
s as key
* values.
*
* Internally, keys are case insensitive: ABC
= abc
.
*
* Two methods have been added to facilitate working with Sets of key strings.
* See stringKeySet()
and stringKeyIterator()
.
*/
public class CaseBlindHashMap extends HashMap {
/**
* Constructors
*/
public CaseBlindHashMap() {
super();
}
public CaseBlindHashMap(CaseBlindHashMap map) {
super(map);
}
public CaseBlindHashMap(int initCap) {
super(initCap);
}
public CaseBlindHashMap(int initCap, float loadFactor) {
super(initCap, loadFactor);
}
/*
* Extensions
*/
/**
* Get the set of keys contained in this map. Keys values are returned as
* simple String
s (not the CaseBlindString
s
* used internally).
*
* This is accopmlished by making a copy of the original map - modifications * made to this copy are not reflected in the original. * * @return The set of keys */ public Set stringKeySet() { Iterator iterator = super.keySet().iterator(); HashMap stringKeys = new HashMap(); while (iterator.hasNext()) { String key = ((CaseBlindString) iterator.next()).toString(); stringKeys.put(key, get(key)); } return stringKeys.keySet(); } /** * Get an Iterator to the String based key set * * @return An iterator to the key set */ public Iterator stringKeyIterator() { return stringKeySet().iterator(); } /* * Overridden HashMap methods */ /** * Does the map contain this key? * * @param key * The key to look up * @return true If the key is present in the map */ public boolean containsKey(String key) { return super.containsKey(new CaseBlindString(key)); } /** * Fetch a value by name - null keys are not supported * * @param key * The key to look up * @return The associated value object */ public Object get(String key) { return super.get(new CaseBlindString(key)); } /** * Add the key/value pair to the map - null values are not supported * * @param key * The key name * @param value * The object to store */ public void put(String key, Object value) { super.put(new CaseBlindString(key), value); } /** * Remove a key/value pair from this map * * @param key * Non-null key to remove */ public void remove(String key) { if (key == null) { throw new UnsupportedOperationException("null key"); } super.remove(new CaseBlindString(key)); } /** * A crude, case insensitive string - used internally to represent key values. * Preserve the originl case, but compare for equality in a case blind * fashion. */ public static class CaseBlindString { String string; /** * Constructors */ private CaseBlindString() { } public CaseBlindString(String string) { this.string = string; } /** * Fetch the original string * * @return The original string */ public String toString() { return string; } /** * Case insensitive compare * * @return True if the two strings match */ public boolean equals(Object object) { if (string == null) { return string == null; } return string.equalsIgnoreCase(((CaseBlindString) object).toString()); } /** * Get a hash code for this case insensitive string * * @return Hash code value */ public int hashCode() { if (string == null) { return "null".hashCode(); } return string.toUpperCase().hashCode(); } } }