jpaul.DataStructs
Class NonIterableSet<T>

java.lang.Object
  extended by jpaul.DataStructs.NonIterableSet<T>
All Implemented Interfaces:
java.io.Serializable

public class NonIterableSet<T>
extends java.lang.Object
implements java.io.Serializable

NonIterableSet is a very simple set that CANNOT be traversed. As such, it avoids the non-determinism problems of the HashSets, without the cost of building a linked list (as LinkedHashSet does). The idea of having such a class occured to me while trying to find which HashSets do not affect the externally-visible determinism of a piece of code. Clearly, all sets that are used only for membership testing (without ever being iterated upon) can be left to be HashSet. One can use a NonIterableSet instead, and get the type system check the lack of iterations.

Version:
$Id: NonIterableSet.java,v 1.4 2006/02/22 05:04:30 salcianu Exp $
Author:
Alex Salcianu - salcianu@alum.mit.edu
See Also:
Serialized Form

Constructor Summary
NonIterableSet()
          Creates a NonIterableSet backed by a private HashSet with the default initial capacity.
NonIterableSet(int initialCapacity)
          Creates a NonIterableSet backed by a private HashSet of a certain initial capacity.
 
Method Summary
 boolean add(T elem)
          Adds element elem to this set.
 boolean addAll(java.util.Collection<T> coll)
          Adds all elements of collection coll to this set.
 void clear()
          Removes all elements from this set.
 boolean contains(T elem)
          Checks whether the element elem belongs to this set.
 boolean isEmpty()
          Checks whether this set is empty.
 boolean remove(T elem)
          Removes the element elem from this set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NonIterableSet

public NonIterableSet()
Creates a NonIterableSet backed by a private HashSet with the default initial capacity.


NonIterableSet

public NonIterableSet(int initialCapacity)
Creates a NonIterableSet backed by a private HashSet of a certain initial capacity.

Method Detail

add

public boolean add(T elem)
Adds element elem to this set. Returns true iff elem is a new element (i.e., it was not in the set before the call to this method).


addAll

public boolean addAll(java.util.Collection<T> coll)
Adds all elements of collection coll to this set. Returns true iff we added at least one new element to this non-iterable set.


contains

public boolean contains(T elem)
Checks whether the element elem belongs to this set.


remove

public boolean remove(T elem)
Removes the element elem from this set. Returns true if the set contained elem.


isEmpty

public boolean isEmpty()
Checks whether this set is empty.


clear

public void clear()
Removes all elements from this set.



Copyright 2005 Alexandru Salcianu - salcianu@alum.mit.edu