Package ch.tocco.nice2.types.api
Interface Type<T>
- All Known Implementing Classes:
TypeImpl
public interface Type<T>
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionAttempts to convert ("parse") the given string input into the data type.<C> Type
<C> int
Compares two values of this type.boolean
<C> C
Converts the value of this type to the target type.<A> A
findAdapter
(Class<A> adapterClass) Returns the type adapter as contributed in hive for this type or its base type.<A> A
getAdapter
(Class<A> adapterClass) Returns the type adapter as contributed in hive for this type.@Nullable Object
Returns what this type considers to be "empty".getName()
The name of the type, for example "string", see https://wiki.tocco.ch/wiki/index.php/Nice2-typesgetRelationTo
(Type<?> target) The class this type represents, for example java.lang.String.int
getSize()
Deprecated.Collection
<Type<T>> boolean
This only returns true for the boolean type, not for types that are boolean-able.boolean
boolean
Tells if this value is comparable to another value of the same type usingcompare(T, T)
.boolean
isConvertibleTo
(Type<?> target) Tells if this value is convertible to the target type specified usingconvertTo(ch.tocco.nice2.types.api.Type<C>, T)
.boolean
boolean
boolean
This only returns true for numeric types, not for types that can be used like numbers (boolean).Returns the value so that future changes to the value won't write through to the returned value.boolean
boolean
boolean
boolean
Tells if the values of this type should be unique in their context.unsafe()
Casts this type toType<Object>
.void
Makes sure the value is in the given type and range.
-
Method Details
-
getName
String getName()The name of the type, for example "string", see https://wiki.tocco.ch/wiki/index.php/Nice2-types- Returns:
- The lower-case data type name.
-
isNumeric
boolean isNumeric()This only returns true for numeric types, not for types that can be used like numbers (boolean). STATUS EXPERIMENTAL: raffi does not see the necessity.- Returns:
- Tells if the type is numeric.
-
isChronological
boolean isChronological()- Returns:
- Tells if the type is date/time specific.
-
isBoolean
boolean isBoolean()This only returns true for the boolean type, not for types that are boolean-able. This is here to avoid the use of "boolean".equals(getName()). STATUS EXPERIMENTAL: raffi does not see the necessity.- Returns:
- Tells if the type is boolean.
-
shouldBeUnique
boolean shouldBeUnique()Tells if the values of this type should be unique in their context.Examples: identifier, counter.
Context can mean: in their entity (db table), together with the business unit.
-
getRepresentationClass
The class this type represents, for example java.lang.String. Virtual types return the class of the base type.- Returns:
- .
-
getEmptyValue
Returns what this type considers to be "empty". Examples are "" for String, 0 for Integer, false for Boolean.- Returns:
- Null if the type does not support it, for example for Date.
-
cast
-
unsafe
Casts this type toType<Object>
. -
asString
- Returns:
- null-in-null-out
-
asObject
Attempts to convert ("parse") the given string input into the data type.Example: if your type is 'integer' then an
Integer.valueOf(str)
is executed, and the result is returned on success, or a StringConversionException on failure.- Parameters:
str
- The string to parse into the data type. null-in-null-out.- Returns:
- The value as this type's data type. null-in-null-out.
- Throws:
StringConversionException
- If it does not fit.
-
isolate
Returns the value so that future changes to the value won't write through to the returned value.- Returns:
- The given object if immutable, or a defensive copy of the value, or null-in-null-out.
-
isVirtual
boolean isVirtual() -
getBaseType
-
getVirtualTypes
Collection<Type<T>> getVirtualTypes() -
validate
Makes sure the value is in the given type and range.null
is usually accepted. For example the type 'serial' doesn't just check for a number, but also for a non-negative number.- Throws:
ValidationException
- See Also:
-
isValid
- See Also:
-
convertTo
Converts the value of this type to the target type.- Parameters:
target
- The target type to which to convert to.value
- The value to convert.- Returns:
- The value in the target type, null-in-null-out.
- Throws:
TypeConversionException
- See Also:
-
isConvertibleTo
Tells if this value is convertible to the target type specified usingconvertTo(ch.tocco.nice2.types.api.Type<C>, T)
. Basically, this is a synonym togetRelationTo(target)!=Relation.UNRELATED
- Parameters:
target
- The target type to which to convert to.- See Also:
-
getRelationTo
-
isComparable
boolean isComparable()Tells if this value is comparable to another value of the same type usingcompare(T, T)
.- See Also:
-
compare
Compares two values of this type. For example the String class could use left.compareToIgnoreCase(right);- Returns:
- As usual. Two nulls are equal, otherwise null is considered "smaller". From the java docs: A negative integer, zero, or a positive integer as the right object is greater than, equal to, or less than the left object.
- Throws:
UnsupportedTypeOperationException
- if notisComparable()
.- See Also:
-
isEqual
-
isMatchable
boolean isMatchable() -
matches
-
contains
-
getSize
Deprecated.Replaced withgetRelationTo(Type)
TODO in bits? bytes? sometimes a configured 'max value' is returned, for example for 'string' where no fixed length is applicable. also see https://wiki.tocco.ch/wiki/index.php/Nice2-types- Returns:
- The storage size for Java.
-
getAdapter
Returns the type adapter as contributed in hive for this type.Example:
MyTypeAdapter mta = type.getAdapter(MyTypeAdapter.class);
Use this instead of
findAdapter(java.lang.Class<A>)
if you DO NOT want to fall back to the base type's adapter.- Returns:
null
if no such type adapter for that type was contributed.- See Also:
-
findAdapter
Returns the type adapter as contributed in hive for this type or its base type.Example:
MyTypeAdapter mta = type.findAdapter(MyTypeAdapter.class);
Use this instead of
getAdapter(java.lang.Class<A>)
if you want to fall back to the base type's adapter.- Returns:
null
if no such type adapter for that type nor its base type was contributed.- See Also:
-
getGenerationType
Type.GenerationType getGenerationType()- Returns:
- If and how values of this type are automatically generated
-
getRelationTo(Type)