forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractMultiMap.java
More file actions
133 lines (115 loc) · 3.2 KB
/
AbstractMultiMap.java
File metadata and controls
133 lines (115 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package soot.util;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import heros.solver.Pair;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public abstract class AbstractMultiMap<K, V> implements MultiMap<K, V>, Serializable {
private static final long serialVersionUID = 4558567794548019671L;
private class EntryIterator implements Iterator<Pair<K, V>> {
Iterator<K> keyIterator = keySet().iterator();
Iterator<V> valueIterator = null;
K currentKey = null;
@Override
public boolean hasNext() {
if (valueIterator != null && valueIterator.hasNext()) {
return true;
}
// Prepare for the next key
valueIterator = null;
currentKey = null;
return keyIterator.hasNext();
}
@Override
public Pair<K, V> next() {
// Obtain the next key
if (valueIterator == null) {
currentKey = keyIterator.next();
valueIterator = get(currentKey).iterator();
}
return new Pair<K, V>(currentKey, valueIterator.next());
}
@Override
public void remove() {
if (valueIterator == null) {
// Removing an element twice or removing no valid element does not make sense
return;
}
valueIterator.remove();
if (get(currentKey).isEmpty()) {
keyIterator.remove();
valueIterator = null;
currentKey = null;
}
}
}
@Override
public boolean putAll(MultiMap<K, V> m) {
boolean hasNew = false;
for (K key : m.keySet()) {
if (putAll(key, m.get(key))) {
hasNew = true;
}
}
return hasNew;
}
@Override
public boolean putAll(Map<K, Collection<V>> m) {
boolean hasNew = false;
for (K key : m.keySet()) {
if (putAll(key, m.get(key))) {
hasNew = true;
}
}
return hasNew;
}
@Override
public boolean isEmpty() {
return numKeys() == 0;
}
@Override
public boolean contains(K key, V value) {
Set<V> set = get(key);
if (set == null) {
return false;
}
return set.contains(value);
}
@Override
public Iterator<Pair<K, V>> iterator() {
return new EntryIterator();
}
@Override
public boolean putMap(Map<K, V> m) {
boolean hasNew = false;
for (Entry<K, V> entry : m.entrySet()) {
if (put(entry.getKey(), entry.getValue())) {
hasNew = true;
}
}
return hasNew;
}
}