Package uk.ac.starlink.array
Class Type
- java.lang.Object
-
- uk.ac.starlink.array.Type
-
public class Type extends java.lang.Object
Primitive numeric data type identifier. Objects in this class are used to identify the type of the bulk data held in anNDArray
.This class exemplifies the typesafe enum pattern -- the only possible instances are supplied as static final fields of the class, and these instances are immutable.
- Version:
- $Id$
- Author:
- Mark Taylor (Starlink)
-
-
Field Summary
Fields Modifier and Type Field Description static Type
BYTE
Object representing primitive data ofbyte
type.static Type
DOUBLE
Object representing primitive data ofdouble
type.static Type
FLOAT
Object representing primitive data offloat
type.static Type
INT
Object representing primitive data ofint
type.static Type
SHORT
Object representing primitive data ofshort
type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.util.List
allTypes()
Returns a list of all the known Types.void
checkArray(java.lang.Object array, int minsize)
Checks that a given Object is in fact an array of the primitive type corresponding to this Type, and contains at least a given number of elements; throws an exception if not.BadHandler
defaultBadHandler()
Returns a default bad value handler for this type.java.lang.Number
defaultBadValue()
Returns the default bad value used for this type.int
getNumBytes()
Returns the number of bytes occupied by this primitive type.static Type
getType(java.lang.Class cls)
Returns the Type object corresponding to a given java class.boolean
isFloating()
Indicates whether this type represents floating point values.java.lang.Class
javaClass()
Returns the java primitive class associated with this type.double
maximumValue()
Returns the highest value which can be represented by this type.double
minimumValue()
Returns the lowest (= most negative) value which can be represented by this type.java.lang.Object
newArray(int size)
Constructs a new primitive array of a requested size and the appropriate type for this object.java.lang.String
toString()
-
-
-
Field Detail
-
BYTE
public static final Type BYTE
Object representing primitive data ofbyte
type.
-
SHORT
public static final Type SHORT
Object representing primitive data ofshort
type.
-
INT
public static final Type INT
Object representing primitive data ofint
type.
-
FLOAT
public static final Type FLOAT
Object representing primitive data offloat
type.
-
DOUBLE
public static final Type DOUBLE
Object representing primitive data ofdouble
type.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getNumBytes
public int getNumBytes()
Returns the number of bytes occupied by this primitive type.- Returns:
- the size of this type in bytes
-
javaClass
public java.lang.Class javaClass()
Returns the java primitive class associated with this type.- Returns:
- the class corresponding to this object
-
defaultBadValue
public java.lang.Number defaultBadValue()
Returns the default bad value used for this type. This is never null; it is the NaN value for floating point types, and the lowest (negatative) value for the integer types.- Returns:
- the bad value used for this Type by default
-
defaultBadHandler
public BadHandler defaultBadHandler()
Returns a default bad value handler for this type. For type BYTE, this is a null handler (no values considered bad), and for the other types it is a handler using the value returned by thedefaultBadValue
method.- Returns:
- a BadHandler object implementing the default bad value policy for this type
-
isFloating
public boolean isFloating()
Indicates whether this type represents floating point values.- Returns:
- true for floating point types, false for integral types
-
minimumValue
public double minimumValue()
Returns the lowest (= most negative) value which can be represented by this type.- Returns:
- lowest value this type can represent, as a double
-
maximumValue
public double maximumValue()
Returns the highest value which can be represented by this type.- Returns:
- highest value this type can represetn, as a double
-
newArray
public java.lang.Object newArray(int size)
Constructs a new primitive array of a requested size and the appropriate type for this object. This utility method is useful for array allocation in type-generic programming. The current implementation just invokesArray.newInstance(java.lang.Class<?>, int)
.- Parameters:
size
- the number of elements required- Returns:
- a new primitive array
-
checkArray
public void checkArray(java.lang.Object array, int minsize)
Checks that a given Object is in fact an array of the primitive type corresponding to this Type, and contains at least a given number of elements; throws an exception if not. If the given Object satisfies these requirements no action is taken; if it does not then an IllegalArgumentException is thrown. This utility method is useful for parameter checking in type-generic programming.- Parameters:
array
- an Object purporting to be a java array of primitives of the right primitive type for this Type and of at least minsize elementsminsize
- the minimum satisfactory size of array- Throws:
java.lang.IllegalArgumentException
- if array is not an array or has fewer than minSize elements
-
allTypes
public static java.util.List allTypes()
Returns a list of all the known Types.- Returns:
- an unmodifiable List containing all the existing type objects.
-
getType
public static Type getType(java.lang.Class cls)
Returns the Type object corresponding to a given java class. If no corresponding type exists (cls is not one of the supported primitive numeric types) then null is returned.- Parameters:
cls
- a (presumably numeric primitive) class. May be null- Returns:
- the Type object corresponding to cls, or null
-
-