Top Description Inners Fields Constructors Methods
org.jboss.modules

pack-priv Class FastCopyHashSet<E>

extends AbstractSet<E>
implements Set<E>, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, java.util.Set, java.util.Collection, java.lang.Iterable
Imports
java.io.IOException, .Serializable, java.util.AbstractSet, .Arrays, .Collection, .ConcurrentModificationException, .Iterator, .NoSuchElementException, .Set

A HashSet that is optimized for fast shallow copies. If the copy-ctor is passed another FastCopyHashSet, or clone is called on this set, the shallow copy can be performed using little more than a single array copy. In order to accomplish this, immutable objects must be used internally, so update operations result in slightly more object churn than HashSet.

Note

It is very important to use a smaller load factor than you normally would for HashSet, since the implementation is open-addressed with linear probing. With a 50% load-factor a get is expected to return in only 2 probes. However, a 90% load-factor is expected to return in around 50 probes.

Authors
Jason T. Greene, David M. Lloyd

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private static final int
DEFAULT_CAPACITY

Same default as HashMap, must be a power of 2

private static final float
private transient int
hashCode

Accumulated hash code

private final float
loadFactor

The user defined load factor which defines when to resize

private static final int
MAXIMUM_CAPACITY

MAX_INT - 1

private transient int
modCount

Counter used to detect changes made outside of an iterator

private static final long
serialVersionUID

Serialization ID

private transient int
size

The current number of key-value pairs

private transient E[]
table

The open-addressed table

private transient int
threshold

The next resize

Constructor Summary

AccessConstructor and Description
pack-priv
FastCopyHashSet(int initialCapacity, float loadFactor)

pack-priv
FastCopyHashSet(Set<? extends E> set)

pack-priv
FastCopyHashSet(int initialCapacity)

pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
add(E
element to be added to this set
key
)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

