jfreerails.util
Class GrowableBase

java.lang.Object
  extended by jfreerails.util.GrowableBase
Direct Known Subclasses:
ArrayBase

public abstract class GrowableBase
extends java.lang.Object

Base class for various types of collections based on type-specific growable arrays. The underlying array used for storage of items doubles in size each time more space is required, up to an optional maximum growth increment specified by the user.

Version:
1.0
Author:
Dennis M. Sosnoski

Field Summary
protected  int countLimit
          Size of the current array.
static int DEFAULT_SIZE
          Default initial array size.
protected  int maximumGrowth
          Maximum size increment for growing array.
 
Constructor Summary
GrowableBase(GrowableBase base)
          Copy (clone) constructor.
GrowableBase(int size, java.lang.Class type)
          Constructor with partial specification.
GrowableBase(int size, int growth, java.lang.Class type)
          Constructor with full specification.
 
Method Summary
protected  java.lang.Object buildArray(java.lang.Class type, int offset, int length)
          Constructs and returns a simple array containing the same data as held in a portion of this growable array.
protected  void discardValues(int from, int to)
          Discards values for a range of indices in the array.
 void ensureCapacity(int min)
          Ensure that the array has the capacity for at least the specified number of values.
protected abstract  java.lang.Object getArray()
          Get the backing array.
protected  void growArray(int required)
          Increase the size of the array to at least a specified size.
protected  void resizeCopy(java.lang.Object base, java.lang.Object grown)
          Copy data after array resize.
protected abstract  void setArray(java.lang.Object array)
          Set the backing array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
Default initial array size.

See Also:
Constant Field Values

countLimit

protected int countLimit
Size of the current array.


maximumGrowth

protected int maximumGrowth
Maximum size increment for growing array.

Constructor Detail

GrowableBase

public GrowableBase(int size,
                    int growth,
                    java.lang.Class type)
Constructor with full specification.

Parameters:
size - number of elements in initial array
growth - maximum size increment for growing array
type - array element type

GrowableBase

public GrowableBase(int size,
                    java.lang.Class type)
Constructor with partial specification.

Parameters:
size - number of elements initially allowed in array
type - array element type

GrowableBase

public GrowableBase(GrowableBase base)
Copy (clone) constructor.

Parameters:
base - instance being copied
Method Detail

getArray

protected abstract java.lang.Object getArray()
Get the backing array. This method is used by the type-agnostic base class code to access the array used for type-specific storage by the child class.

Returns:
backing array object

setArray

protected abstract void setArray(java.lang.Object array)
Set the backing array. This method is used by the type-agnostic base class code to set the array used for type-specific storage by the child class.


resizeCopy

protected void resizeCopy(java.lang.Object base,
                          java.lang.Object grown)
Copy data after array resize. This default implementation just copies the entire contents of the old array to the start of the new array. It should be overridden in cases where data needs to be rearranged in the array after a resize.

Parameters:
base - original array containing data
grown - resized array for data

discardValues

protected void discardValues(int from,
                             int to)
Discards values for a range of indices in the array. Checks if the values stored in the array are object references, and if so clears them. If the values are primitives, this method does nothing.

Parameters:
from - index of first value to be discarded
to - index past last value to be discarded

growArray

protected void growArray(int required)
Increase the size of the array to at least a specified size. The array will normally be at least doubled in size, but if a maximum size increment was specified in the constructor and the value is less than the current size of the array, the maximum increment will be used instead. If the requested size requires more than the default growth, the requested size overrides the normal growth and determines the size of the replacement array.

Parameters:
required - new minimum size required

ensureCapacity

public final void ensureCapacity(int min)
Ensure that the array has the capacity for at least the specified number of values.

Parameters:
min - minimum capacity to be guaranteed

buildArray

protected java.lang.Object buildArray(java.lang.Class type,
                                      int offset,
                                      int length)
Constructs and returns a simple array containing the same data as held in a portion of this growable array.

Parameters:
type - element type for constructed array
offset - start offset in array
length - number of characters to use
Returns:
array containing a copy of the data