|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractSet<T>
inf.util.SingletonSet<T>
T
- the element type for this Setpublic class SingletonSet<T>
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.
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 |
---|
protected T element
Constructor Detail |
---|
public SingletonSet()
This constructor creates an empty Set.
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.
element
- the element that will be contained in this SetMethod Detail |
---|
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).
clone
in class java.lang.Object
public void clear()
This function empties this Set.
clear
in interface java.util.Collection<T>
clear
in interface java.util.Set<T>
clear
in class java.util.AbstractCollection<T>
public boolean isEmpty()
This function tests whether this Set is empty.
isEmpty
in interface java.util.Collection<T>
isEmpty
in interface java.util.Set<T>
isEmpty
in class java.util.AbstractCollection<T>
public int size()
This function returns the number of elements contained in this Set which can be either 0 or 1.
size
in interface java.util.Collection<T>
size
in interface java.util.Set<T>
size
in class java.util.AbstractCollection<T>
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.
add
in interface java.util.Collection<T>
add
in interface java.util.Set<T>
add
in class java.util.AbstractCollection<T>
elt
- the new element to be added to this Set
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.
contains
in interface java.util.Collection<T>
contains
in interface java.util.Set<T>
contains
in class java.util.AbstractCollection<T>
obj
- the Object for which set membership will be tested
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.
remove
in interface java.util.Collection<T>
remove
in interface java.util.Set<T>
remove
in class java.util.AbstractCollection<T>
elt
- the new element to be added to this Set
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.
toArray
in interface java.util.Collection<T>
toArray
in interface java.util.Set<T>
toArray
in class java.util.AbstractCollection<T>
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.
iterator
in interface java.lang.Iterable<T>
iterator
in interface java.util.Collection<T>
iterator
in interface java.util.Set<T>
iterator
in class java.util.AbstractCollection<T>
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.
equals
in interface java.util.Collection<T>
equals
in interface java.util.Set<T>
equals
in class java.util.AbstractSet<T>
obj
- the Object this Set is compared to
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().
other
- the Set this Set is compared to
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.
hashCode
in interface java.util.Collection<T>
hashCode
in interface java.util.Set<T>
hashCode
in class java.util.AbstractSet<T>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |