net.sf.molae.pipe.basic
Class CollectionAsSet<B extends Collection<E>,E>

java.lang.Object
  extended by net.sf.molae.pipe.basic.ObjectProxy<B>
      extended by net.sf.molae.pipe.basic.CollectionProxy<B,E>
          extended by net.sf.molae.pipe.basic.CollectionAsSet<B,E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
Direct Known Subclasses:
SetAsSortedSet

public class CollectionAsSet<B extends Collection<E>,E>
extends CollectionProxy<B,E>
implements Set<E>

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


Constructor Summary
CollectionAsSet(B 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.
static
<E> CollectionAsSet<Collection<E>,E>
newInstance(Collection<E> base)
          Constructs a new CollectionAsSet object.
 
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(B 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.

newInstance

public static <E> CollectionAsSet<Collection<E>,E> newInstance(Collection<E> base)
Constructs a new CollectionAsSet object.

Parameters:
base - the underlying object.
Throws:
NullPointerException - if the specified object is null.

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 Collection<E>
Specified by:
equals in interface Set<E>
Overrides:
equals in class ObjectProxy<B extends Collection<E>>
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 Collection<E>
Specified by:
hashCode in interface Set<E>
Overrides:
hashCode in class ObjectProxy<B extends Collection<E>>
Returns:
the hash code value for this set.