Adds the specified element to this set if it is not already present (optional operation).
public boolean
addAll(Collection<? extends E>
collection containing elements to be added to this set
set
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Set.addAll, java.util.Collection.addAll.

Adds all of the elements in the specified collection to this set if they're not already present (optional operation).
public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Removes all of the elements from this set (optional operation).
public FastCopyHashSet<E>
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.
public boolean
contains(Object
element whose presence in this set is to be tested
key
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Returns true if this set contains the specified element.
public boolean
containsAll(final Collection<?>
collection to be checked for containment in this set
c
)

Overrides java.util.AbstractCollection.containsAll.

Implements java.util.Set.containsAll, java.util.Collection.containsAll.

Returns true if this set contains all of the elements of the specified collection.
public boolean
equals(final Object
object to be compared for equality with this set
o
)

Overrides java.util.AbstractSet.equals.

Implements java.util.Set.equals, java.util.Collection.equals.

Compares the specified object with this set for equality.
public Object[]
public int
hashCode()

Overrides java.util.AbstractSet.hashCode.

Implements java.util.Set.hashCode, java.util.Collection.hashCode.

Returns the hash code value for this set.
private static int
index(int hashCode, int length)

private void
init(int initialCapacity, float loadFactor)

public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Returns true if this set contains no elements.
public Iterator<E>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.
private int
nextIndex(int index, int length)

public void
private void
putForCreate(E key)

private void
private void
relocate(int start)

public boolean
remove(Object
object to be removed from this set, if present
key
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

Removes the specified element from this set if it is present (optional operation).
private void
resize(int from)

public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set (its cardinality).
private void
Inherited from java.util.AbstractSet:
removeAll

Field Detail

DEFAULT_CAPACITYback to summary
private static final int DEFAULT_CAPACITY

Same default as HashMap, must be a power of 2

DEFAULT_LOAD_FACTORback to summary
private static final float DEFAULT_LOAD_FACTOR

50%

hashCodeback to summary
private transient int hashCode

Accumulated hash code

loadFactorback to summary
private final float loadFactor

The user defined load factor which defines when to resize

MAXIMUM_CAPACITYback to summary
private static final int MAXIMUM_CAPACITY

MAX_INT - 1

modCountback to summary
private transient int modCount

Counter used to detect changes made outside of an iterator

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization ID

sizeback to summary
private transient int size

The current number of key-value pairs

tableback to summary
private transient E[] table

The open-addressed table

thresholdback to summary
private transient int threshold

The next resize

Constructor Detail

FastCopyHashSetback to summary
pack-priv FastCopyHashSet(int initialCapacity, float loadFactor)
FastCopyHashSetback to summary
pack-priv FastCopyHashSet(Set<? extends E> set)
FastCopyHashSetback to summary
pack-priv FastCopyHashSet(int initialCapacity)
FastCopyHashSetback to summary
pack-priv FastCopyHashSet()

Method Detail

addback to summary
public boolean add(E key)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

Doc from java.util.Set.add.

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

Parameters
key:E

element to be added to this set

Returns:boolean

true if this set did not already contain the specified element

addAllback to summary
public boolean addAll(Collection<? extends E> set)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Set.addAll, java.util.Collection.addAll.

Doc from java.util.Set.addAll.

Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters
set:Collection<? extends E>

collection containing elements to be added to this set

Returns:boolean

true if this set changed as a result of the call

clearback to summary
public void clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Doc from java.util.Set.clear.

Removes all of the elements from this set (optional operation). The set will be empty after this call returns.

cloneback to summary
public FastCopyHashSet<E> clone()

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:FastCopyHashSet<E>

a clone of this instance.

Annotations
@SuppressWarnings:unchecked
containsback to summary
public boolean contains(Object key)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Doc from java.util.Set.contains.

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Parameters
key:Object

element whose presence in this set is to be tested

Returns:boolean

true if this set contains the specified element

containsAllback to summary
public boolean containsAll(final Collection<?> c)

Overrides java.util.AbstractCollection.containsAll.

Implements java.util.Set.containsAll, java.util.Collection.containsAll.

Doc from java.util.Set.containsAll.

Returns true if this set contains all of the elements of the specified collection. If the specified collection is also a set, this method returns true if it is a subset of this set.

Parameters
c:Collection<?>

collection to be checked for containment in this set

Returns:boolean

true if this set contains all of the elements of the specified collection

equalsback to summary
public boolean equals(final Object o)

Overrides java.util.AbstractSet.equals.

Implements java.util.Set.equals, java.util.Collection.equals.

Doc from java.util.Set.equals.

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

Parameters
o:Object

object to be compared for equality with this set

Returns:boolean

true if the specified object is equal to this set

Annotations
@SuppressWarnings:NonFinalFieldReferenceInEquals
getRawArrayback to summary
public Object[] getRawArray()
hashCodeback to summary
public int hashCode()

Overrides java.util.AbstractSet.hashCode.

Implements java.util.Set.hashCode, java.util.Collection.hashCode.

Doc from java.util.Set.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, where the hash code of a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object#hashCode.

Returns:int

the hash code value for this set

Annotations
@SuppressWarnings:NonFinalFieldReferencedInHashCode
indexback to summary
private static int index(int hashCode, int length)
initback to summary
private void init(int initialCapacity, float loadFactor)
Annotations
@SuppressWarnings:unchecked
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Doc from java.util.Set.isEmpty.

Returns true if this set contains no elements.

Returns:boolean

true if this set contains no elements

iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Returns:Iterator<E>

an iterator over the elements in this set

nextIndexback to summary
private int nextIndex(int index, int length)
printDebugStatsback to summary
public void printDebugStats()
putForCreateback to summary
private void putForCreate(E key)
Annotations
@SuppressWarnings:unchecked
readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
Annotations
@SuppressWarnings:unchecked
relocateback to summary
private void relocate(int start)
removeback to summary
public boolean remove(Object key)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

Doc from java.util.Set.remove.

Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Parameters
key:Object

object to be removed from this set, if present

Returns:boolean

true if this set contained the specified element

resizeback to summary
private void resize(int from)
Annotations
@SuppressWarnings:unchecked
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Doc from java.util.Set.size.

Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this set (its cardinality)

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException
org.jboss.modules back to summary

private Class FastCopyHashSet.KeyIterator

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Field Summary

Modifier and TypeField and Description
private int
private int
private boolean
private int
private E[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
private int current
expectedCountback to summary
private int expectedCount
hasNextback to summary
private boolean hasNext
nextback to summary
private int next
tableback to summary
private E[] table

Constructor Detail

KeyIteratorback to summary
private KeyIterator()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@SuppressWarnings:unchecked