org.erights.e.elib.tables
Class ArrayHelper

java.lang.Object
  |
  +--org.erights.e.elib.tables.ArrayHelper

public class ArrayHelper
extends Object

Untamed: Dangerous, for use by trusted code only.

For when you really want to deal with arrays rather than lists for efficiency (or because you're building lists out of arrays), but want to deal with the arrays in an E-like manner.

This class must not be made safe, as it provides public operations that mutate arrays, and that break the encapsulation of lists. Use only with great care, and never to mutate the array inside a list.

Author:
Mark S. Miller

Field Summary
private static Hashtable ArrayTypeMap
           
private static Class[][] ArrayTypes
           
 
Constructor Summary
private ArrayHelper()
          prevents instantiation
 
Method Summary
static void arraycopy(Object src, int src_position, Object dst, int dst_position, int length)
          Enabled: Like System.arraycopy(Object, int, Object, int, int), but automatically coerces if necessary, and is void/null tolerant.
static void arraySet(Object array, int index, Object val)
          Enabled: Like Array.set(Object, int, Object), but automatically coerces if necessary, and is void/null tolerant.
static Object getFastArray(EList list)
          Enabled: Like list.getArray(), but may provide direct access to the array encapsulated inside a list for speed.
private static Hashtable initArrayTypeMap(int from, int to)
          We use a Hashtable rather than one of our own in order to avoid a circular init-time dependency, since our tables depend on mapping scalar types.
static Object newArray(Class elType, int size)
          Enabled: Like Array.newInstance(Class, int), but the type is first converted according to typeForArray(Class).
static Object resize(Object oldArray, int newLen)
          Enabled: Returns a copy of oldArray but of length newLen.
static Object slice(Object oldArray, int start, int bound)
          Enabled: Returns a copy of the part of oldArray between start inclusive and bound exclusive.
static Class typeForArray(Class elType)
          Enabled: Given a class, return a preferred class for making an array of (abstractly) the kinds of values described by the argument class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ArrayTypes

private static final Class[][] ArrayTypes

ArrayTypeMap

private static final Hashtable ArrayTypeMap
Constructor Detail

ArrayHelper

private ArrayHelper()
prevents instantiation

Method Detail

initArrayTypeMap

private static Hashtable initArrayTypeMap(int from,
                                          int to)
We use a Hashtable rather than one of our own in order to avoid a circular init-time dependency, since our tables depend on mapping scalar types.


typeForArray

public static Class typeForArray(Class elType)
Enabled: Given a class, return a preferred class for making an array of (abstractly) the kinds of values described by the argument class.

It maps Void.TYPE to Void.class.
It maps boxed scalar types, like Integer.class, to the corresponding primitive (unboxed) scalar type, like Integer.TYPE. Otherwise, it just returns its argument.


newArray

public static Object newArray(Class elType,
                              int size)
Enabled: Like Array.newInstance(Class, int), but the type is first converted according to typeForArray(Class).


getFastArray

public static Object getFastArray(EList list)
Enabled: Like list.getArray(), but may provide direct access to the array encapsulated inside a list for speed.

Use this for good and not for evil. In particular, it must never be used to mutate the array inside a list, or to enable such mutation.

Parameters:
list -
Returns:

resize

public static Object resize(Object oldArray,
                            int newLen)
Enabled: Returns a copy of oldArray but of length newLen.

If newLen is larger than the original length, then the extra slots are filled with the zero element for this type. This is null for any Java reference type (and non-primitive class), and 0, '\0', 0.0, or false, as appropriate, for corresponding scalar types.

If newLen is smaller than the original, the extra elements are dropped.


slice

public static Object slice(Object oldArray,
                           int start,
                           int bound)
Enabled: Returns a copy of the part of oldArray between start inclusive and bound exclusive.

Like EList.run(int, int), but for arrays.


arraySet

public static void arraySet(Object array,
                            int index,
                            Object val)
Enabled: Like Array.set(Object, int, Object), but automatically coerces if necessary, and is void/null tolerant.


arraycopy

public static void arraycopy(Object src,
                             int src_position,
                             Object dst,
                             int dst_position,
                             int length)
Enabled: Like System.arraycopy(Object, int, Object, int, int), but automatically coerces if necessary, and is void/null tolerant.

Parameters:
src -
src_position -
dst -
dst_position -
length -


comments?