org.geotools.util
Class CheckedHashSet<E>

Object
  extended by AbstractCollection<E>
      extended by AbstractSet<E>
          extended by HashSet<E>
              extended by LinkedHashSet<E>
                  extended by CheckedHashSet<E>
Type Parameters:
E - The type of elements in the set.
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>, CheckedCollection<E>

public class CheckedHashSet<E>
extends LinkedHashSet<E>
implements CheckedCollection<E>, Cloneable

A checked and synchronized Set. Type checks are performed at run-time in addition of compile-time checks. The synchronization lock can be modified at runtime by overriding the getLock() method.

This class is similar to using the wrappers provided in Collections, minus the cost of indirection levels and with the addition of overrideable methods.

Since:
2.1
Author:
Jody Garnett (Refractions Research), Martin Desruisseaux (IRD)
See Also:
Collections.checkedSet(java.util.Set, java.lang.Class), Collections.synchronizedSet(java.util.Set), Serialized Form

Constructor Summary
CheckedHashSet(Class<E> type)
          Constructs a set of the specified type.
CheckedHashSet(Class<E> type, int capacity)
          Constructs a set of the specified type and initial capacity.
 
Method Summary
 boolean add(E element)
          Adds the specified element to this set if it is not already present.
 boolean addAll(Collection<? extends E> collection)
          Appends all of the elements in the specified collection to this set.
protected  void checkWritePermission()
          Checks if changes in this collection are allowed.
 void clear()
          Removes all of the elements from this set.
 CheckedHashSet<E> clone()
          Returns a shallow copy of this set.
 boolean contains(Object o)
          Returns true if this set contains the specified element.
protected  void ensureValidType(E element)
          Checks the type of the specified object.
 boolean equals(Object o)
          Compares the specified object with this set for equality.
 Class<E> getElementType()
          Returns the element type given at construction time.
protected  Object getLock()
          Returns the synchronization lock.
 int hashCode()
          Returns the hash code value for this set.
 boolean isEmpty()
          Returns true if this set contains no elements.
 Iterator<E> iterator()
          Returns an iterator over the elements in this set.
 boolean remove(Object o)
          Removes the pecified element from this set.
 boolean removeAll(Collection<?> c)
          Removes all of this set's elements that are also contained in the specified collection.
 boolean retainAll(Collection<?> c)
          Retains only the elements in this set that are contained in the specified collection.
 int size()
          Returns the number of elements in this set.
 Object[] toArray()
          Returns an array containing all of the elements in this set.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this set.
 String toString()
          Returns a string representation of this set.
 
Methods inherited from class AbstractCollection
containsAll
 
Methods inherited from class Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface Collection
containsAll
 
Methods inherited from interface Set
containsAll
 

Constructor Detail

CheckedHashSet

public CheckedHashSet(Class<E> type)
Constructs a set of the specified type.

Parameters:
type - The element type (should not be null).

CheckedHashSet

public CheckedHashSet(Class<E> type,
                      int capacity)
Constructs a set of the specified type and initial capacity.

Parameters:
type - The element type (should not be null).
capacity - The initial capacity.
Since:
2.4
Method Detail

getElementType

public Class<E> getElementType()
Returns the element type given at construction time.

Specified by:
getElementType in interface CheckedCollection<E>
Returns:
The element type.
Since:
2.4

ensureValidType

protected void ensureValidType(E element)
                        throws IllegalArgumentException
Checks the type of the specified object. The default implementation ensure that the object is assignable to the type specified at construction time.

Parameters:
element - the object to check, or null.
Throws:
IllegalArgumentException - if the specified element is not of the expected type.

checkWritePermission

protected void checkWritePermission()
                             throws UnsupportedOperationException
Checks if changes in this collection are allowed. This method is automatically invoked after this collection got the lock and before any operation that may change the content. The default implementation does nothing (i.e. this collection is modifiable). Subclasses should override this method if they want to control write access.

Throws:
UnsupportedOperationException - if this collection is unmodifiable.
Since:
2.5

getLock

protected Object getLock()
Returns the synchronization lock. The default implementation returns this. Subclasses that override this method should be careful to update the lock reference when this set is cloned.

Returns:
The synchronization lock.
Since:
2.5

iterator

public Iterator<E> iterator()
Returns an iterator over the elements in this set.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Overrides:
iterator in class HashSet<E>

size

public int size()
Returns the number of elements in this set.

Specified by:
size in interface Collection<E>
Specified by:
size in interface Set<E>
Overrides:
size in class HashSet<E>

isEmpty

public boolean isEmpty()
Returns true if this set contains no elements.

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface Set<E>
Overrides:
isEmpty in class HashSet<E>

contains

public boolean contains(Object o)
Returns true if this set contains the specified element.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Set<E>
Overrides:
contains in class HashSet<E>

add

public boolean add(E element)
            throws IllegalArgumentException,
                   UnsupportedOperationException
Adds the specified element to this set if it is not already present.

Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class HashSet<E>
Parameters:
element - element to be added to this set.
Returns:
true if the set did not already contain the specified element.
Throws:
IllegalArgumentException - if the specified element is not of the expected type.
UnsupportedOperationException - if this collection is unmodifiable.

addAll

public boolean addAll(Collection<? extends E> collection)
               throws IllegalArgumentException,
                      UnsupportedOperationException
Appends all of the elements in the specified collection to this set.

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface Set<E>
Overrides:
addAll in class AbstractCollection<E>
Parameters:
collection - the elements to be inserted into this set.
Returns:
true if this set changed as a result of the call.
Throws:
IllegalArgumentException - if at least one element is not of the expected type.
UnsupportedOperationException - if this collection is unmodifiable.

remove

public boolean remove(Object o)
               throws UnsupportedOperationException
Removes the pecified element from this set.

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>
Overrides:
remove in class HashSet<E>
Throws:
UnsupportedOperationException - if this collection is unmodifiable.

removeAll

public boolean removeAll(Collection<?> c)
                  throws UnsupportedOperationException
Removes all of this set's elements that are also contained in the specified collection.

Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface Set<E>
Overrides:
removeAll in class AbstractSet<E>
Throws:
UnsupportedOperationException - if this collection is unmodifiable.

retainAll

public boolean retainAll(Collection<?> c)
                  throws UnsupportedOperationException
Retains only the elements in this set that are contained in the specified collection.

Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface Set<E>
Overrides:
retainAll in class AbstractCollection<E>
Throws:
UnsupportedOperationException - if this collection is unmodifiable.

clear

public void clear()
           throws UnsupportedOperationException
Removes all of the elements from this set.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>
Overrides:
clear in class HashSet<E>
Throws:
UnsupportedOperationException - if this collection is unmodifiable.

toArray

public Object[] toArray()
Returns an array containing all of the elements in this set.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>
Overrides:
toArray in class AbstractCollection<E>

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>
Overrides:
toArray in class AbstractCollection<E>
Type Parameters:
T - The type of array elements.

toString

public String toString()
Returns a string representation of this set.

Overrides:
toString in class AbstractCollection<E>

equals

public boolean equals(Object o)
Compares the specified object with this set for equality.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface Set<E>
Overrides:
equals in class AbstractSet<E>

hashCode

public int hashCode()
Returns the hash code value for this set.

Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface Set<E>
Overrides:
hashCode in class AbstractSet<E>

clone

public CheckedHashSet<E> clone()
Returns a shallow copy of this set.

Overrides:
clone in class HashSet<E>
Returns:
A shallow copy of this set.
See Also:
Object.clone()


Copyright © 1996-2014 Geotools. All Rights Reserved.