com.multicorebsp.util
Class LazyCollection<T>

java.lang.Object
  extended by com.multicorebsp.util.LazyCollection<T>
Type Parameters:
T - Data type
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>

public class LazyCollection<T>
extends java.lang.Object
implements java.util.Collection<T>

Aims for O(1) access on as many collection functions possible.


Field Summary
protected  java.util.ArrayList<java.util.Collection<? extends T>> added
          Added collections since creation
protected  java.util.Collection<T> added_items
          Added items since creation
protected  java.util.Collection<T> original
          Original collection
protected  java.util.ArrayList<java.util.Collection<?>> removed
          Removed collections since creation
protected  java.util.Collection<T> removed_items
          Removed items since creation
 
Constructor Summary
LazyCollection(java.util.Collection<T> original)
           
LazyCollection(java.util.Collection<T> original, java.util.Collection<T> addBuffer, java.util.Collection<T> removeBuffer)
          Base constructor.
 
Method Summary
 boolean add(T e)
          Does not fold.
 boolean addAll(java.util.Collection<? extends T> c)
          Does not fold.
 void clear()
          Only clears original and added buffers.
 boolean contains(java.lang.Object o)
          Any removed statements take precedance over added items! O(contains(n)), where contains(n) is the asymptotic running time of a contains operation on the datastructures used and n is the maximum number of elements of all buffers.
 boolean containsAll(java.util.Collection<?> c)
          If an element of c is contained in any of the removed buffers, will fold everything.
 void foldAll()
          Empties all buffers to reconstruct the originals array.
 boolean isEmpty()
          Will fold everything >if removed buffer is nonempty< since duplicates may or may not exist in the datastructure used.
 java.util.Iterator<T> iterator()
          Will fold everything since duplicates may or may not exist in the datastructure used.
 boolean remove(java.lang.Object o)
           
 boolean removeAll(java.util.Collection<?> c)
          Does not fold.
 boolean retainAll(java.util.Collection<?> c)
          Folds everything since retainAll will have to compare every element.
 int size()
          Will fold everything since duplicates may or may not exist in the datastructure used.
 java.lang.Object[] toArray()
          Will fold everything.
<U> U[]
toArray(U[] a)
          Will fold everything.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

original

protected java.util.Collection<T> original
Original collection


added

protected java.util.ArrayList<java.util.Collection<? extends T>> added
Added collections since creation


removed

protected java.util.ArrayList<java.util.Collection<?>> removed
Removed collections since creation


added_items

protected java.util.Collection<T> added_items
Added items since creation


removed_items

protected java.util.Collection<T> removed_items
Removed items since creation

Constructor Detail

LazyCollection

public LazyCollection(java.util.Collection<T> original)
               throws java.lang.InstantiationException,
                      java.lang.IllegalAccessException
Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException

LazyCollection

public LazyCollection(java.util.Collection<T> original,
                      java.util.Collection<T> addBuffer,
                      java.util.Collection<T> removeBuffer)
Base constructor.

Parameters:
original - Original collection.
addBuffer - Buffer to store added elements in (typically empty)
removeBuffer - Buffer to store removed elements in (typically empty)
Method Detail

add

public boolean add(T e)
Does not fold.

Specified by:
add in interface java.util.Collection<T>

addAll

public boolean addAll(java.util.Collection<? extends T> c)
Does not fold.

Specified by:
addAll in interface java.util.Collection<T>

clear

public void clear()
Only clears original and added buffers.

Specified by:
clear in interface java.util.Collection<T>

contains

public boolean contains(java.lang.Object o)
Any removed statements take precedance over added items! O(contains(n)), where contains(n) is the asymptotic running time of a contains operation on the datastructures used and n is the maximum number of elements of all buffers.

Specified by:
contains in interface java.util.Collection<T>

foldAll

public void foldAll()
Empties all buffers to reconstruct the originals array. Basically executes all buffered adds and removes. Takes a lot of time. Removes are processed last (and thus take precedence over adds).


containsAll

public boolean containsAll(java.util.Collection<?> c)
If an element of c is contained in any of the removed buffers, will fold everything.

Specified by:
containsAll in interface java.util.Collection<T>

isEmpty

public boolean isEmpty()
Will fold everything >if removed buffer is nonempty< since duplicates may or may not exist in the datastructure used.

Specified by:
isEmpty in interface java.util.Collection<T>

iterator

public java.util.Iterator<T> iterator()
Will fold everything since duplicates may or may not exist in the datastructure used.

Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection<T>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Does not fold.

Specified by:
removeAll in interface java.util.Collection<T>

retainAll

public boolean retainAll(java.util.Collection<?> c)
Folds everything since retainAll will have to compare every element.

Specified by:
retainAll in interface java.util.Collection<T>

size

public int size()
Will fold everything since duplicates may or may not exist in the datastructure used.

Specified by:
size in interface java.util.Collection<T>

toArray

public java.lang.Object[] toArray()
Will fold everything.

Specified by:
toArray in interface java.util.Collection<T>

toArray

public <U> U[] toArray(U[] a)
Will fold everything.

Specified by:
toArray in interface java.util.Collection<T>