| MemoryMultiValueMap.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.util;
24
25 import com.liferay.portal.kernel.util.MultiValueMap;
26
27 import java.io.Serializable;
28
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36 * <a href="MemoryMultiValueMap.java.html"><b><i>View Source</i></b></a>
37 *
38 * @author Alexander Chow
39 *
40 */
41 public class MemoryMultiValueMap<K extends Serializable, V extends Serializable>
42 extends MultiValueMap<K, V> {
43
44 public void clear() {
45 _map.clear();
46 }
47
48 public boolean containsKey(Object key) {
49 return _map.containsKey(key);
50 }
51
52 public boolean containsValue(Object value) {
53 for (K key : keySet()) {
54 Set<V> values = getAll(key);
55
56 if (values.contains(value)) {
57 return true;
58 }
59 }
60
61 return false;
62 }
63
64 public Set<V> getAll(Object key) {
65 return _map.get(key);
66 }
67
68 public boolean isEmpty() {
69 return _map.isEmpty();
70 }
71
72 public Set<K> keySet() {
73 return _map.keySet();
74 }
75
76 public V put(K key, V value) {
77 Set<V> values = _map.get(key);
78
79 if (values == null) {
80 values = new HashSet<V>();
81 }
82
83 values.add(value);
84
85 _map.put(key, values);
86
87 return value;
88 }
89
90 public Set<V> putAll(K key, Collection<? extends V> values) {
91 Set<V> oldValues = _map.get(key);
92
93 if (oldValues == null) {
94 oldValues = new HashSet<V>();
95 }
96
97 oldValues.addAll(values);
98
99 _map.put(key, oldValues);
100
101 return oldValues;
102 }
103
104 public V remove(Object key) {
105 V value = null;
106
107 Set<V> values = _map.remove(key);
108
109 if ((values != null) && !values.isEmpty()) {
110 value = values.iterator().next();
111 }
112
113 return value;
114 }
115
116 private Map<K, Set<V>> _map = new HashMap<K, Set<V>>();
117
118 }