javax.baja.nre.util
Class Array

java.lang.Object
  extended byjavax.baja.nre.util.Array

public class Array
extends java.lang.Object

Array is a dynamic array similar to Vector or ArrayList except that it stores its elements in an array of a specific type. This provides fail fast capability when adding items of the wrong type and is easy to use when converting back to a Java array.


Constructor Summary
Array()
          Convenience for Array(Object.class, 10)
Array(java.lang.Class ofClass)
          Convenience for Array(ofClass, 10)
Array(java.lang.Class ofClass, java.util.Collection c)
          Construct an Array using Collection.toArray()
Array(java.lang.Class ofClass, int capacity)
          Create an array of the specified type.
Array(java.util.Collection c)
          Convenience forArray(Object.class, c)
Array(java.lang.Object[] array)
          Convenience for Array(array, array.length)
Array(java.lang.Object[] array, int size)
          Construct an Array to use the specified array as the initial internal array including its contents.
 
Method Summary
 void add(int index, java.lang.Object item)
          Add the item at the specified index.
 boolean add(java.lang.Object item)
          Add the item to the end of this array.
 void addAll(Array array)
          Convenience for add(array.array(), array.size()).
 void addAll(java.util.Collection c)
          Add all the items from the collection to this array.
 void addAll(java.lang.Object[] array)
          Convenience for add(array, array.length).
 void addAll(java.lang.Object[] array, int size)
          Add all the items from array[0] to array[size-1].
 Array apply(java.lang.Class resultOf, ILambda lambda)
          Return a new Array where the items are the result of applying the lambda function to all this Array's items.
 Array apply(ILambda lambda)
          Convenience for apply(null, lambda).
 java.lang.Object[] array()
          Get a direct reference to the internal array.
 void clear()
          Remove all the items.
 boolean contains(java.lang.Object item)
          Return if this array contains the specified item using equals().
 Array copy()
          Return a shallow copy of this array.
 Array copy(int beginIndex)
          Returns a new Array that is a subsequence of this Array.
 Array copy(int beginIndex, int endIndex)
          Returns a new Array that is a subsequence of this Array.
 boolean equals(java.lang.Object o)
          Compare another Array for equality.
 Array filter(IFilter filter)
          Return a new Array with this Array's items which return true for filter.include().
 Array filterNull()
          Return a new Array with all the null items filtered out.
 java.lang.Object first()
          Get the item at index 0 or return null if empty.
 java.lang.Object get(int index)
          Get the item at the specified index.
 void grow(int length)
          Grow the array to ensure the specified length.
 int indexOf(java.lang.Object item)
          Return the index of the specified object using equals(), or -1 if not in this array.
 int indexOf(java.lang.Object item, int fromIndex)
          Return the index of the specified object using equals(), or -1 if not in this array, starting the search at the specified index.
 Array intersection(java.lang.Object[] items)
          Return a new Array that contains the intersection of this Array's contents and the specified items.
 boolean isEmpty()
          Return if size is zero.
 java.util.ListIterator iterator()
          Return an instance of ListIterator to iterator through the items in the array.
 java.lang.Object last()
          Get the item at index size-1 or return null if empty.
 int lastIndexOf(java.lang.Object item)
          Return the rightmost index of the specified object using equals(), or -1 if not in this array, searching backwards starting at size() - 1.
 int lastIndexOf(java.lang.Object item, int fromIndex)
          Return the rightmost index of the specified object using equals(), or -1 if not in this array, searching backwards starting at the specified index.
 java.util.List list()
          Return a modifiable List which wraps this Array.
 java.lang.Class ofClass()
          Get the Class type of the items in this array.
 java.lang.Object peek()
          Convenience for get(size()-1) when working with the array as a stack.
 java.lang.Object pop()
          Convenience for remove(size()-1) when working with the array as a stack.
 void push(java.lang.Object item)
          Convenience for add(item) when working with the array as a stack.
 java.lang.Object remove(int index)
          Remove the item at the specified index and return it.
 boolean remove(java.lang.Object item)
          If the item is in this array using indexOf() then remove it and return true.
 Array removeAll(java.lang.Object[] items)
          Return a new Array having this Array's contents, with all the specified items filtered out.
 void removeRange(int fromIndex, int toIndex)
          Remove from this Array all of the items whose index is between fromIndex, inclusive and toIndex, exclusive.
 Array reverse()
          Return a new Aray with the order of the items reversed.
 Array rsort()
          Return a new Array with the items sorted in descending order using SortUtil.
 java.lang.Object set(int index, java.lang.Object item)
          Replace the item at the specified index and return the old item.
 int size()
          Return the number of items in the array.
 Array slice(int fromIndex, int toIndex)
          Returns a new Array that is a shallow copy of the portion of this Array that lies between fromIndex, inclusive, and toIndex, exclusive.
 Array sort()
          Return a new Array with the items sorted in ascending order using SortUtil.
 Array sort(java.util.Comparator comparator)
          Return a new Array with the items sorted using specified Comparator.
 void swap(int i1, int i2)
          Swap two items in the array.
 java.lang.String toString()
          Return a string representation of the Array.
 java.lang.Object[] trim()
          Trim the internal array to size() and get a direct reference to it.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Array

