Class SimpleType<T>
- Type Parameters:
T
- The value type of this simple type, for exampleInteger
.
- All Implemented Interfaces:
Type
Users that do not implement a custom column type can ignore this class.
This class must be part of the API because it appears in the signatures of
the CL.ofArray(int, acdp.design.SimpleType<T>)
and CL.create(acdp.design.SimpleType<T>)
methods and because implementers
of custom column types need to provide a concrete subclass of this class.
However, even implementers of a custom column type can vastly ignore all
methods declared with a public
access modifier and focus on the
instance methods declared with a protected
access modifier.
Those public methods are either returning the arguments of the constructor or
are used internally by ACDP.
Implementers of a custom column type must implement the toBytes(Object)
and the fromBytes(byte[], int, int)
methods and
may want to override the toBytes(Object, byte[], int)
and the
typeDescPrefix()
methods.
Furthermore, implementers of a custom column type must implement a
public
and static
type factory method annotated with the SimpleType.TypeFromDesc
annotation.
A simple column type has a length.
If this simple column type has an inrow storage
scheme then its length is identical to the maximum number n
of bytes
of the byte representation of any value of this
simple column type.
If this simple column type has an outrow storage
scheme then n
is equal to min
(256length
-
1, Integer.MAX_VALUE
) where length
denotes the length of
this simple column type.
Other properties are explained in the description of the constructor.
- Author:
- Beat Hörmann
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final class
Provides convenient access to the individual fields of a type descriptor that is represented by aString
.static @interface
A class method annotated with theTypeFromDesc
annotation takes theString
representation of a type descriptor and returns an instance of a subclass ofSimpleType
.Nested classes/interfaces inherited from interface acdp.types.Type
Type.Scheme
-
Field Summary
FieldsFields inherited from class acdp.internal.types.Type_
scheme
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SimpleType
(Class<T> valueType, Type.Scheme scheme, boolean nullable, int limit, boolean variable) The constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal T
convertFromBytes
(byte[] bytes, int offset) Converts the specified byte subarray to anObject
.final T
convertFromBytes
(byte[] bytes, int offset, int len) Convertslen
bytes of the specified byte subarray to anObject
.final byte[]
convertToBytes
(Object val) Converts the specified value to a byte array.final int
convertToBytes
(Object val, byte[] bytes, int offset) Converts the specified value to a byte array and puts it into the specified byte array starting at the specified offset.protected abstract T
fromBytes
(byte[] bytes, int offset, int len) Converts the byte representation of the value contained in the specified byte subarray to anObject
.final String
Returns the name of the custom type factory class.final String
Returns the classpath of the custom type factory class.final boolean
isCompatible
(Object val) Tests if the specified value is compatible with this column type.final int
length()
Returns the length of this column type.final boolean
nullable()
Returns the information whether values of this column type are allowed to benull
.final void
setTypeFactoryClassName
(acdp.internal.types.TypeFactory.Friend friend, String cn) Friend-only setter of the name of the custom type factory class.final void
setTypeFactoryClasspath
(acdp.internal.types.TypeFactory.Friend friend, String cp) Friend-only setter of the classpath of the custom type factory class.protected abstract byte[]
Converts the specified value to its byte representation.protected int
Converts the specified value to its byte representation and puts it into the specified byte array starting at the specified offset.final String
typeDesc()
Returns the type descriptor of this column type.protected String
Returns the first part of the descriptor of this column type.protected static final String
typeDescSuffix
(Type.Scheme scheme, boolean nullable, int limit, boolean variable) Returns the second part of the descriptor for a column type constructed with the specified arguments.Returns the value type of this column type.final boolean
variable()
Returns the information whether the length of the byte representation of a value of this column type, which is supposed to have an inrow storage scheme, may vary from value to value.Methods inherited from class acdp.internal.types.Type_
isBuiltInType, scheme
-
Field Details
-
length
protected final int lengthSee here.
-
-
Constructor Details
-
SimpleType
protected SimpleType(Class<T> valueType, Type.Scheme scheme, boolean nullable, int limit, boolean variable) throws NullPointerException, IllegalArgumentException The constructor.- Parameters:
valueType
- The object representing the value type of this column type, not allowed to benull
.scheme
- The storage scheme of this column type, not allowed to benull
.nullable
- The information whether this column type allows thenull
value (true
) or not (false
). Typically, values of column types forbidding thenull
value can be persisted more efficiently than values of column types allowing thenull
value.limit
- The limit is directly related to the length of this column type. This value must be greater than or equal to 1 and ifscheme
is equal toScheme.OUTROW
then it must be less than or equal to 4.variable
- The information whether the length of the byte representation of a value of this column type may vary from value to value (true
) or if the length is fixed (false
). This value is ignored ifscheme
is equal toScheme.OUTROW
.- Throws:
NullPointerException
- Ifscheme
orvalueType
isnull
.IllegalArgumentException
- Iflimit
is less than 1 or, provided thatscheme
is equal toScheme.OUTROW
,limit
is greater than 4. Furthermore, this exception is thrown ifscheme
is equal toScheme.INROW
andvariable
is equal totrue
andlimit
is greater thanInteger.MAX_VALUE
- 4.
-
-
Method Details
-
typeDescSuffix
protected static final String typeDescSuffix(Type.Scheme scheme, boolean nullable, int limit, boolean variable) Returns the second part of the descriptor for a column type constructed with the specified arguments.This method is hardly ever used by type implementers.
- Parameters:
scheme
- The storage scheme of the column type, not allowed to benull
.nullable
- The information whether the column type allows thenull
value (true
) or not (false
).limit
- The limit of the column type. This value must be greater than or equal to 1 and ifscheme
is equal toScheme.OUTROW
then it must be less than or equal to 4.variable
- The information whether the length of the byte representation of a value of the column type may vary from value to value (true
) or if the length is fixed (false
). This value is ignored ifscheme
is equal toScheme.OUTROW
.- Returns:
- The suffix of the type descriptor, never
null
and never an empty string.
-
setTypeFactoryClassName
public final void setTypeFactoryClassName(acdp.internal.types.TypeFactory.Friend friend, String cn) throws NullPointerException Friend-only setter of the name of the custom type factory class. (Since the friend resides in a different package, this method has to be public.)- Parameters:
friend
- The friend, not allowed to benull
.cn
- The name of the custom type factory class.- Throws:
NullPointerException
- Iffriend
isnull
.
-
getTypeFactoryClassName
Returns the name of the custom type factory class.- Returns:
- The name of the custom type factory class.
This value is
null
if and only if this column type is a built-in column type.
-
setTypeFactoryClasspath
public final void setTypeFactoryClasspath(acdp.internal.types.TypeFactory.Friend friend, String cp) throws NullPointerException Friend-only setter of the classpath of the custom type factory class. (Since the friend resides in a different package, this method has to be public.)- Parameters:
friend
- The friend, not allowed to benull
.cp
- The classpath of the custom type factory.- Throws:
NullPointerException
- Iffriend
isnull
.
-
getTypeFactoryClasspath
Returns the classpath of the custom type factory class.- Returns:
- The classpath of the custom type factory class, may be
null
.
-
typeDescPrefix
Returns the first part of the descriptor of this column type.Implementers can override this implementation to return a prefix with a smaller length or to handle prefixes that encode custom specific information.
Note that the returned value must start with an upper case letter so that ACDP can easily distinguish between the type descriptor of a built-in column type (which starts with a lower case letter) and a type descriptor of a custom column type.
- Returns:
- The prefix of the type descriptor, never
null
and never an empty string.
-
typeDesc
Description copied from interface:Type
Returns the type descriptor of this column type.The type descriptor uniquely identifies a particular column type among all other column types. It is used by a type factory to create an instance of that column type.
If the type descriptor starts with an upper case character then this column type is a custom column type otherwise it is a built-in column type. (A custom column type is provided by a client whereas a built-in column type is provided by ACDP.)
Implementers of this method must ensure that the value returned by this method is indeed unique and that it remains consistent even across session boundaries.
- Returns:
- The column type descriptor, never
null
.
-
valueType
Returns the value type of this column type.The value type is used, for example, when an arbitrary value of type
Object
is tested for compatibility with this column type.- Returns:
- The value type of this column type, never
null
.
-
nullable
public final boolean nullable()Returns the information whether values of this column type are allowed to benull
.- Returns:
- The boolean value
true
if values of this column type are allowed to benull
,false
otherwise.
-
length
public final int length()Returns the length of this column type. The length is equal to thelimit
argument of the constructor unless this column type has an inrow storage scheme and thevariable
argument of the constructor is equal totrue
. In such a case the length is equal tolimit
+ ⌊log
256(limit
)⌋ + 1.- Returns:
- The length of this column type, always greater than or equal to 1 and if the storage scheme is an outrow storage scheme then this value is less than or equal to 4.
-
variable
public final boolean variable()Returns the information whether the length of the byte representation of a value of this column type, which is supposed to have an inrow storage scheme, may vary from value to value.- Returns:
- The boolean value
true
if and only if this column type has an inrow storage scheme and if thevariable
argument passed to the constructor was set totrue
.
-
isCompatible
Tests if the specified value is compatible with this column type. In accordance with the general definition of compatibility the specified value is compatible with this column type if and only if- The value is
null
and thenullable
method returnstrue
or - The value is not
null
but the value isassignment-compatible
with the value type of this column type.
- Parameters:
val
- The value to test for compatibility.- Returns:
- The boolean value
true
if the value is compatible with this column type,false
otherwise.
- The value is
-
toBytes
Converts the specified value to its byte representation.This method assumes that calling the
isCompatible(java.lang.Object)
method onval
returnstrue
. If this is not the case then this method may throw an exception not mentioned below.The returned length of the byte representation must be less than or equal to the value returned by the
length()
method. Implementers of this method should not check if this condition is met because it is internally checked anyway.- Parameters:
val
- The value to convert, not allowed to benull
.- Returns:
- The byte representation of the value, never
null
. - Throws:
NullPointerException
- Ifval
isnull
.
-
convertToBytes
public final byte[] convertToBytes(Object val) throws NullPointerException, IllegalArgumentException Converts the specified value to a byte array.This method assumes that calling the
isCompatible
method onval
returnstrue
. If this is not the case then this method may throw an exception that is not mentioned below.This method should be invoked only if this column type has an outrow storage scheme.
- Parameters:
val
- The value to convert, not allowed to benull
.- Returns:
- The value as a byte array, never
null
. - Throws:
NullPointerException
- Ifval
isnull
.IllegalArgumentException
- If the length of the byte representation of the specified value exceeds the maximum number of bytes allowed by this column type.
-
toBytes
protected int toBytes(T val, byte[] bytes, int offset) throws NullPointerException, IndexOutOfBoundsException Converts the specified value to its byte representation and puts it into the specified byte array starting at the specified offset.This method assumes that calling the
isCompatible(java.lang.Object)
method onval
returnstrue
. If this is not the case then this method may throw an exception not mentioned below.The returned length of the byte representation must be less than or equal to the value returned by the
length()
method. Implementers of this method should not check if this condition is met because it is internally checked anyway.Type implementers are encouraged to override this method if they can avoid a call to the
System.arraycopy
method.- Parameters:
val
- The value to convert, not allowed to benull
.bytes
- The destination byte array, not allowed to benull
.offset
- The index withinbytes
where to start saving the byte representation.- Returns:
- The length of the byte representation.
- Throws:
NullPointerException
- Ifval
orbytes
arenull
.IndexOutOfBoundsException
- If saving the byte representation would cause access of data outside of the array bounds of the specified byte array.
-
convertToBytes
public final int convertToBytes(Object val, byte[] bytes, int offset) throws NullPointerException, IllegalArgumentException, IndexOutOfBoundsException Converts the specified value to a byte array and puts it into the specified byte array starting at the specified offset.This method assumes that calling the
isCompatible
method onval
returnstrue
. If this is not the case then this method may throw an exception that is not mentioned below.This method should be invoked only if this column type has an inrow storage scheme.
- Parameters:
val
- The value to convert, not allowed to benull
.bytes
- The destination byte array, not allowed to benull
.offset
- The index withinbytes
where to start saving the the converted value.- Returns:
- The length of the byte representation.
- Throws:
NullPointerException
- Ifval
orbytes
isnull
.IllegalArgumentException
- If the length of the byte representation of the specified value exceeds the maximum number of bytes allowed by this column type.IndexOutOfBoundsException
- If saving the byte representation would cause access of data outside of the array bounds of the specified byte array.
-
fromBytes
Converts the byte representation of the value contained in the specified byte subarray to anObject
.- Parameters:
bytes
- The byte array containing the byte representation of the value, not allowed to benull
.offset
- The index withinbytes
of the first byte to convert, must be greater than or equal to zero.len
- The number of bytes to convert.- Returns:
- The resulting object.
- Throws:
IndexOutOfBoundsException
- If converting the byte representation would cause access of data outside of the array bounds of the specified byte array.
-
convertFromBytes
Converts the specified byte subarray to anObject
.This method should be invoked only if this column type has an inrow storage scheme.
- Parameters:
bytes
- The byte array containing the byte representation of the value, not allowed to benull
.offset
- The index withinbytes
of the first byte to convert, must be greater than or equal to zero.- Returns:
- The resulting object.
- Throws:
IndexOutOfBoundsException
- If converting the byte representation would cause access of data outside of the array bounds of the specified byte array.
-
convertFromBytes
Convertslen
bytes of the specified byte subarray to anObject
.This method should be invoked only if this column type has an outrow storage scheme.
- Parameters:
bytes
- The byte array containing the byte representation of the value, not allowed to benull
.offset
- The index withinbytes
of the first byte to convert, must be greater than or equal to zero.len
- The number of bytes to convert.- Returns:
- The resulting object.
- Throws:
IndexOutOfBoundsException
- If converting the byte representation would cause access of data outside of the array bounds of the specified byte array.
-