1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }