org.norther.tammi.acorn.util
Class ArrayAsList

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

public class ArrayAsList
extends AbstractList
implements Cloneable, Serializable

An array implementation of the List 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 lists, but provides a non-blocking container for read oriented tasks.

Additional methods, addIfAbsent and addAllAbsent, support the ArrayAsSet implementation of the Set interface but can be used with this class as well. The copyIn, copyAll and copyAbsent methods support modification of contents with one operation.

Note that the list maintains the original component type, if any, in all array operations.

Note that iterators don't support other that set modifications.

Based on Doug Lea's CopyOnWriteArrayList .

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

Nested Class Summary
protected static class ArrayAsList.ArrayIterator
          An inner class for iterators.
 
Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ArrayAsList()
          Constructs an empty list.
ArrayAsList(Collection c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayAsList(Object[] array)
          Constructs a list using the specified array as its back-end and maintaining the component type of the array during modifications.
 
Method Summary
 void add(int index, Object item)
           
 boolean add(Object item)
           
 boolean addAll(Collection c)
           
 boolean addAll(int index, Collection c)
           
 int addAllAbsent(Collection collection)
          Adds all items in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's toArray method.
 int addAllAbsent(int index, Collection c)
          Adds all items in the specified collection that are not already contained in this list, at the specified position of this list, in the order that they are returned by the specified collection's toArray method.
 boolean addIfAbsent(int index, Object item)
          Adds the specified item at the specified position in this list if it is not present.
 boolean addIfAbsent(Object item)
          Adds the specified item to the end of this list if it is not present.
 void clear()
           
 Object clone()
          Clones this list by returning a shallow copy.
 boolean contains(Object item)
           
 boolean containsAll(Collection c)
           
 int copyAbsent(int index, Object[] source)
          Copies missing elements from the specified array to the given position of the back-end.
 int copyAbsent(Object[] source)
          Copies missing elements from the specified array to the back-end.
 void copyAll(int index, Object[] source)
          Copies all elements from the specified array to the given position of the back-end.
 void copyAll(Object[] source)
          Copies all elements from the specified array to the back-end.
 Object get(int index)
           
 Object[] getArray()
          Gets a snapshot of the back-end array.
 Class getComponentType()
          Gets the component type of the back-end array.
 int indexOf(Object item)
           
protected static int indexOf(Object item, Object[] array, int offset, int n)
          A static index method without synchronization.
 Iterator iterator()
           
 int lastIndexOf(Object item)
           
 ListIterator listIterator()
           
 ListIterator listIterator(int index)
           
protected static void rangeCheck(int index, int length)
          Checks if the given index is in range.
 Object remove(int index)
           
 boolean remove(Object item)
           
 boolean removeAll(Collection c)
           
 void removeRange(int fromIndex, int toIndex)
          Removes a range of elements from this list in one operation.
 boolean retainAll(Collection c)
           
 Object set(int index, Object item)
           
 void setArray(Object[] array)
          Sets the the back-end array.
 void setSize(int size)
          Sets the size of the back-end array by adding nulls or by truncating.
 int size()
           
 Object[] toArray()
           
 Object[] toArray(Object[] a)
           
 
Methods inherited from class java.util.AbstractList
equals, hashCode, subList
 
Methods inherited from class java.util.AbstractCollection
isEmpty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
isEmpty
 

Constructor Detail

ArrayAsList

public ArrayAsList()
Constructs an empty list.


ArrayAsList

public ArrayAsList(Collection c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

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

ArrayAsList

public ArrayAsList(Object[] array)
Constructs a list 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 internal array.
Method Detail

indexOf

protected static int indexOf(Object item,
                             Object[] array,
                             int offset,
                             int n)
A static index method without synchronization.

Parameters:
item - the item to search.
array - the array of items.
offset - the start index.
n - the number of items to iterate.
Returns:
the index of the found item or -1.

rangeCheck

protected static void rangeCheck(int index,
                                 int length)
Checks if the given index is in range.

Parameters:
index - the index to check.
length - the number of items.
Throws:
IndexOutOfBoundsException - on errors.

clone

public Object clone()
Clones this list by returning a shallow copy.

Overrides:
clone in class Object
Returns:
a clone of this list.

copyAll

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

Parameters:
source - the array to copy.

copyAll

public void copyAll(int index,
                    Object[] source)
Copies all 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.
Throws:
IndexOutOfBoundsException - for invalid index.

copyAbsent

public int copyAbsent(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.

copyAbsent

public int copyAbsent(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.

addIfAbsent

public boolean addIfAbsent(Object item)
Adds the specified item to the end of this list if it is not present.

Parameters:
item - the item to be added.
Returns:
true if added, otherwise false.

addIfAbsent

public boolean addIfAbsent(int index,
                           Object item)
Adds the specified item at the specified position in this list if it is not present. Shifts the item currently at that position (if any) and any subsequent items to the right (adds one to their indices).

Parameters:
index - the index of the item.
item - the item to be inserted.
Returns:
true if added, otherwise false.
Throws:
IndexOutOfBoundsException - for invalid index.

addAllAbsent

public int addAllAbsent(Collection collection)
Adds all items in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's toArray method.

Parameters:
collection - the items to be added.
Returns:
the number of items added.

addAllAbsent

public int addAllAbsent(int index,
                        Collection c)
Adds all items in the specified collection that are not already contained in this list, at the specified position of this list, in the order that they are returned by the specified collection's toArray method.

Parameters:
index - the index of the item.
c - the items to be added.
Returns:
the number of items added.

removeRange

public void removeRange(int fromIndex,
                        int toIndex)
Removes a range of elements from this list in one operation.

Overrides:
removeRange in class AbstractList
Parameters:
fromIndex - index of first element to be removed.
toIndex - index after last element to be removed.

setSize

public void setSize(int size)
Sets the size of the back-end array by adding nulls or by truncating.

Parameters:
size - the new size.

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 list is browsed frequently.

Returns:
the back-end array.

setArray

public void setArray(Object[] array)
Sets the the back-end array.

Parameters:
array - the back-end array.

getComponentType

public Class getComponentType()
Gets the component type of the back-end array.

Returns:
the component type.

size

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

contains

public boolean contains(Object item)
Specified by:
contains in interface Collection
Specified by:
contains in interface List
Overrides:
contains in class AbstractCollection

containsAll

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

indexOf

public int indexOf(Object item)
Specified by:
indexOf in interface List
Overrides:
indexOf in class AbstractList

lastIndexOf

public int lastIndexOf(Object item)
Specified by:
lastIndexOf in interface List
Overrides:
lastIndexOf in class AbstractList

toArray

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

toArray

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

get

public Object get(int index)
Specified by:
get in interface List
Specified by:
get in class AbstractList

set

public Object set(int index,
                  Object item)
Specified by:
set in interface List
Overrides:
set in class AbstractList

add

public boolean add(Object item)
Specified by:
add in interface Collection
Specified by:
add in interface List
Overrides:
add in class AbstractList

add

public void add(int index,
                Object item)
Specified by:
add in interface List
Overrides:
add in class AbstractList

remove

public Object remove(int index)
Specified by:
remove in interface List
Overrides:
remove in class AbstractList

remove

public boolean remove(Object item)
Specified by:
remove in interface Collection
Specified by:
remove in interface List
Overrides:
remove in class AbstractCollection

clear

public void clear()
Specified by:
clear in interface Collection
Specified by:
clear in interface List
Overrides:
clear in class AbstractList

addAll

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

addAll

public boolean addAll(int index,
                      Collection c)
Specified by:
addAll in interface List
Overrides:
addAll in class AbstractList

removeAll

public boolean removeAll(Collection c)
Specified by:
removeAll in interface Collection
Specified by:
removeAll in interface List
Overrides:
removeAll in class AbstractCollection

retainAll

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

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface List
Overrides:
iterator in class AbstractList

listIterator

public ListIterator listIterator()
Specified by:
listIterator in interface List
Overrides:
listIterator in class AbstractList

listIterator

public ListIterator listIterator(int index)
Specified by:
listIterator in interface List
Overrides:
listIterator in class AbstractList


Copyright © 2004 The Norther Organization. All rights reserved.