public Array()
Convenience for Array(Object.class, 10)


Array

public Array(java.lang.Class ofClass)
Convenience for Array(ofClass, 10)


Array

public Array(java.lang.Class ofClass,
             int capacity)
Create an array of the specified type.


Array

public Array(java.lang.Object[] array)
Convenience for Array(array, array.length)


Array

public Array(java.lang.Object[] array,
             int size)
Construct an Array to use the specified array as the initial internal array including its contents.


Array

public Array(java.lang.Class ofClass,
             java.util.Collection c)
Construct an Array using Collection.toArray()


Array

public Array(java.util.Collection c)
Convenience forArray(Object.class, c)

Method Detail

ofClass

public final java.lang.Class ofClass()
Get the Class type of the items in this array. This is always equal to array().getClass().getComponentType().


get

public final java.lang.Object get(int index)
Get the item at the specified index.


indexOf

public final int indexOf(java.lang.Object item)
Return the index of the specified object using equals(), or -1 if not in this array.


indexOf

public final int indexOf(java.lang.Object item,
                         int fromIndex)
Return the index of the specified object using equals(), or -1 if not in this array, starting the search at the specified index.


lastIndexOf

public final int lastIndexOf(java.lang.Object item)
Return the rightmost index of the specified object using equals(), or -1 if not in this array, searching backwards starting at size() - 1.


lastIndexOf

public final int lastIndexOf(java.lang.Object item,
                             int fromIndex)
Return the rightmost index of the specified object using equals(), or -1 if not in this array, searching backwards starting at the specified index.


contains

public final boolean contains(java.lang.Object item)
Return if this array contains the specified item using equals().


size

public final int size()
Return the number of items in the array.


isEmpty

public final boolean isEmpty()
Return if size is zero.


equals

public boolean equals(java.lang.Object o)
Compare another Array for equality. Return true only if both Arrays have the same size() and ofClass() and every item returns true for their equals() method.


toString

public java.lang.String toString()
Return a string representation of the Array.


add

public final boolean add(java.lang.Object item)
Add the item to the end of this array.


add

public final void add(int index,
                      java.lang.Object item)
Add the item at the specified index. All items after index are shifted up one index.


addAll

public final void addAll(java.lang.Object[] array)
Convenience for add(array, array.length).


addAll

public final void addAll(Array array)
Convenience for add(array.array(), array.size()).


addAll

public final void addAll(java.lang.Object[] array,
                         int size)
Add all the items from array[0] to array[size-1].


addAll

public final void addAll(java.util.Collection c)
Add all the items from the collection to this array.


set

public final java.lang.Object set(int index,
                                  java.lang.Object item)
