inf.util
Class SingletonSet<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by inf.util.SingletonSet<T>
Type Parameters:
T - the element type for this Set
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

public class SingletonSet<T>
extends java.util.AbstractSet<T>
implements java.lang.Cloneable

This class represents a Set that contains either zero or one element of the type T. The functionality provided is no different from any other kind of Set, except when an attempt is made to add a second element, for example. The difference lies in the efficiency with which most member functions can be implemented for such a Set.

Note: As opposed to general Sets, a SingletonSet may not contain the null element.

Author:
Gerhard Wickler

Nested Class Summary
(package private)  class SingletonSet.SingletonSetIterator<E>
           
 
Field Summary
protected  T element
          the element in this Set or null for no element
 
Constructor Summary
SingletonSet()
           This constructor creates an empty Set.
SingletonSet(T element)
           This constructor creates a Set that contains the given element.
 
Method Summary
 boolean add(T elt)
           This function adds the given element to this Set.
 void clear()
           This function empties this Set.
 SingletonSet<T> clone()
           This function returns a semi-shallow clone of this Set.
 boolean contains(java.lang.Object obj)
           This function tests whether the given Object is the element contained in this Set.
 boolean equals(java.lang.Object obj)
           This function compares this SingletonSet to the given Object.
 boolean equals(java.util.Set<T> other)
           This function compares this SingletonSet to the given Set.
 int hashCode()
           This function computes a hash value for this Set.
 boolean isEmpty()
           This function tests whether this Set is empty.
 java.util.Iterator<T> iterator()
           This function returns an Iterator for the elements in this Set.
 boolean remove(java.lang.Object elt)
           This function attempts to remove the given element from this Set.
 int size()
           This function returns the number of elements contained in this Set which can be either 0 or 1.
 java.lang.Object[] toArray()
           This function creates an array of Objects containing the element in this Set.
 
Methods inherited from class java.util.AbstractSet
removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, retainAll, toArray
 

Field Detail

element

protected T element
the element in this Set or null for no element

Constructor Detail

SingletonSet

public SingletonSet()

This constructor creates an empty Set.


SingletonSet

public SingletonSet(T element)

This constructor creates a Set that contains the given element. Note that the Set will be empty if the given element is null.

Parameters:
element - the element that will be contained in this Set
Method Detail

clone

public SingletonSet<T> clone()

This function returns a semi-shallow clone of this Set. The contained element will be the same as is this Set, but the returned Set will be equal, but not the same (this).

Overrides:
clone in class java.lang.Object
Returns:
a semi-shallow copy of this Set

clear

public void clear()

This function empties this Set.

Specified by:
clear in interface java.util.Collection<T>
Specified by:
clear in interface java.util.Set<T>
Overrides:
clear in class java.util.AbstractCollection<T>

isEmpty

public boolean isEmpty()

This function tests whether this Set is empty.

Specified by:
isEmpty in interface java.util.Collection<T>
Specified by:
isEmpty in interface java.util.Set<T>
Overrides:
isEmpty in class java.util.AbstractCollection<T>
Returns:
whether this.size() == 0

size

public int size()

This function returns the number of elements contained in this Set which can be either 0 or 1.

Specified by:
size in interface java.util.Collection<T>
Specified by:
size in interface java.util.Set<T>
Specified by:
size in class java.util.AbstractCollection<T>
Returns:
the cardinality of this Set

add

public boolean add(T elt)

This function adds the given element to this Set. This will succeed if this Set is empty or it already contains the given element. Otherwise an IllegalArgumentException will be thrown because a SingletonSet can hold at most one element. Furthermore, the given element should not be null.

Specified by:
add in interface java.util.Collection<T>
Specified by:
add in interface java.util.Set<T>
Overrides:
add in class java.util.AbstractCollection<T>
Parameters:
elt - the new element to be added to this Set
Returns:
whether this Set was modified by this operation

contains

public boolean contains(java.lang.Object obj)

This function tests whether the given Object is the element contained in this Set. Note that null is never an element of a SingletonSet.

Specified by:
contains in interface java.util.Collection<T>
Specified by:
contains in interface java.util.Set<T>
Overrides:
contains in class java.util.AbstractCollection<T>
Parameters:
obj - the Object for which set membership will be tested
Returns:
whether this Set contains an equal element

remove

public boolean remove(java.lang.Object elt)

This function attempts to remove the given element from this Set. If the given element is the one contained in this Set, the Set will be empty as a result and this function will return true. Otherwise the Set remains unchanged and the function returns false. Note that null is never a member of a SingletonSet and thus cannot be removed from this Set.

Specified by:
remove in interface java.util.Collection<T>
Specified by:
remove in interface java.util.Set<T>
Overrides:
remove in class java.util.AbstractCollection<T>
Parameters:
elt - the new element to be added to this Set
Returns:
whether this Set was modified by this operation

toArray

public java.lang.Object[] toArray()

This function creates an array of Objects containing the element in this Set. If the Set is empty the array will have length 0. Otherwise it will have length 1 and contain the single element in this Set.

Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.Set<T>
Overrides:
toArray in class java.util.AbstractCollection<T>
Returns:
a new array holding the elements, 0 or 1, of this Set

iterator

public java.util.Iterator<T> iterator()

This function returns an Iterator for the elements in this Set. Note that this will return at most one element for a SingletonSet.

Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in interface java.util.Set<T>
Specified by:
iterator in class java.util.AbstractCollection<T>
Returns:
an Iterator for this SingletonSet

equals

public boolean equals(java.lang.Object obj)

This function compares this SingletonSet to the given Object. They are equal only if the given Object is a Set that is equal to this SingletonSet. The latter is tested using equals(Set). The given Object should not be null.

Specified by:
equals in interface java.util.Collection<T>
Specified by:
equals in interface java.util.Set<T>
Overrides:
equals in class java.util.AbstractSet<T>
Parameters:
obj - the Object this Set is compared to
Returns:
true iff obj is a Set with an equal element

equals

public boolean equals(java.util.Set<T> other)

This function compares this SingletonSet to the given Set. They are equal if and only if they are both empty or the given Set contains exactly one element which is equal to the element in this Set. This relies on the function Set.contains().

Parameters:
other - the Set this Set is compared to
Returns:
true iff the contained elements are equal

hashCode

public int hashCode()

This function computes a hash value for this Set. In line with other Sets, the hash value will be the sum of the hash values for the elements, which is just one in this case.

Specified by:
hashCode in interface java.util.Collection<T>
Specified by:
hashCode in interface java.util.Set<T>
Overrides:
hashCode in class java.util.AbstractSet<T>
Returns:
the hashCode() for the contained element or 0 if empty