All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.com.generationjava.collections.MultiKeyedMap Maven / Gradle / Ivy

Go to download

A re-usable library of Java classes. Originally stand-alone, many of the classes have since gone into various parts of the Jakarta Commons project, and thus this package contains various dependencies.

The newest version!
/*
 * Copyright (c) 2003, Henri Yandell
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the 
 * following conditions are met:
 * 
 * + Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * 
 * + Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 * 
 * + Neither the name of Genjava-Core nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.generationjava.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

/**
 * A Map in which multiple keys are used, ie) an Object array 
 * of keys. 
 *
 * @author [email protected]
 * @date 2001-12-19
 */
/// Pass in:  { "key1", "key2", "key3" } and a value.
public class MultiKeyedMap implements Map {

    // this is not lazy instantiation. One MUST be 
    // constructed in a constructor.
    private Map myMap = null;

    public MultiKeyedMap(Map m) {
        myMap = m;
    }

    public MultiKeyedMap() {
        this(new HashMap());
    }

    /* map interface */
    
    // Removes all mappings from this map (optional operation) {. 
    public void clear() { 
        myMap.clear();
    }

    // Returns true if this map contains a mapping for the specified key. 
    public boolean containsKey(Object key) { 
        return get(key) != null;
    }

    // Returns true if this map maps one or more keys to the specified value. 
    public boolean containsValue(Object value) { 
        return this.containsValue(this.myMap, value);
    }

    private boolean containsValue(Map map, Object value) {
        Iterator values = map.values().iterator();
        while(values.hasNext()) {
            Object obj = values.next();
            if(obj instanceof Map) {
                if(containsValue((Map)obj, value) ) {
                    return true;
                }
            }
            if(value == obj) {
                return true;
            }
            if(value.equals(obj)) {
                return true;
            }
        }

        return false;
    }

    // Returns a set view of the mappings contained in this map. 
    public Set entrySet() { 
        // FIX  : Make this actually work.
        return myMap.entrySet();
    }

    // Compares the specified object with this map for equality. 
    public boolean equals(Object o) {
        return myMap.equals(o);
    }

    // Returns the value to which this map maps the specified key. 
    public Object get(Object key) { 
        if(key instanceof Object[]) {
            Object[] keys = (Object[])key;
            Map map = this.myMap;
            int szLessOne = keys.length - 1;
            for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy