aQute.lib.collections.MultiMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bnd Show documentation
Show all versions of bnd Show documentation
A command line utility and Ant plugin to wrap, build, or examine bundles.
package aQute.lib.collections;
import java.util.*;
public class MultiMap extends HashMap> implements Map> {
private static final long serialVersionUID = 1L;
final boolean noduplicates;
final Class< ? > keyClass;
final Class< ? > valueClass;
final Set EMPTY = Collections.emptySet();
public MultiMap() {
noduplicates = false;
keyClass = Object.class;
valueClass = Object.class;
}
public MultiMap(Class keyClass, Class valueClass, boolean noduplicates) {
this.noduplicates = noduplicates;
this.keyClass = keyClass;
this.valueClass = valueClass;
}
public MultiMap(Map> other) {
this();
for ( java.util.Map.Entry> e : other.entrySet()) {
addAll(e.getKey(), e.getValue());
}
}
public MultiMap(MultiMap other) {
keyClass = other.keyClass;
valueClass = other.valueClass;
noduplicates = other.noduplicates;
for ( java.util.Map.Entry> e : other.entrySet()) {
addAll(e.getKey(), e.getValue());
}
}
@SuppressWarnings("unchecked")
public boolean add(K key, V value) {
assert keyClass.isInstance(key);
assert valueClass.isInstance(value);
List set = get(key);
if (set == null) {
set = new ArrayList();
if (valueClass != Object.class) {
set = Collections.checkedList(set, (Class) valueClass);
}
put(key, set);
} else {
if (noduplicates) {
if (set.contains(value))
return false;
}
}
return set.add(value);
}
@SuppressWarnings("unchecked")
public boolean addAll(K key, Collection< ? extends V> value) {
assert keyClass.isInstance(key);
List set = get(key);
if (set == null) {
set = new ArrayList();
if (valueClass != Object.class) {
set = Collections.checkedList(set, (Class) valueClass);
}
put(key, set);
} else if (noduplicates) {
boolean r = false;
for (V v : value) {
assert valueClass.isInstance(v);
if (!set.contains(v))
r |= set.add(v);
}
return r;
}
return set.addAll(value);
}
public boolean remove(K key, V value) {
assert keyClass.isInstance(key);
assert valueClass.isInstance(value);
List set = get(key);
if (set == null) {
return false;
}
boolean result = set.remove(value);
if (set.isEmpty())
remove(key);
return result;
}
public boolean removeAll(K key, Collection value) {
assert keyClass.isInstance(key);
List set = get(key);
if (set == null) {
return false;
}
boolean result = set.removeAll(value);
if (set.isEmpty())
remove(key);
return result;
}
public Iterator iterate(K key) {
assert keyClass.isInstance(key);
List set = get(key);
if (set == null)
return EMPTY.iterator();
return set.iterator();
}
public Iterator all() {
return new Iterator() {
Iterator> master = values().iterator();
Iterator current = null;
public boolean hasNext() {
if (current == null || !current.hasNext()) {
if (master.hasNext()) {
current = master.next().iterator();
return current.hasNext();
}
return false;
}
return true;
}
public V next() {
return current.next();
}
public void remove() {
current.remove();
}
};
}
public Map flatten() {
Map map = new LinkedHashMap();
for (Map.Entry> entry : entrySet()) {
List v = entry.getValue();
if (v == null || v.isEmpty())
continue;
map.put(entry.getKey(), v.get(0));
}
return map;
}
public MultiMap transpose() {
MultiMap inverted = new MultiMap();
for (Map.Entry> entry : entrySet()) {
K key = entry.getKey();
List value = entry.getValue();
if (value == null)
continue;
for (V v : value)
inverted.add(v, key);
}
return inverted;
}
/**
* Return a collection with all values
* @return
*/
public List allValues() {
List result = new ArrayList();
Iterator i = all();
while( i.hasNext())
result.add(i.next());
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy