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.kernel.util;
16  
17  import java.io.Serializable;
18  
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  
24  /**
25   * <a href="MultiValueMap.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Alexander Chow
28   */
29  public abstract class MultiValueMap
30      <K extends Serializable, V extends Serializable> implements Map<K, V> {
31  
32      public Set<Map.Entry<K, V>> entrySet() {
33          throw new UnsupportedOperationException();
34      }
35  
36      public V get(Object key) {
37          throw new UnsupportedOperationException();
38      }
39  
40      public abstract Set<V> getAll(Object key);
41  
42      public abstract Set<V> putAll(K key, Collection<? extends V> values);
43  
44      public void putAll(Map<? extends K, ? extends V> map) {
45          MultiValueMap<? extends K, ? extends V> multiValueMap = null;
46  
47          if (map instanceof MultiValueMap<?, ?>) {
48              multiValueMap = (MultiValueMap<? extends K, ? extends V>)map;
49          }
50  
51          for (K key : map.keySet()) {
52              if (multiValueMap != null) {
53                  putAll(key, multiValueMap.getAll(key));
54              }
55              else {
56                  put(key, map.get(key));
57              }
58          }
59      }
60  
61      public int size() {
62          int size = 0;
63  
64          for (K key : keySet()) {
65              size += size(key);
66          }
67  
68          return size;
69      }
70  
71      public int size(Object key) {
72          int size = 0;
73  
74          Collection<V> values = getAll(key);
75  
76          if (values != null) {
77              size = values.size();
78          }
79  
80          return size;
81      }
82  
83      public Collection<V> values() {
84          Set<V> values = new HashSet<V>();
85  
86          Set<K> keys = keySet();
87  
88          for (K key : keys) {
89              values.addAll(getAll(key));
90          }
91  
92          return values;
93      }
94  
95  }