RefEntryIndex Class

package org.mcraig.cs445.refentry;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Hash of RefEntry documents grouped according to search strings.
 * @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
 */
class RefEntryIndex implements Serializable {
    /** Default constructor. */
    RefEntryIndex() {
        Index = new HashMap(); 
    }
    /**
     * Add an existing RefEntry document for a particular
     * string. The string is folded to lower case.
     * @param key String to match later.
     * @param value The RefEntry to add.
     */
    boolean add(String key, RefEntry value) {
        if (key == null || value == null) return false;

        key = key.toLowerCase();
        HashSet values = new HashSet();
        if (!Index.isEmpty() && Index.get(key) != null)
            values.addAll((HashSet)Index.get(key));
        if (!values.add(value)) return false;
        Index.put(key, values);
        return true;
    }
    /**
     * Get the set of RefEntry documents matching a string.
     * The string is folded to lower case.
     * @param key String to match.
     */
    HashSet match(String key) {
        if (key == null) return (new HashSet());
        key = key.toLowerCase();
	if (Index.get(key) == null) return (new HashSet());
        HashSet matches = new HashSet((HashSet)Index.get(key));
        return matches;
    }
    /** Structure containing the indexed RefEntry documents. */
    private HashMap Index;
}