Class ArrayMetaData
Provides metadata about an array or array type and allows you to generate arrays with a different base
type or dimensionality. The base type is the component type of the
innermost array of an n-dimensional array. So for int[][][] that would be int.
int[][] is the component type of int[][][]; int is its base type. In other words:
the base type of an array always is a non-array type. (NB while component type is standard
terminology, there is no commonly accepted term for the type of objects that ultimately occupy the slots in
the array — in any dimension. Here we use the term base type. Element type would
another reasonable option.)
Example usage:
ArrayMetaData metadata = ArrayMetaData.of(int[][].class); Integer[][][] cube = metadata.box().addDimension().newHyperCube(10);
-
Method Summary
Modifier and TypeMethodDescriptionReturns anArrayMetaDatainstance with one more dimension than this instance.box()Returns anArrayMetaDatainstance for the boxed version of the base type.static intcountDimensions(Class<?> clazz) Returns zero for non-array types, and the number of dimensions for array types.static intcountDimensions(Object obj) Returns zero for non-array objects, and the number of dimensions for arrays.static StringReturns a description of the provided array.booleanstatic ArrayMetaDataReturns anArrayMetaDatainstance corresponding to the specified array object.Class<?> Returns theClassobject corresponding to this instance.Returns the class name of the array type represented by this instance.Class<?> Returns the base type of the array represented by this instance.static Class<?> getBaseType(Class<?> arrayType) Returns the base type of the specified array type.static Class<?> getBaseType(Object array) Returns the base type of the specified array.intReturns the number of dimensions of the array represented by this instance.inthashCode()<T> TnewArray(int length, int... lengths) Creates a new array with the specified length, using the base type and number of dimensions of thisArrayMetaDatainstance.<T> TnewHyperCube(int length) Creates a "hyper cube" in which all arrays in all dimensions have the same length (as innew int[10][10][10]).static ArrayMetaDataReturns aArrayMetaDatainstance corresponding to the specified array type.static ArrayMetaDataCreates a newArrayMetaDatainstance.Returns anArrayMetaDatainstance with one dimension less than this instance.toString()Returns the simple class name of the array type represented by this instance.unbox()Returns theArrayMetaDatafor the unboxed version of the base type.withBaseType(Class<?> elementType) Returns anArrayMetaDatainstance with the same number of dimensions as this instance, and with the specified base type.withDimensions(int dimensions) Returns anArrayMetaDatainstance with the same base type as this instance, and with the specified number of dimensions.
-
Method Details
-
describe
Returns a description of the provided array. It contains the base type's simple class name and the length of the outermost array. For example, for an array defined asnew Double[4][12], this method would return "Double[4][]".- Parameters:
array- the array to describe- Returns:
- a description of the array
-
getBaseType
Returns the base type of the specified array type. AnIllegalArgumentExceptionis thrown if the provided class is not an array type.- Parameters:
arrayType- the array type to inspect- Returns:
- the base type of the specified array type
-
getBaseType
Returns the base type of the specified array. AnIllegalArgumentExceptionis thrown if the provided object is not an array.- Parameters:
array- the array to inspect- Returns:
- the base type of the specified array
-
countDimensions
Returns zero for non-array types, and the number of dimensions for array types.- Parameters:
clazz- the type for which to get the number of dimensions- Returns:
- the dimensionality of the type.
-
countDimensions
Returns zero for non-array objects, and the number of dimensions for arrays.- Parameters:
obj- the object for which to get the number of dimensions- Returns:
- the dimensionality of the object.
-
forArray
Returns anArrayMetaDatainstance corresponding to the specified array object. AnIllegalArgumentExceptionis thrown if the provided object is not an array.- Parameters:
array- the array- Returns:
- the
ArrayMetaDatainstance
-
of
Returns aArrayMetaDatainstance corresponding to the specified array type. AnIllegalArgumentExceptionis thrown if the provided class object does not represent an array type.- Parameters:
arrayType- the array class- Returns:
- the
ArrayMetaDatainstance
-
of
Creates a newArrayMetaDatainstance. The provided type may or may not be an array type. If it is an array type, its base type will become the base type of theArrayMetaDatainstance and its dimension count will be added to the provided number of dimensions. If the provided type is not an array type, the type itself will become the base type of theArrayMetaDatainstance. The specified number of dimensions may be zero or negative, as long as the sum of the dimensions remains positive:var twoDimensional = float[][].class; // base type: float var threeDimensional = ArrayMetaData.of(twoDimensional, 1); // represents float[][][].class var oneDimensional = ArrayMetaData.of(twoDimensional, -1); // represents float[].class- Parameters:
type- the type to inspectdimensions- the number of dimensions
-
getBaseType
Returns the base type of the array represented by this instance.- Returns:
- the base type of the array represented by this instance
-
getDimensions
public int getDimensions()Returns the number of dimensions of the array represented by this instance.- Returns:
- the number of dimensions of the array represented by this instance
-
getArrayClass
Returns theClassobject corresponding to this instance. For example, if the base type of this instance isbyteand the number of dimensions recorded by this instance is three, then this method will returnbyte[][][].class.- Returns:
- a
Classobject
-
newArray
public <T> T newArray(int length, int... lengths) Creates a new array with the specified length, using the base type and number of dimensions of this
ArrayMetaDatainstance. For example, if the provided length is 10, the base type isshort.class, and the number of dimensions is three, then calling this method amounts to callingnew short[10][][]:var expected = new short[10][][]; ArrayMetaData metadata = ArrayMetaData.of(short.class, 3); var actual = metadata.newArray(10); assertArrayEquals(expected, actual);If this
ArrayMetaDatainstance represents a multidimensional array, you may optionally specify the lengths of the arrays in the other dimensions (akin to callingnew short[10][8][]ornew short[10][8][15]).- Type Parameters:
T- the type of the array- Parameters:
length- the length of the arraylengths- the lengths of the arrays in the other dimensions- Returns:
- a new array
-
newHyperCube
public <T> T newHyperCube(int length) Creates a "hyper cube" in which all arrays in all dimensions have the same length (as innew int[10][10][10]). Note that "hyper cube" is in fact a misnomer, because this method can also be used to create a two-dimensional array (new int[10][10), or even a one-dimensional array.- Type Parameters:
T- the type of the array- Parameters:
length- the array length in all dimensions- Returns:
- a new array
-
withBaseType
Returns anArrayMetaDatainstance with the same number of dimensions as this instance, and with the specified base type.- Parameters:
elementType- the base type of the returnedArrayMetaDatainstance- Returns:
- an
ArrayMetaDatainstance with the same number of dimensions as this instance, and with the specified base type
-
withDimensions
Returns anArrayMetaDatainstance with the same base type as this instance, and with the specified number of dimensions.- Parameters:
dimensions- the number of dimensions- Returns:
- an
ArrayMetaDatainstance with the same base type as this instance, and with the specified number of dimensions
-
addDimension
Returns anArrayMetaDatainstance with one more dimension than this instance.- Returns:
- an
ArrayMetaDatainstance with one more dimension than this instance
-
removeDimension
Returns anArrayMetaDatainstance with one dimension less than this instance.- Returns:
- an
ArrayMetaDatainstance with one dimension less than this instance
-
box
Returns anArrayMetaDatainstance for the boxed version of the base type. If the base type of this instance is not a primitive type, the instance itself is returned.- Returns:
- an
ArrayMetaDatafor the boxed version of the base type
-
unbox
Returns theArrayMetaDatafor the unboxed version of the base type. If the base type of this instance is not a primitive wrapper type (likeDouble), the instance itself is returned.- Returns:
- the
ArrayMetaDatafor the unboxed version of the base type
-
equals
-
hashCode
-
toString
Returns the simple class name of the array type represented by this instance. The returned string is easier to understand than what you get fromClass.getSimpleName(). For example the return value forint[][].classwould be "int[][]". -
getArrayClassName
Returns the class name of the array type represented by this instance. The returned string is easier to understand than what you get fromClass.getName(). For example, the return value forString[][].classwould be "String[][]". For types outside thejava.langpackage, the fully-qualified class name is used (e.g. "java.io.File[][]").- Returns:
- the class name of the array type represented by this
ArrayMetaData
-