org.norther.tammi.acorn.util
Class ArrayAsSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet
          extended by org.norther.tammi.acorn.util.ArrayAsSet
All Implemented Interfaces:
Serializable, Cloneable, Iterable, Collection, Set

public class ArrayAsSet
extends AbstractSet
implements Cloneable, Serializable

An implementation of the Set interface avoiding synchronization by making a new copy of its back-end array whenever it is structurally modified. The solution consumes memory and is not suitable for frequently changing sets, but provides a non-blocking container for reader oriented tasks.

Note that iterators do not support modifications.

Based on Doug Lea's CopyOnWriteArraySet .

Author:
Doug Lea, Ilkka Priha
See Also:
Serialized Form

Constructor Summary
ArrayAsSet()
          Constructs an empty set.
ArrayAsSet(ArrayAsList l)
          Constructs a set with the back-end list.
ArrayAsSet(Collection c)
          Constructs a set containing the elements of the specified collection.
ArrayAsSet(Object[] array)
          Constructs a set using the specified array as its back-end and maintaining the component type of the array during modifications.
ArrayAsSet(Object[] array, boolean force)
          Constructs a set using the specified array as its back-end without optionally removing duplicate elements and maintaining the component type of the array during modifications.
 
Method Summary
 boolean add(int index, Object o)
          Adds an object to the set at the specified position.
 boolean add(Object o)
           
 boolean addAll(Collection c)
           
 void clear()
           
 Object clone()
          Returns a shallow copy of this set instance.
 boolean contains(Object o)
           
 boolean containsAll(Collection c)
           
 int copyAll(int index, Object[] source)
          Copies missing elements from the specified array to the given position of the back-end.
 int copyAll(Object[] source)
          Copies missing elements from the specified array to the back-end.
 Object[] getArray()
          Gets a snapshot of the back-end array.
 boolean isEmpty()
           
 Iterator iterator()
           
 Object remove(int index)
          Removes an object from the set at the specified position.
 boolean remove(Object o)
           
 boolean removeAll(Collection c)
           
 boolean retainAll(Collection c)
           
 int size()
           
 Object[] toArray()
           
 Object[] toArray(Object[] a)
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode
 
Methods inherited from class java.util.AbstractCollection
toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArrayAsSet

public ArrayAsSet()
Constructs an empty set.


ArrayAsSet

public ArrayAsSet(ArrayAsList l)
Constructs a set with the back-end list.

Parameters:
l - the back-end list.

ArrayAsSet

public ArrayAsSet(Collection c)
Constructs a set containing the elements of the specified collection.

Parameters:
c - the collection whose elements are to be copied.

ArrayAsSet

public ArrayAsSet(Object[] array)
Constructs a set using the specified array as its back-end and maintaining the component type of the array during modifications.

Parameters:
array - the array, which is used as the back-end.

ArrayAsSet

public ArrayAsSet(Object[] array,
                  boolean force)
Constructs a set using the specified array as its back-end without optionally removing duplicate elements and maintaining the component type of the array during modifications.

Parameters:
array - the array, which is used as the back-end.
force - if true, duplicates, if any, are not removed.
Method Detail

clone

public Object clone()
Returns a shallow copy of this set instance.

Overrides:
clone in class Object
Returns:
a shallow copy of this set.

copyAll

public int copyAll(Object[] source)
Copies missing elements from the specified array to the back-end.

Parameters:
source - the array to copy.
Returns:
the number of copied elements.

copyAll

public int copyAll(int index,
                   Object[] source)
Copies missing elements from the specified array to the given position of the back-end.

Parameters:
index - the index of the elements.
source - the array to copy.
Returns:
the number of copied elements.
Throws:
IndexOutOfBoundsException - for invalid index.

size

public int size()
Specified by:
size in interface Collection
Specified by:
size in interface Set
Specified by:
size in class AbstractCollection

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection
Specified by:
isEmpty in interface Set
Overrides:
isEmpty in class AbstractCollection

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection
Specified by:
contains in interface Set
Overrides:
contains in class AbstractCollection

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection

toArray

public Object[] toArray(Object[] a)
Specified by:
toArray in interface Collection
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection

clear

public void clear()
Specified by:
clear in interface Collection
Specified by:
clear in interface Set
Overrides:
clear in class AbstractCollection

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface Set
Specified by:
iterator in class AbstractCollection

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection
Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection

containsAll

public boolean containsAll(Collection c)
Specified by:
containsAll in interface Collection
Specified by:
containsAll in interface Set
Overrides:
containsAll in class AbstractCollection

addAll

public boolean addAll(Collection c)
Specified by:
addAll in interface Collection
Specified by:
addAll in interface Set
Overrides:
addAll in class AbstractCollection

removeAll

public boolean removeAll(Collection c)
Specified by:
removeAll in interface Collection
Specified by:
removeAll in interface Set
Overrides:
removeAll in class AbstractSet

retainAll

public boolean retainAll(Collection c)
Specified by:
retainAll in interface Collection
Specified by:
retainAll in interface Set
Overrides:
retainAll in class AbstractCollection

add

public boolean add(Object o)
Specified by:
add in interface Collection
Specified by:
add in interface Set
Overrides:
add in class AbstractCollection

add

public boolean add(int index,
                   Object o)
Adds an object to the set at the specified position.

Parameters:
index - the index of object.
o - the object to add.
Returns:
true if added, otherwise false.

remove

public Object remove(int index)
Removes an object from the set at the specified position.

Parameters:
index - the index of object.
Returns:
the removed object or null.

getArray

public Object[] getArray()
Gets a snapshot of the back-end array. Note that the array should not be modified but can be used instead of an iterator to save some memory when the set is browsed frequently.

Returns:
the back-end array.


Copyright © 2004 The Norther Organization. All rights reserved.