com.scottlogic.util
Class AutoFixingPatchWorkArray<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<T>
          extended by com.scottlogic.util.PatchWorkArray<T>
              extended by com.scottlogic.util.AutoFixingPatchWorkArray<T>
Type Parameters:
T - The type of element that this list should store.
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>

public class AutoFixingPatchWorkArray<T>
extends PatchWorkArray<T>

Subclass of the PatchWorkArray which automatically fixes itself if the number of alterations reaches some specified limit.

Version:
1.0
Author:
Mark Rhodes
See Also:
List, Collection, ArrayList, PatchWorkArray, Serialized Form

Field Summary
static float DEFAULT_MAX_PERFORMANCE_HIT_FACTOR
          The default maximum factor of the size of a list that the performance hit can reach before it automatically fixes itself.
static int DEFAULT_MIN_PERFORMANCE_HIT_BEFORE_FIX
          The default minimum performance factor at which a list will automatically fix itself.
protected  float maxPerformanceHitFactor
          The maximum factor of the size of this list that the performance hit can reach before it automatically fixes itself.
protected  int minPerformanceHitBeforeFix
          The minimum performance factor at which this list will automatically fix itself.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
AutoFixingPatchWorkArray()
          Default constructor which fixes after the default limits are breached.
AutoFixingPatchWorkArray(int initialCapacity)
          Constructor which fixes after the default limits are breached and the backing list has the given initial capacity.
AutoFixingPatchWorkArray(int initialCapacity, float maxPerformanceHitFactor, int minPerformanceHitBeforeFix)
          Constructor which allows the limits controlling the when this list auto-fixes.
 
Method Summary
 void add(int index, T obj)
          Inserts the specified element at the specified position in this list.
protected  boolean isFixRequired()
          Determines if this list needs to be fixed or not.
 T remove(int index)
          Removes and returns the element at the given index in this list.
 
Methods inherited from class com.scottlogic.util.PatchWorkArray
add, clear, fix, get, getPerformanceHit, iterator, size
 
Methods inherited from class java.util.AbstractList
addAll, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, removeRange, set, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

DEFAULT_MAX_PERFORMANCE_HIT_FACTOR

public static final float DEFAULT_MAX_PERFORMANCE_HIT_FACTOR
The default maximum factor of the size of a list that the performance hit can reach before it automatically fixes itself.

See Also:
Constant Field Values

DEFAULT_MIN_PERFORMANCE_HIT_BEFORE_FIX

public static final int DEFAULT_MIN_PERFORMANCE_HIT_BEFORE_FIX
The default minimum performance factor at which a list will automatically fix itself.

See Also:
Constant Field Values

maxPerformanceHitFactor

protected final float maxPerformanceHitFactor
The maximum factor of the size of this list that the performance hit can reach before it automatically fixes itself.


minPerformanceHitBeforeFix

protected final int minPerformanceHitBeforeFix
The minimum performance factor at which this list will automatically fix itself.

Constructor Detail

AutoFixingPatchWorkArray

public AutoFixingPatchWorkArray()
Default constructor which fixes after the default limits are breached.


AutoFixingPatchWorkArray

public AutoFixingPatchWorkArray(int initialCapacity)
Constructor which fixes after the default limits are breached and the backing list has the given initial capacity.

Parameters:
initialCapacity - the initial capacity of the backing list.

AutoFixingPatchWorkArray

public AutoFixingPatchWorkArray(int initialCapacity,
                                float maxPerformanceHitFactor,
                                int minPerformanceHitBeforeFix)
Constructor which allows the limits controlling the when this list auto-fixes. The list automatically calls fix() when the performance hit is greater than both minPerformanceHitBeforeFix and maxPerformanceHitFactor * size().

Parameters:
initialCapacity - the initial size of the backing list.
maxPerformanceHitFactor - the fraction of size of the list that the performance hit must exceed in order for this list to fix itself. This value must be between 0 and 1 inclusive.
minPerformanceHitBeforeFix - the smallest value for the performance hit at which this list will fix itself.
Throws:
java.lang.IllegalArgumentException - in the case that the maxPerformanceHitFactor is smaller than 0 or larger than 1.
Method Detail

remove

public T remove(int index)
Removes and returns the element at the given index in this list.

This method can potentially increase or decrease the number of alterations, and hence the performance hit. If after removing the element at the given index the performance hit breaches the auto-fixing limit, fix is called.

Specified by:
remove in interface java.util.List<T>
Overrides:
remove in class PatchWorkArray<T>
Parameters:
index - the index of the element to remove.
Returns:
the element which was removed from the list.
Throws:
java.lang.IllegalArgumentException - in the case that the index is not a valid index.

add

public void add(int index,
                T obj)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

This method can potentially increase or decrease the number of alterations, and hence the performance hit. If after inserting the given element the performance hit breaches the auto-fixing limit, fix is called.

Specified by:
add in interface java.util.List<T>
Overrides:
add in class PatchWorkArray<T>
Parameters:
index - at which the element should be added.
obj - the object to by inserted.
Throws:
java.lang.IllegalArgumentException - in the case that the given element is null.
java.lang.IndexOutOfBoundsException - in the case that (0 <= index <= size()) does not hold.

isFixRequired

protected boolean isFixRequired()
Determines if this list needs to be fixed or not.

This implementation returns true when the performance hit is at least minPerformanceHitBeforeFix and is more than maxPerformanceHitFactor of the total size of the list.

Returns:
true if this list needs to be fixed and false otherwise.