Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.collections; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 5 | public class MultiMap<K, V> extends HashMap<K,List<V>> { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 6 | private static final long serialVersionUID = 1L; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 7 | final boolean noduplicates; |
| 8 | final Class< ? > keyClass; |
| 9 | final Class< ? > valueClass; |
| 10 | |
| 11 | final Set<V> EMPTY = Collections.emptySet(); |
| 12 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | public MultiMap() { |
| 14 | noduplicates = false; |
| 15 | keyClass = Object.class; |
| 16 | valueClass = Object.class; |
| 17 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 18 | |
| 19 | public MultiMap(Class<K> keyClass, Class<V> valueClass, boolean noduplicates) { |
| 20 | this.noduplicates = noduplicates; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | this.keyClass = keyClass; |
| 22 | this.valueClass = valueClass; |
| 23 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 24 | |
| 25 | @SuppressWarnings("unchecked") |
| 26 | public boolean add(K key, V value) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | assert keyClass.isInstance(key); |
| 28 | assert valueClass.isInstance(value); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 29 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | List<V> set = get(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 31 | if (set == null) { |
| 32 | set = new ArrayList<V>(); |
| 33 | if (valueClass != Object.class) { |
| 34 | set = Collections.checkedList(set, (Class<V>) valueClass); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 36 | put(key, set); |
| 37 | } else { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | if (noduplicates) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 39 | if (set.contains(value)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | return set.add(value); |
| 44 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 45 | |
| 46 | @SuppressWarnings("unchecked") |
| 47 | public boolean addAll(K key, Collection< ? extends V> value) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | assert keyClass.isInstance(key); |
| 49 | List<V> set = get(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 50 | if (set == null) { |
| 51 | set = new ArrayList<V>(); |
| 52 | if (valueClass != Object.class) { |
| 53 | set = Collections.checkedList(set, (Class<V>) valueClass); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 55 | put(key, set); |
| 56 | } else if (noduplicates) { |
| 57 | boolean r = false; |
| 58 | for (V v : value) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 59 | assert valueClass.isInstance(v); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 60 | if (!set.contains(value)) |
| 61 | r |= set.add(v); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 62 | } |
| 63 | return r; |
| 64 | } |
| 65 | return set.addAll(value); |
| 66 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 67 | |
| 68 | public boolean remove(K key, V value) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | assert keyClass.isInstance(key); |
| 70 | assert valueClass.isInstance(value); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 71 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 72 | List<V> set = get(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 73 | if (set == null) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | boolean result = set.remove(value); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 77 | if (set.isEmpty()) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | remove(key); |
| 79 | return result; |
| 80 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 81 | |
| 82 | public boolean removeAll(K key, Collection<V> value) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 83 | assert keyClass.isInstance(key); |
| 84 | List<V> set = get(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 85 | if (set == null) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | boolean result = set.removeAll(value); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 89 | if (set.isEmpty()) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 90 | remove(key); |
| 91 | return result; |
| 92 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 93 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | public Iterator<V> iterate(K key) { |
| 95 | assert keyClass.isInstance(key); |
| 96 | List<V> set = get(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 97 | if (set == null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 98 | return EMPTY.iterator(); |
| 99 | return set.iterator(); |
| 100 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 101 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | public Iterator<V> all() { |
| 103 | return new Iterator<V>() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 104 | Iterator<List<V>> master = values().iterator(); |
| 105 | Iterator<V> current = null; |
| 106 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | public boolean hasNext() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 108 | if (current == null || !current.hasNext()) { |
| 109 | if (master.hasNext()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 110 | current = master.next().iterator(); |
| 111 | return current.hasNext(); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | public V next() { |
| 119 | return current.next(); |
| 120 | } |
| 121 | |
| 122 | public void remove() { |
| 123 | current.remove(); |
| 124 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 125 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 126 | }; |
| 127 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 128 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 129 | public Map<K,V> flatten() { |
| 130 | Map<K,V> map = new LinkedHashMap<K,V>(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 131 | for (Map.Entry<K,List<V>> entry : entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 132 | List<V> v = entry.getValue(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 133 | if (v == null || v.isEmpty()) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 134 | continue; |
| 135 | |
| 136 | map.put(entry.getKey(), v.get(0)); |
| 137 | } |
| 138 | return map; |
| 139 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 140 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 141 | public MultiMap<V,K> transpose() { |
| 142 | MultiMap<V,K> inverted = new MultiMap<V,K>(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 143 | for (Map.Entry<K,List<V>> entry : entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 144 | K key = entry.getKey(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 145 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 146 | List<V> value = entry.getValue(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 147 | if (value == null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 148 | continue; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 149 | |
| 150 | for (V v : value) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 151 | inverted.add(v, key); |
| 152 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 153 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 154 | return inverted; |
| 155 | } |
| 156 | } |