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

gov.nist.core.MultiValueMapImpl Maven / Gradle / Ivy

There is a newer version: 1.3.0-91
Show newest version
/*
 * Conditions Of Use
 *
 * This software was developed by employees of the National Institute of
 * Standards and Technology (NIST), an agency of the Federal Government.
 * Pursuant to title 15 Untied States Code Section 105, works of NIST
 * employees are not subject to copyright protection in the United States
 * and are considered to be in the public domain.  As a result, a formal
 * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement.
 *
 */
package android.gov.nist.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MultiValueMapImpl implements MultiValueMap, Cloneable {
	// jeand : lazy init of the map to reduce mem consumption
    private HashMap> map = null;

    private static final long serialVersionUID = 4275505380960964605L;

    public MultiValueMapImpl() {
        super();
    }

    public List put(String key, V value) {    	
        ArrayList keyList = null;
        if(map != null) {
        	keyList = map.get(key);
        }
        if (keyList == null) {
            keyList = new ArrayList();
            getMap().put(key, keyList);
        }

        keyList.add(value);
        return keyList;
    }

    public boolean containsValue(Object value) {
        Set pairs = null;
        if(map != null) {
        	pairs = map.entrySet();
        }
        if (pairs == null)
            return false;

        Iterator pairsIterator = pairs.iterator();
        while (pairsIterator.hasNext()) {
            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
            ArrayList list = (ArrayList) (keyValuePair.getValue());
            if (list.contains(value))
                return true;
        }
        return false;
    }

    public void clear() {
    	if(map != null) {
	        Set pairs = map.entrySet();
	        Iterator pairsIterator = pairs.iterator();
	        while (pairsIterator.hasNext()) {
	            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
	            ArrayList list = (ArrayList) (keyValuePair.getValue());
	            list.clear();
	        }
	        map.clear();
    	}
    }

    public Collection values() {
    	if(map == null) {
    		return new ArrayList();
    	}
        ArrayList returnList = new ArrayList(map.size());

        Set pairs = map.entrySet();
        Iterator pairsIterator = pairs.iterator();
        while (pairsIterator.hasNext()) {
            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
            ArrayList list = (ArrayList) (keyValuePair.getValue());

            Object[] values = list.toArray();
            for (int ii = 0; ii < values.length; ii++) {
                returnList.add(values[ii]);
            }
        }
        return returnList;
    }

    public Object clone() {
        MultiValueMapImpl obj = new MultiValueMapImpl();
        if(map != null) {
        	obj.map = (HashMap>) this.map.clone();
        }
        return obj;
    }

    public int size() {
    	if(map == null) {
    		return 0;
    	}
        return this.map.size();
    }

    public boolean containsKey(Object key) {
    	if(map == null) {
    		return false;
    	}
        return map.containsKey(key);
    }

    public Set entrySet() {
    	if(map == null) {
    		return new HashSet();
    	}
        return map.entrySet();
    }

    public boolean isEmpty() {
    	if(map == null) {
    		return true;
    	}
        return map.isEmpty();
    }

    public Set keySet() {
    	if(map == null) {
    		return new HashSet();
    	}
        return this.map.keySet();
    }

    public Object removeKV(String key, V item) {
    	if(map == null) {
    		return null;
    	}
        ArrayList list = this.map.get(key);
        if (list == null) {
            return null;
        } else {
            return list.remove(item);
        }
    }

    public List get(Object key) {
    	if(map == null) {
    		return null;
    	}
        return map.get(key);
    }

    public List put(String key, List value) {
        return this.getMap().put(key,(ArrayList) value);
    }

    public List remove(Object key) {
    	if(map == null) {
    		return null;
    	}
        return map.remove(key);
    }
    
    public void putAll(Map< ? extends String, ? extends List> mapToPut) {
        for (String k : mapToPut.keySet()) {
            ArrayList al = new ArrayList();
            al.addAll(mapToPut.get(k));
            getMap().put(k, al);
        }  
    }


	/**
	 * @return the map
	 */
	public HashMap> getMap() {
		if(map == null) {
			map = new HashMap>(0);
		}
		return map;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy