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 bndlib Show documentation
Show all versions of bndlib Show documentation
A Swiss Army Knife for OSGi
package aQute.lib.collections;
import java.util.*;
public class MultiMap extends HashMap> {
private static final long serialVersionUID = 1L;
final Set EMPTY = Collections.emptySet();
public boolean add( K key, V value ) {
Set set = get(key);
if ( set == null) {
set=new HashSet();
put(key,set);
}
return set.add(value);
}
public boolean addAll( K key, Collection value ) {
Set set = get(key);
if ( set == null) {
set=new HashSet();
put(key,set);
}
return set.addAll(value);
}
public boolean remove( K key, V value ) {
Set 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 ) {
Set 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) {
Set set = get(key);
if ( set == null)
return EMPTY.iterator();
else
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();
}
};
}
}