All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.lib.collections.MultiMap Maven / Gradle / Ivy

There is a newer version: 2.0.0.20130123-133441
Show newest version
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();
			}
			
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy