ariba.util.core
Class DynamicArray

java.lang.Object
  extended by ariba.util.core.DynamicArray
Direct Known Subclasses:
BooleanArray, DoubleArray, FileArray, IntegerArray, MapArray, ObjectArray, StringArray

public abstract class DynamicArray
extends java.lang.Object

A DynamicArray is like List except that is exposes the underlying array. DynamicArray is abstract so that clients can supply a method to allocate the array to be of a particular type. This gets back some of the runtime type checking that List loses, and allows more concise enumeration. Here is a sample concrete subclass:

        public class IntegerArray extends DynamicArray
        {
            public Object[] alloc (int size)
            {
                return new Integer[ size ];
            }

            public final Integer[] array ()
            {
                return (Integer[]) array;
            }
        }
    

A use of IntegerArray might look like this:

        IntegerArray v = Util.integerList();
        v.addElement (Constants.getInteger(1));
        v.addElement (Constants.getInteger(2));

        Integer[] a = v.array();
        for (int k = 0; k < v.inUse; k++) {
            doIntegerOp(a[k]);
        }
    

Using List, each element access in the loop would entail a procedure call and a cast.

Note that this is not a thread-safe implementation. Consumer is responsible for synchronizing the access to the object


Field Summary
 java.lang.Object[] array
          The storage array.
 int inUse
          Deprecated. see inUse()
 
Constructor Summary
DynamicArray()
          Create a DynamicArray with a default storage size.
DynamicArray(int size)
          Create a new DynamicArray
 
Method Summary
 void add(int index, java.lang.Object element)
          Insert an object into the array
 void add(java.lang.Object o)
          Add an element to the array.
 void addAll(java.util.Collection c)
          Add all the elements of the specified Collection.
 void addAll(DynamicArray that)
          Add all the elements from another array.
 void addAll(java.lang.Object[] that)
          Add all the elements from another array.
 void addElement(java.lang.Object o)
          Deprecated. use add instead
 void addElementIfAbsent(java.lang.Object o)
          Add an element to the array, if it isn't in the array
 void addElements(DynamicArray that)
          Add all the elements from another array.
 void addElements(java.lang.Object[] that)
          Add all the elements from another array.
 void addElementsIfAbsent(java.lang.Object[] that)
          Add all elements in the passed array if not already present.
 void addIdentical(java.lang.Object o)
          Add an element to the array, if it isn't in the array
abstract  java.lang.Object[] alloc(int size)
          Allocate the storage array.
 java.lang.Object[] arrayCopy()
          Return a copy of the underlying array sized to fit the number of allocated elements.
 void clear()
          Resets the array The size of underlying array remains the same
 boolean contains(java.lang.Object x)
          Check if the array contains an object.
 boolean containsIdentical(java.lang.Object x)
          Check if the array contains an object.
 java.lang.Object[] extend(java.lang.Object[] a, java.lang.Object x)
          A version of addElement for cases where all you've got is a Foo[]
 int indexOf(java.lang.Object o)
          Find the location of an object in the array.
 void insertElementAt(java.lang.Object element, int index)
          Insert an object into the array
 int inUse()
          The number of members that are currently used.
 void makeRoomFor(int want)
          Make sure there is room for additional elements.
 java.lang.Object remove(int index)
          Remove an element from the array
 java.lang.Object removeElementAt(int index)
          Remove an element from the array
 void setArray(java.lang.Object[] array)
          Replace the storage array.
 void setCapacity(int size)
          Change the size of the storage.
 java.util.List toList()
          Converts the array to a List.
 java.lang.String toString()
          Converts to a String.
 java.lang.Object[] trim()
          Make the allocated length equal the number of elements in use.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

array

public java.lang.Object[] array
The storage array. This member should be accessed directly for reading only It is public for unit tests only.


inUse

public int inUse
Deprecated. see inUse()
The number of members that are currently used. This is the logical length of the array. This member should be accessed directly only for reading.

Constructor Detail

DynamicArray

public DynamicArray()
Create a DynamicArray with a default storage size.


DynamicArray

public DynamicArray(int size)
Create a new DynamicArray

Parameters:
size - The starting size of the storage array. A size of -1 creates an instance with no array allocated.
Method Detail

inUse

public int inUse()
The number of members that are currently used. This is the logical length of the array. This member should be accessed directly only for reading.

Returns:
the number of members that are currently used

setArray

public void setArray(java.lang.Object[] array)
Replace the storage array. The new storage array has to be the right type or the next call to array() will cause a ClassCastException. Must override in subclass if using the local array hack to get around the Netscape varifier bug involving casting of Object arrays.

Parameters:
array - The new array to use as storage

