kodkod.util.collections
Class SingletonIdentitySet<V>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<V>
          extended by kodkod.util.collections.SingletonIdentitySet<V>
All Implemented Interfaces:
java.lang.Iterable<V>, java.util.Collection<V>, java.util.Set<V>

public final class SingletonIdentitySet<V>
extends java.util.AbstractSet<V>

This is an immutable singleton set implementation that tests for membership using reference-equality in place of object-equality when comparing elements.

Author:
Emina Torlak
specfield:
element: V

Constructor Summary
SingletonIdentitySet(java.util.Collection<? extends V> collection)
          Constructs a SingletonIdentitySet that will hold the first element returned by the given collection's iterator.
SingletonIdentitySet(V element)
          Constructs a SingletonIdentitySet that will hold the given element.
 
Method Summary
 boolean contains(java.lang.Object obj)
          Returns true iff this.element and obj are the same object.
 boolean containsAll(java.util.Collection<?> collection)
           
 boolean equals(java.lang.Object o)
          Compares the specified object with this set for equality.
 int hashCode()
          Returns 0 if this.element is null; otherwise returns this.element.hashCode().
 boolean isEmpty()
          Returns false.
 java.util.Iterator<V> iterator()
           
 int size()
          Returns 1.
 
Methods inherited from class java.util.AbstractSet
removeAll
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, remove, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
add, addAll, clear, remove, retainAll, toArray, toArray
 

Constructor Detail

SingletonIdentitySet

public SingletonIdentitySet(V element)
Constructs a SingletonIdentitySet that will hold the given element.

effects:
this.element' = element

SingletonIdentitySet

public SingletonIdentitySet(java.util.Collection<? extends V> collection)
Constructs a SingletonIdentitySet that will hold the first element returned by the given collection's iterator.

Throws:
java.util.NoSuchElementException - - collection.isEmpty()
effects:
this.element' = collection.iterator().next()
Method Detail

contains

public boolean contains(java.lang.Object obj)
Returns true iff this.element and obj are the same object.

Specified by:
contains in interface java.util.Collection<V>
Specified by:
contains in interface java.util.Set<V>
Overrides:
contains in class java.util.AbstractCollection<V>
Returns:
this.element == obj

containsAll

public boolean containsAll(java.util.Collection<?> collection)
Specified by:
containsAll in interface java.util.Collection<V>
Specified by:
containsAll in interface java.util.Set<V>
Overrides:
containsAll in class java.util.AbstractCollection<V>
See Also:
Set.containsAll(java.util.Collection)

isEmpty

public boolean isEmpty()
Returns false.

Specified by:
isEmpty in interface java.util.Collection<V>
Specified by:
isEmpty in interface java.util.Set<V>
Overrides:
isEmpty in class java.util.AbstractCollection<V>
Returns:
false.

iterator

public java.util.Iterator<V> iterator()
Specified by:
iterator in interface java.lang.Iterable<V>
Specified by:
iterator in interface java.util.Collection<V>
Specified by:
iterator in interface java.util.Set<V>
Specified by:
iterator in class java.util.AbstractCollection<V>
See Also:
Set.iterator()

size

public int size()
Returns 1.

Specified by:
size in interface java.util.Collection<V>
Specified by:
size in interface java.util.Set<V>
Specified by:
size in class java.util.AbstractCollection<V>
Returns:
1

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this set for equality. Returns true if the given object is also a set and the two sets contain identical object-references.

Owing to the reference-equality-based semantics of this set it is possible that the symmetry and transitivity requirements of the Object.equals contract may be violated if this set is compared to a normal set. However, the Object.equals contract is guaranteed to hold among SingletonHashSet instances.

Specified by:
equals in interface java.util.Collection<V>
Specified by:
equals in interface java.util.Set<V>
Overrides:
equals in class java.util.AbstractSet<V>
Returns:
true if the specified object is equal to this set.
See Also:
Object.equals(Object)

hashCode

public int hashCode()
Returns 0 if this.element is null; otherwise returns this.element.hashCode().

Specified by:
hashCode in interface java.util.Collection<V>
Specified by:
hashCode in interface java.util.Set<V>
Overrides:
hashCode in class java.util.AbstractSet<V>
Returns:
this.element = null ? 0 : this.element.hashCode()
See Also:
AbstractSet.hashCode()