IndexedSet.java

package org.microspace.table.column;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import org.microspace.annotation.IndexType;

/**
 * A set of field values. 
 * The type of the set is controlled by their @{link org.microspace.annotation.IndexType IndexType}.
 * @author Gaspar Sinai
 * @version 2012-06-02
 */
public class IndexedSet<T> implements Iterable<T> {
	
	Set<T>	set;
	IndexType type;
	
	public IndexedSet (IndexType type) {
		this.type = type;
		switch (type) {
		case HASHED:
			set = new HashSet<T> ();
			break;
		case SORTED:
			set = new TreeSet<T> ();
			break;
		default:
			throw new IllegalArgumentException ("Unhandled index type: " + type);
		}
	}
	
	public IndexType getIndexType () {
		return type;
	}
	
	public void add (T value) {
		set.add (value);
	}
	public void remove (T value) {
		set.remove(value);
	}
	
	public Set<T> getUnderlyingSet () {
		return set;
	}
	
	public int size() {
		return set.size();
	}
	
	public void addAll (IndexedSet<T> s) {
		set.addAll (s.set);
	}
	
	public Iterator<T> iterator () {
		return set.iterator();
	}
	
	public void retainAll (IndexedSet<T> s) {
		set.retainAll(s.set);
	}
	
	public void removeAll (IndexedSet<T> s) {
		set.removeAll(s.set);
	}
	
	public boolean contains (T key) {
		return set.contains(key);
	}
	public void clear () {
		set.clear();
	}
}