com.partnersoft.data
Class AbstractDynamicArray<T>

java.lang.Object
  extended by com.partnersoft.data.DynamicArray
      extended by com.partnersoft.data.AbstractDynamicArray<T>
All Implemented Interfaces:
java.lang.Iterable<T>
Direct Known Subclasses:
AbstractDynamicComparableArray, DynamicObjectArray, DynamicStringArray, DynamicXyPointArray

public abstract class AbstractDynamicArray<T>
extends DynamicArray
implements java.lang.Iterable<T>

A DynamicArray of Objects.

It would be nice to make this fully parameterizable via Generics, but it doesn't seem to be possible due to the limitations on instantiation of generic types and array handling.

It would be nice to use reflection to take care of the newArray() method, too, but I've found (around v1.5) that it takes three times as long to use Array.newInstance(type, size) than to use new Foo[size]. newArray() gets called a lot, especially during early growth, so that seems bad.

So, a compromise approach. Either generate an inline class like this:

 AbstractDynamicObjectArray<Foo> array = new AbstractDynamicArray<Foo>() {
        protected Foo[] newArrayImp(size) {
                return new Foo[size];
        }
 };
 
Or, a full class like this:
  public class DynamicFooArray extends AbstractDynamicArray<Foo> {
 
  public DynamicFooArray() {
  }
 
  public DynamicFooArray(int capacity) {
  super(capacity);
  }
 
  public DynamicFooArray(Foo...contents) {
  super(contents);
  }
 
  protected Foo[] newArrayImp(size) {
  return new Foo[size];
  }
 

Copyright 2001-2006 Partner Software, Inc.

Version:
$Id$
Author:
Paul Reavis

Field Summary
 T[] array
           
 
Fields inherited from class com.partnersoft.data.DynamicArray
arrayObject, capacity, end, fastGrowthFactor, fastGrowthLimit, slowGrowthAmount, start
 
Constructor Summary
AbstractDynamicArray()
          Creates an empty DynamicObjectArray, for objects of the given type, with size and capacity of 0.
AbstractDynamicArray(int capacity)
          Creates a DynamicObjectArray of the given type and given capacity.
AbstractDynamicArray(T... contents)
          Creates a DynamicObjectArray of the given type and initial contents.
 
Method Summary
 void add(T value)
          Append a single object to the array.
 void addMultiple(T value, int copyCount)
          Append a single object multiple times.
 void append(java.util.Collection<T> values)
          Append a collection of objects to the array.
 void append(T... values)
          Append an array of objects to the array.
 java.lang.String contentsToString()
          Returns the contents as a String.
 void fitToSize(int newSize, T defaultValue)
          Sets the array to the given size by either truncating or filling with the given defaultValue.
 java.util.Iterator<T> iterator()
           
 void newArray(int size)
          Allocates a new, empty array of the given size and assign it to the arrayObject property.
protected abstract  T[] newArrayImp(int size)
          Subclasses must implement this to return an array of the correct type and size.
 T[] toFixedArray()
           
 java.util.List<T> toList()
           
 
Methods inherited from class com.partnersoft.data.DynamicArray
append, clear, copy, copy, copyExactly, copyFrom, copyTo, insert, isEmpty, makeRoomFor, makeRoomFor, pack, remove, size, subsection, tidy, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

array

public T[] array
Constructor Detail

AbstractDynamicArray

public AbstractDynamicArray()
Creates an empty DynamicObjectArray, for objects of the given type, with size and capacity of 0.


AbstractDynamicArray

public AbstractDynamicArray(int capacity)
Creates a DynamicObjectArray of the given type and given capacity.


AbstractDynamicArray

public AbstractDynamicArray(T... contents)
Creates a DynamicObjectArray of the given type and initial contents.

Method Detail

newArrayImp

protected abstract T[] newArrayImp(int size)
Subclasses must implement this to return an array of the correct type and size. Sorry, guys.


add

public void add(T value)
Append a single object to the array.


addMultiple

public void addMultiple(T value,
                        int copyCount)
Append a single object multiple times.


append

public void append(T... values)
Append an array of objects to the array.


append

public void append(java.util.Collection<T> values)
Append a collection of objects to the array.


fitToSize

public void fitToSize(int newSize,
                      T defaultValue)
Sets the array to the given size by either truncating or filling with the given defaultValue.

Parameters:
newSize -
defaultValue -

contentsToString

public java.lang.String contentsToString()
Returns the contents as a String. toString() would have been a better name but is already taken.

Returns:

toFixedArray

public final T[] toFixedArray()

toList

public java.util.List<T> toList()

iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface java.lang.Iterable<T>

newArray

public void newArray(int size)
Description copied from class: DynamicArray
Allocates a new, empty array of the given size and assign it to the arrayObject property. It is assumed that subclasses will also set it to a variable of the correct class (e.g. char[]) for ease of access as well. Copying of existing contents is Dynamic by this superclass. Your code should look something like this:
 public abstract void newArray(int size) {
     arrayObject = array = new Foo[size];
 }
 

Specified by:
newArray in class DynamicArray