Package nom.tam.util
Class ArrayFuncs
- java.lang.Object
-
- nom.tam.util.ArrayFuncs
-
public final class ArrayFuncs extends java.lang.Object
This is a package of static functions which perform computations on arrays. Generally these routines attempt to complete without throwing errors by ignoring data they cannot understand.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Logger
LOG
-
Constructor Summary
Constructors Modifier Constructor Description private
ArrayFuncs()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String
arrayDescription(java.lang.Object o)
static long
computeLSize(java.lang.Object o)
static int
computeSize(java.lang.Object o)
Deprecated.May silently underestimate the size if the size > 2 GB.static java.lang.Object
convertArray(java.lang.Object array, java.lang.Class<?> newType)
static java.lang.Object
convertArray(java.lang.Object array, java.lang.Class<?> newType, boolean reuse)
static void
copyArray(java.lang.Object original, java.lang.Object copy)
Copy one array into another.static void
copyInto(java.lang.Object array, java.lang.Object mimic)
Copy an array into an array of a different type.static java.lang.Object
curl(java.lang.Object input, int[] dimens)
Curl an input array up into a multi-dimensional array.static java.lang.Object
deepClone(java.lang.Object o)
static java.lang.Object
flatten(java.lang.Object input)
Given an array of arbitrary dimensionality .static java.lang.Object
genericClone(java.lang.Object o)
Clone an Object if possible.static java.lang.Object
getBaseArray(java.lang.Object o)
This routine returns the base array of a multi-dimensional array.static java.lang.Class<?>
getBaseClass(java.lang.Object o)
This routine returns the base class of an object.static int
getBaseLength(java.lang.Object o)
This routine returns the size of the base element of an array.static int[]
getDimensions(java.lang.Object o)
Find the dimensions of an object.static java.lang.Object
mimicArray(java.lang.Object array, java.lang.Class<?> newType)
Create an array of a type given by new type with the dimensionality given in array.static int
nElements(java.lang.Object o)
Deprecated.May silently underestimate size if number is > 2 G.static java.lang.Object
newInstance(java.lang.Class<?> cl, int dim)
Allocate an array dynamically.static java.lang.Object
newInstance(java.lang.Class<?> cl, int[] dims)
Allocate an array dynamically.static long
nLElements(java.lang.Object o)
Deprecated.May silently underestimate size if number is > 2 G.static int[]
reverseIndices(int[] indices)
Reverse an integer array.
-
-
-
Method Detail
-
arrayDescription
public static java.lang.String arrayDescription(java.lang.Object o)
- Parameters:
o
- The array to be described.- Returns:
- a description of an array (presumed rectangular).
-
computeLSize
public static long computeLSize(java.lang.Object o)
-
computeSize
@Deprecated public static int computeSize(java.lang.Object o)
Deprecated.May silently underestimate the size if the size > 2 GB.- Parameters:
o
- The object whose size is desired.- Returns:
- Compute the size of an object. Note that this only handles arrays or scalars of the primitive objects and Strings. It returns 0 for any object array element it does not understand.
-
convertArray
public static java.lang.Object convertArray(java.lang.Object array, java.lang.Class<?> newType)
- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.- Returns:
- Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
-
convertArray
public static java.lang.Object convertArray(java.lang.Object array, java.lang.Class<?> newType, boolean reuse)
- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.reuse
- If set, and the requested type is the same as the original, then the original is returned.- Returns:
- Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
-
copyArray
public static void copyArray(java.lang.Object original, java.lang.Object copy)
Copy one array into another. This function copies the contents of one array into a previously allocated array. The arrays must agree in type and size.- Parameters:
original
- The array to be copied.copy
- The array to be copied into. This array must already be fully allocated.
-
copyInto
public static void copyInto(java.lang.Object array, java.lang.Object mimic)
Copy an array into an array of a different type. The dimensions and dimensionalities of the two arrays should be the same.- Parameters:
array
- The original array.mimic
- The array mimicking the original.
-
curl
public static java.lang.Object curl(java.lang.Object input, int[] dimens)
Curl an input array up into a multi-dimensional array.- Parameters:
input
- The one dimensional array to be curled.dimens
- The desired dimensions- Returns:
- The curled array.
-
deepClone
public static java.lang.Object deepClone(java.lang.Object o)
- Parameters:
o
- The object to be copied.- Returns:
- a deep clone of an Array or a standard clone of a scalar. The object may comprise arrays of any primitive type or any Object type which implements Cloneable. However, if the Object is some kind of collection, e.g., a Vector then only a shallow copy of that object is made. I.e., deep refers only to arrays.
-
flatten
public static java.lang.Object flatten(java.lang.Object input)
Given an array of arbitrary dimensionality .- Parameters:
input
- The input array.- Returns:
- the array flattened into a single dimension.
-
genericClone
public static java.lang.Object genericClone(java.lang.Object o)
Clone an Object if possible. This method returns an Object which is a clone of the input object. It checks if the method implements the Cloneable interface and then uses reflection to invoke the clone method. This can't be done directly since as far as the compiler is concerned the clone method for Object is protected and someone could implement Cloneable but leave the clone method protected. The cloning can fail in a variety of ways which are trapped so that it returns null instead. This method will generally create a shallow clone. If you wish a deep copy of an array the method deepClone should be used.- Parameters:
o
- The object to be cloned.- Returns:
- the clone
-
getBaseArray
public static java.lang.Object getBaseArray(java.lang.Object o)
This routine returns the base array of a multi-dimensional array. I.e., a one-d array of whatever the array is composed of. Note that arrays are not guaranteed to be rectangular, so this returns o[0][0]....- Parameters:
o
- the multi-dimensional array- Returns:
- base array of a multi-dimensional array.
-
getBaseClass
public static java.lang.Class<?> getBaseClass(java.lang.Object o)
This routine returns the base class of an object. This is just the class of the object for non-arrays.- Parameters:
o
- array to get the base class from- Returns:
- the base class of an array
-
getBaseLength
public static int getBaseLength(java.lang.Object o)
This routine returns the size of the base element of an array.- Parameters:
o
- The array object whose base length is desired.- Returns:
- the size of the object in bytes, 0 if null, or -1 if not a primitive array.
-
getDimensions
public static int[] getDimensions(java.lang.Object o)
Find the dimensions of an object. This method returns an integer array with the dimensions of the object o which should usually be an array. It returns an array of dimension 0 for scalar objects and it returns -1 for dimension which have not been allocated, e.g.,int[][][] x = new int[100][][];
should return [100,-1,-1].- Parameters:
o
- The object to get the dimensions of.- Returns:
- the dimensions of an object
-
mimicArray
public static java.lang.Object mimicArray(java.lang.Object array, java.lang.Class<?> newType)
Create an array of a type given by new type with the dimensionality given in array.- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.- Returns:
- the new array with same dimensions
-
nElements
@Deprecated public static int nElements(java.lang.Object o)
Deprecated.May silently underestimate size if number is > 2 G.- Parameters:
o
- the array to count the elements- Returns:
- Count the number of elements in an array.
-
newInstance
public static java.lang.Object newInstance(java.lang.Class<?> cl, int dim)
Allocate an array dynamically. The Array.newInstance method does not throw an error when there is insufficient memory and silently returns a null.throws an OutOfMemoryError if insufficient space is available.- Parameters:
cl
- The class of the array.dim
- The dimension of the array.- Returns:
- The allocated array.
-
newInstance
public static java.lang.Object newInstance(java.lang.Class<?> cl, int[] dims)
Allocate an array dynamically. The Array.newInstance method does not throw an error and silently returns a null.throws an OutOfMemoryError if insufficient space is available.- Parameters:
cl
- The class of the array.dims
- The dimensions of the array.- Returns:
- The allocated array.
-
nLElements
@Deprecated public static long nLElements(java.lang.Object o)
Deprecated.May silently underestimate size if number is > 2 G.- Parameters:
o
- the array to count elements in- Returns:
- Count the number of elements in an array.
-
reverseIndices
public static int[] reverseIndices(int[] indices)
Reverse an integer array. This can be especially useful when dealing with an array of indices in FITS order that you wish to have in Java order.- Parameters:
indices
- the array to reverse- Returns:
- the reversed array.
-
-