net.sf.molae.pipe.basic
Class CollectionAsSet

java.lang.Object
  |
  +--net.sf.molae.pipe.basic.ObjectProxy
        |
        +--net.sf.molae.pipe.basic.CollectionProxy
              |
              +--net.sf.molae.pipe.basic.CollectionAsSet
All Implemented Interfaces:
Collection, Set
Direct Known Subclasses:
SetAsSortedSet

public class CollectionAsSet
extends CollectionProxy
implements Set

If it can be guaranteed that a collection is a set, this wrapper can be used to create a Set implementation.


Constructor Summary
CollectionAsSet(Collection base)
          Constructs a new CollectionAsSet object.
 
Method Summary
 boolean equals(Object o)
          Compares the specified object with this set for equality.
 int hashCode()
          Returns the hash code value for this set.
static boolean isSet(Collection col)
          Checks if the specified collection is a set.
 
Methods inherited from class net.sf.molae.pipe.basic.CollectionProxy
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from class net.sf.molae.pipe.basic.ObjectProxy
assertNotNull, getBase, toString, toVerboseString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

CollectionAsSet

public CollectionAsSet(Collection base)
Constructs a new CollectionAsSet object.
Parameters:
base - the underlying object.
Throws:
NullPointerException - if the specified object is null.
Method Detail

isSet

public static boolean isSet(Collection col)
Checks if the specified collection is a set.
Parameters:
col - collection to check
Returns:
true if the specified collection is a set, that is no two elements in it are equal.

equals

public boolean equals(Object o)
Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it it returns false. If so, it returns containsAll((Collection) o).

Copied from AbstractSet.

Specified by:
equals in interface Set
Overrides:
equals in class ObjectProxy
Parameters:
o - Object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set.

hashCode

public int hashCode()
Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set. This implementation enumerates over the set, calling the hashCode method on each element in the collection, and adding up the results.

Copied from AbstractSet.

Specified by:
hashCode in interface Set
Overrides:
hashCode in class ObjectProxy
Returns:
the hash code value for this set.