1/*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government.
6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain.  As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement.
22 *
23 */
24package gov.nist.core;
25
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.HashMap;
29import java.util.Iterator;
30import java.util.List;
31import java.util.Map;
32import java.util.Set;
33
34public class MultiValueMapImpl<V> implements MultiValueMap<String, V>, Cloneable {
35    private HashMap<String, ArrayList<V>> map = new HashMap<String, ArrayList<V>>();
36
37    private static final long serialVersionUID = 4275505380960964605L;
38
39    public MultiValueMapImpl() {
40        super();
41
42    }
43
44    public List<V> put(String key, V value) {
45        ArrayList<V> keyList = map.get(key);
46        if (keyList == null) {
47            keyList = new ArrayList<V>(10);
48            map.put(key, keyList);
49        }
50
51        keyList.add(value);
52        return keyList;
53    }
54
55    public boolean containsValue(Object value) {
56        Set pairs = map.entrySet();
57
58        if (pairs == null)
59            return false;
60
61        Iterator pairsIterator = pairs.iterator();
62        while (pairsIterator.hasNext()) {
63            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
64            ArrayList list = (ArrayList) (keyValuePair.getValue());
65            if (list.contains(value))
66                return true;
67        }
68        return false;
69    }
70
71    public void clear() {
72        Set pairs = map.entrySet();
73        Iterator pairsIterator = pairs.iterator();
74        while (pairsIterator.hasNext()) {
75            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
76            ArrayList list = (ArrayList) (keyValuePair.getValue());
77            list.clear();
78        }
79        map.clear();
80    }
81
82    public Collection values() {
83        ArrayList returnList = new ArrayList(map.size());
84
85        Set pairs = map.entrySet();
86        Iterator pairsIterator = pairs.iterator();
87        while (pairsIterator.hasNext()) {
88            Map.Entry keyValuePair = (Map.Entry) (pairsIterator.next());
89            ArrayList list = (ArrayList) (keyValuePair.getValue());
90
91            Object[] values = list.toArray();
92            for (int ii = 0; ii < values.length; ii++) {
93                returnList.add(values[ii]);
94            }
95        }
96        return returnList;
97    }
98
99    public Object clone() {
100        MultiValueMapImpl obj = new MultiValueMapImpl<V>();
101        obj.map = (HashMap<Object, ArrayList<V>>) this.map.clone();
102        return obj;
103    }
104
105    public int size() {
106        return this.map.size();
107    }
108
109    public boolean containsKey(Object key) {
110        return map.containsKey(key);
111    }
112
113    public Set entrySet() {
114        return map.entrySet();
115    }
116
117    public boolean isEmpty() {
118        return map.isEmpty();
119    }
120
121    public Set<String> keySet() {
122        return this.map.keySet();
123    }
124
125    public Object remove(String key, V item) {
126        ArrayList<V> list = this.map.get(key);
127        if (list == null) {
128            return null;
129        } else {
130            return list.remove(item);
131        }
132    }
133
134    public List<V> get(Object key) {
135        return map.get(key);
136    }
137
138    public List<V> put(String key, List<V> value) {
139        return this.map.put(key,(ArrayList<V>) value);
140    }
141
142    public List<V> remove(Object key) {
143        return map.remove(key);
144    }
145
146    public void putAll(Map< ? extends String, ? extends List<V>> mapToPut) {
147        for (String k : mapToPut.keySet()) {
148            ArrayList<V> al = new ArrayList<V>();
149            al.addAll(mapToPut.get(k));
150            this.map.put(k, al);
151        }
152    }
153
154}
155