Replace the item at the specified index and return the old item.


remove

public final java.lang.Object remove(int index)
Remove the item at the specified index and return it.


remove

public final boolean remove(java.lang.Object item)
If the item is in this array using indexOf() then remove it and return true. If not in this array return false.


removeAll

public Array removeAll(java.lang.Object[] items)
Return a new Array having this Array's contents, with all the specified items filtered out.


removeRange

public final void removeRange(int fromIndex,
                              int toIndex)
Remove from this Array all of the items whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding items to the left (reduces their index). This call shortens the array by (toIndex - fromIndex) items. (If toIndex==fromIndex, this operation has no effect.)

Parameters:
fromIndex - index of first item to be removed.
toIndex - index after last item to be removed.

slice

public Array slice(int fromIndex,
                   int toIndex)
Returns a new Array that is a shallow copy of the portion of this Array that lies between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned array is empty.)

Parameters:
fromIndex - low endpoint (inclusive) of the slice.
toIndex - high endpoint (exclusive) of the slice.

intersection

public Array intersection(java.lang.Object[] items)
Return a new Array that contains the intersection of this Array's contents and the specified items.


push

public final void push(java.lang.Object item)
Convenience for add(item) when working with the array as a stack.


pop

public final java.lang.Object pop()
Convenience for remove(size()-1) when working with the array as a stack.


peek

public final java.lang.Object peek()
Convenience for get(size()-1) when working with the array as a stack.


first

public final java.lang.Object first()
Get the item at index 0 or return null if empty.


last

public final java.lang.Object last()
Get the item at index size-1 or return null if empty.


grow

public final void grow(int length)
Grow the array to ensure the specified length.


array

public final java.lang.Object[] array()
Get a direct reference to the internal array. The component type of this array is ofClass(). The length of the array is not guaranteed to be equal to size().


trim

public final java.lang.Object[] trim()
Trim the internal array to size() and get a direct reference to it. The component type of this array is ofClass().


clear

public final void clear()
Remove all the items.


iterator

public final java.util.ListIterator iterator()
Return an instance of ListIterator to iterator through the items in the array.


list

public final java.util.List list()
Return a modifiable List which wraps this Array.


swap

public void swap(int i1,
                 int i2)
Swap two items in the array.


copy

public Array copy()
Return a shallow copy of this array. The new Array has a different internal array, but points to the same items.


copy

public Array copy(int beginIndex)
Returns a new Array that is a subsequence of this Array. The subsequence begins with the item at the specified index and extends to the end of this Array.


copy

public Array copy(int beginIndex,
                  int endIndex)
Returns a new Array that is a subsequence of this Array. The subsequence begins at the specified beginIndex and extends to the item at index endIndex - 1.


filter

public Array filter(IFilter filter)
Return a new Array with this Array's items which return true for filter.include(). For example to filter out Strings of length 0:
 
 a = a.filter(new IFilter() 
   {
     public boolean accept(Object o) { return o.toString().length() > 0; }
   });                                     
 


filterNull

public Array filterNull()
Return a new Array with all the null items filtered out.


apply

public Array apply(java.lang.Class resultOf,
                   ILambda lambda)
Return a new Array where the items are the result of applying the lambda function to all this Array's items. For example to convert an Array of Strings to lowercase:
 
 a = a.apply(null, new ILambda() 
   {
     public Object eval(Object o) { return o.toString().toLowerCase(); }
   });                                     
 
The resultOf parameter specifies the ofClass of the new array. If resultOf is null then the new array of the same class as this array.


apply

public Array apply(ILambda lambda)
Convenience for apply(null, lambda).


sort

public Array sort(java.util.Comparator comparator)
Return a new Array with the items sorted using specified Comparator.


sort

public Array sort()
Return a new Array with the items sorted in ascending order using SortUtil.


rsort

public Array rsort()
Return a new Array with the items sorted in descending order using SortUtil.


reverse

public Array reverse()
Return a new Aray with the order of the items reversed.