setCapacity

public void setCapacity(int size)
Change the size of the storage. The size cannot be decreased below the number of elements that are currently being used.

Parameters:
size - The new requested size

makeRoomFor

public void makeRoomFor(int want)
Make sure there is room for additional elements.

Parameters:
want - The number of elements we want to have space for

addElement

public void addElement(java.lang.Object o)
Deprecated. use add instead

Add an element to the array.

Parameters:
o - The element to add
See Also:
add(Object)

add

public void add(java.lang.Object o)
Add an element to the array.

Parameters:
o - The element to add

addElementIfAbsent

public void addElementIfAbsent(java.lang.Object o)
Add an element to the array, if it isn't in the array

Parameters:
o - The element to add. If o is already in the array then it is not added. o.equals() is used to compare elements.

addElementsIfAbsent

public void addElementsIfAbsent(java.lang.Object[] that)
Add all elements in the passed array if not already present.

Parameters:
that -
See Also:
(Object)

addIdentical

public void addIdentical(java.lang.Object o)
Add an element to the array, if it isn't in the array

Parameters:
o - The element to add. If o is already in the array then it is not added. The == operator is used to compare elements.

addElements

public void addElements(DynamicArray that)
Add all the elements from another array.

Parameters:
that - The array that we will copy from

addAll

public void addAll(DynamicArray that)
Add all the elements from another array.

Parameters:
that - The array that we will copy from

addAll

public void addAll(java.util.Collection c)
Add all the elements of the specified Collection.

Parameters:
c - The Collection that we will copy from

addElements

public void addElements(java.lang.Object[] that)
Add all the elements from another array.

Parameters:
that - The array that we will copy from

addAll

public void addAll(java.lang.Object[] that)
Add all the elements from another array.

Parameters:
that - The array that we will copy from

containsIdentical

public boolean containsIdentical(java.lang.Object x)
Check if the array contains an object. The == operator is used to compare objects

Parameters:
x - the object to check for
Returns:
true if the object is in the array

contains

public boolean contains(java.lang.Object x)
Check if the array contains an object. Object.equals() is used to compare objects.

Parameters:
x - the object to check for
Returns:
true if the object is in the array

indexOf

public int indexOf(java.lang.Object o)
Find the location of an object in the array. Object.equals() is used to compare objects.

Parameters:
o - The object to look for
Returns:
the index of the object, or -1 if it is not found

arrayCopy

public java.lang.Object[] arrayCopy()
Return a copy of the underlying array sized to fit the number of allocated elements.

Returns:
The array copy

trim

public java.lang.Object[] trim()
Make the allocated length equal the number of elements in use.

Returns:
the trimmed array.

removeElementAt

public java.lang.Object removeElementAt(int index)
Remove an element from the array

Parameters:
index - The index of the element to remove
Returns:
the removed object
Throws:
java.lang.ArrayIndexOutOfBoundsException - when index is larger than the logical size of the array (inUse) or when it is less than 0.

remove

public java.lang.Object remove(int index)
Remove an element from the array

Parameters:
index - The index of the element to remove
Returns:
the removed object
Throws:
java.lang.ArrayIndexOutOfBoundsException - when index is larger than the logical size of the array (inUse) or when it is less than 0.

insertElementAt

public void insertElementAt(java.lang.Object element,
                            int index)
Insert an object into the array

Parameters:
element - The object to be inserted
index - The location where the object should be inserted
Throws:
java.lang.ArrayIndexOutOfBoundsException - when index is larger than the logical size of the array (inUse)

add

public void add(int index,
                java.lang.Object element)
Insert an object into the array

Parameters:
element - The object to be inserted
index - The location where the object should be inserted
Throws:
java.lang.ArrayIndexOutOfBoundsException - when index is larger than the logical size of the array (inUse)

alloc

public abstract java.lang.Object[] alloc(int size)
Allocate the storage array. Subclasses should override this, allocating an array of the correct type.

Parameters:
size - The size of the array to allocate
Returns:
The allocated array

extend

public java.lang.Object[] extend(java.lang.Object[] a,
                                 java.lang.Object x)
A version of addElement for cases where all you've got is a Foo[]

Parameters:
a - original array
x - element to add
Returns:
new array of length a.length+1 containing contents of a plus x as the last element

toString

public java.lang.String toString()
Converts to a String. toString() is called on each element individually and then a space is placed between each element. The entire array is enclosed in parentheses

Overrides:
toString in class java.lang.Object
Returns:
the String representation of the Dynamic array

toList

public java.util.List toList()
Converts the array to a List.

Returns:
A List version of the array

clear

public void clear()
Resets the array The size of underlying array remains the same



AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.