Package org.geotools.util
Class CheckedHashSet<E>
- Object
-
- AbstractCollection<E>
-
- AbstractSet<E>
-
- HashSet<E>
-
- LinkedHashSet<E>
-
- CheckedHashSet<E>
-
- Type Parameters:
E- The type of elements in the set.
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,Set<E>,Cloneable,CheckedCollection<E>
public class CheckedHashSet<E> extends LinkedHashSet<E> implements CheckedCollection<E>, Cloneable
A checked and synchronizedSet. Type checks are performed at run-time in addition of compile-time checks. The synchronization lock can be modified at runtime by overriding thegetLock()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<E>, java.lang.Class<E>),Collections.synchronizedSet(java.util.Set<T>), Serialized Form
-
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E element)Adds the specified element to this set if it is not already present.booleanaddAll(Collection<? extends E> collection)Appends all of the elements in the specified collection to this set.protected voidcheckWritePermission()Checks if changes in this collection are allowed.voidclear()Removes all of the elements from this set.CheckedHashSet<E>clone()Returns a shallow copy of this set.booleancontains(Object o)Returnstrueif this set contains the specified element.protected voidensureValidType(E element)Checks the type of the specified object.booleanequals(Object o)Compares the specified object with this set for equality.Class<E>getElementType()Returns the element type given at construction time.protected ObjectgetLock()Returns the synchronization lock.inthashCode()Returns the hash code value for this set.booleanisEmpty()Returnstrueif this set contains no elements.Iterator<E>iterator()Returns an iterator over the elements in this set.booleanremove(Object o)Removes the pecified element from this set.booleanremoveAll(Collection<?> c)Removes all of this set's elements that are also contained in the specified collection.booleanretainAll(Collection<?> c)Retains only the elements in this set that are contained in the specified collection.intsize()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.StringtoString()Returns a string representation of this set.-
Methods inherited from class LinkedHashSet
spliterator
-
Methods inherited from class AbstractCollection
containsAll
-
Methods inherited from interface Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface Set
containsAll
-
-
-
-
Method Detail
-
getElementType
public Class<E> getElementType()
Returns the element type given at construction time.- Specified by:
getElementTypein interfaceCheckedCollection<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, ornull.- Throws:
IllegalArgumentException- if the specified element is not of the expected type.
-
checkWritePermission
protected void checkWritePermission() throws UnsupportedOperationExceptionChecks 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 returnsthis. 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
-
size
public int size()
Returns the number of elements in this set.
-
isEmpty
public boolean isEmpty()
Returnstrueif this set contains no elements.
-
contains
public boolean contains(Object o)
Returnstrueif this set contains the specified element.
-
add
public boolean add(E element) throws IllegalArgumentException, UnsupportedOperationException
Adds the specified element to this set if it is not already present.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classHashSet<E>- Parameters:
element- element to be added to this set.- Returns:
trueif 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:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the elements to be inserted into this set.- Returns:
trueif 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:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classHashSet<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:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<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:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if this collection is unmodifiable.
-
clear
public void clear() throws UnsupportedOperationExceptionRemoves all of the elements from this set.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classHashSet<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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>- Type Parameters:
T- The type of array elements.
-
toString
public String toString()
Returns a string representation of this set.- Overrides:
toStringin classAbstractCollection<E>
-
equals
public boolean equals(Object o)
Compares the specified object with this set for equality.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classAbstractSet<E>
-
hashCode
public int hashCode()
Returns the hash code value for this set.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceSet<E>- Overrides:
hashCodein classAbstractSet<E>
-
clone
public CheckedHashSet<E> clone()
Returns a shallow copy of this set.
-
-