CaseBlindHashMap - a HashMap extension, using Strings as key values. : Customized Map « Collections « Java Tutorial






/**********************************************************************************
 *
 * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
 *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
 *
 * Licensed under the Educational Community License Version 1.0 (the "License");
 * By obtaining, using and/or copying this Original Work, you agree that you have read,
 * understand, and will comply with the terms and conditions of the Educational Community License.
 * You may obtain a copy of the License at:
 *
 *      http://cvs.sakaiproject.org/licenses/license_1_0.html
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **********************************************************************************/

/*
 Copyright (c) 2000-2003 Board of Trustees of Leland Stanford Jr. University,
 all rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 Except as contained in this notice, the name of Stanford University shall not
 be used in advertising or otherwise to promote the sale, use or other dealings
 in this Software without prior written authorization from Stanford University.
 */

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * CaseBlindHashMap - a HashMap extension, using <code>String</code>s as key
 * values.
 * 
 * Internally, keys are case insensitive: <code>ABC</code> = <code>abc</code>.
 * 
 * Two methods have been added to facilitate working with Sets of key strings.
 * See <code>stringKeySet()</code> and <code>stringKeyIterator()</code>.
 */
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 <code>String</code>s (not the <code>CaseBlindString</code>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();
    }
  }
}








9.33.Customized Map
9.33.1.Implementation of a bit map of any size, together with static methods to manipulate int, byte and byte[] values as bit maps
9.33.2.A fixed size map implementation.
9.33.3.A memory-efficient hash map.
9.33.4.CaseBlindHashMap - a HashMap extension, using Strings as key values.
9.33.5.Clones a map and prefixes the keys in the clone
9.33.6.Converts array into a java.util.Map.
9.33.7.Ordered Map
9.33.8.A hash map that uses primitive ints for the key rather than objects.
9.33.9.HashNMap stores multiple values by a single key value. Values can be retrieved using a direct query or by creating an enumeration over the stored elements.
9.33.10.A Map where keys are compared by object identity, rather than equals()
9.33.11.A java.util.Map implementation using reference values
9.33.12.A simple hashmap from keys to integers
9.33.13.An IdentityMap that uses reference-equality instead of object-equality
9.33.14.Creates a mutable map from two arrays with keys and values
9.33.15.Fixed size hash map using String values as keys mapped to primitive int values.
9.33.16.Hash map for counting references to Object keys.
9.33.17.Hash map using String values as keys mapped to primitive int values.
9.33.18.IntMap provides a simple hashmap from keys to integers
9.33.19.List ordered map
9.33.20.Lookup table that stores a list of strings
9.33.21.Map implementation Optimized for Strings keys
9.33.22.Map with keys iterated in insertion order