1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.util.MultiValueMap;
18  
19  import java.io.Serializable;
20  
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  /**
28   * <a href="MemoryMultiValueMap.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Alexander Chow
31   */
32  public class MemoryMultiValueMap<K extends Serializable, V extends Serializable>
33      extends MultiValueMap<K, V> {
34  
35      public void clear() {
36          _map.clear();
37      }
38  
39      public boolean containsKey(Object key) {
40          return _map.containsKey(key);
41      }
42  
43      public boolean containsValue(Object value) {
44          for (K key : keySet()) {
45              Set<V> values = getAll(key);
46  
47              if (values.contains(value)) {
48                  return true;
49              }
50          }
51  
52          return false;
53      }
54  
55      public Set<V> getAll(Object key) {
56          return _map.get(key);
57      }
58  
59      public boolean isEmpty() {
60          return _map.isEmpty();
61      }
62  
63      public Set<K> keySet() {
64          return _map.keySet();
65      }
66  
67      public V put(K key, V value) {
68          Set<V> values = _map.get(key);
69  
70          if (values == null) {
71              values = new HashSet<V>();
72          }
73  
74          values.add(value);
75  
76          _map.put(key, values);
77  
78          return value;
79      }
80  
81      public Set<V> putAll(K key, Collection<? extends V> values) {
82          Set<V> oldValues = _map.get(key);
83  
84          if (oldValues == null) {
85              oldValues = new HashSet<V>();
86          }
87  
88          oldValues.addAll(values);
89  
90          _map.put(key, oldValues);
91  
92          return oldValues;
93      }
94  
95      public V remove(Object key) {
96          V value = null;
97  
98          Set<V> values = _map.remove(key);
99  
100         if ((values != null) && !values.isEmpty()) {
101             value = values.iterator().next();
102         }
103 
104         return value;
105     }
106 
107     private Map<K, Set<V>> _map = new HashMap<K, Set<V>>();
108 
109 }