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






CaseBlindHashMap - a HashMap extension, using Strings as key values.

    

/**********************************************************************************
 *
 * 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.
 * <p>
 * Internally, keys are case insensitive: <code>ABC</code> = <code>abc</code>.
 * <p>
 * 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).
   * <p>
   * 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();
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Ordered Map
2.Case Insensitive Map
3.A Map collection with real-time behavior
4.Cache Map
5.Map implementation Optimized for Strings keys
6.An integer hashmap
7.An IdentityMap that uses reference-equality instead of object-equality
8.Int Object HashMap
9.Concurrent Skip List Map
10.A hash map that uses primitive ints for the key rather than objects.
11.Integer Map
12.Copy On Write Map
13.Expiring Map
14.Array Map
15.Int Object HashMap (from CERN)
16.Int HashMap from jodd.org
17.String Map
18.List Map
19.Map using Locale objects as keys
20.Map with keys iterated in insertion order
21.Most Recently Used Map
22.Multi Map
23.MultiMap is a Java version of the C++ STL class std::multimap
24.Object Int Map
25.Sequenced HashMap
26.Int Int Map
27.Int Object Map
28.Identity HashMap
29.A java.util.Map interface which can only hold a single object
30.A multi valued Map
31.A simple hashmap from keys to integers
32.A memory-efficient hash map.
33.An implementation of the java.util.Map interface which can only hold a single object.
34.Utility methods for operating on memory-efficient maps.
35.A fixed size map implementation.
36.Int HashMap
37.IntMap provides a simple hashmap from keys to integers
38.Complex Key HashMap
39.A Map with multiple values for a key
40.A Map that accepts int or Integer keys only
41.A Map where keys are compared by object identity, rather than equals()
42.Type-safe Map, from char array to String value
43.A hashtable-based Map implementation with soft keys
44.List ordered map
45.Hash map using String values as keys mapped to primitive int values.
46.Lookup table that stores a list of strings
47.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.
48.Combines multiple values to form a single composite key. MultiKey can often be used as an alternative to nested maps.