Class TypeImpl<T>

java.lang.Object
ch.tocco.nice2.types.impl.TypeImpl<T>
All Implemented Interfaces:
Type<T>

public class TypeImpl<T> extends Object implements Type<T>
  • Constructor Details

    • TypeImpl

      public TypeImpl(org.slf4j.Logger log, TypeHandler<T> handler, String name, Class<T> representationClass, int size, boolean comparable, boolean matchable)
    • TypeImpl

      public TypeImpl(org.slf4j.Logger log, TypeImpl<T> baseType, TypeHandler<T> handler, String name)
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • uncheckedCreate

      public static TypeImpl<?> uncheckedCreate(org.slf4j.Logger log, TypeHandler<?> handler, String name, Class<?> representationClass, int size, boolean comparable, boolean matchable)
    • uncheckedCreate

      public static TypeImpl<?> uncheckedCreate(org.slf4j.Logger log, TypeImpl<?> baseType, TypeHandler<?> handler, String name)
    • getName

      public String getName()
      Description copied from interface: Type
      The name of the type, for example "string", see https://wiki.tocco.ch/wiki/index.php/Nice2-types
      Specified by:
      getName in interface Type<T>
      Returns:
      The lower-case data type name.
    • isNumeric

      public boolean isNumeric()
      Description copied from interface: Type
      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.
      Specified by:
      isNumeric in interface Type<T>
      Returns:
      Tells if the type is numeric.
    • isChronological

      public boolean isChronological()
      Specified by:
      isChronological in interface Type<T>
      Returns:
      Tells if the type is date/time specific.
    • isBoolean

      public boolean isBoolean()
      Description copied from interface: Type
      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.
      Specified by:
      isBoolean in interface Type<T>
      Returns:
      Tells if the type is boolean.
    • shouldBeUnique

      public boolean shouldBeUnique()
      Description copied from interface: Type
      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.

      Specified by:
      shouldBeUnique in interface Type<T>
    • getRepresentationClass

      public Class<T> getRepresentationClass()
      Description copied from interface: Type
      The class this type represents, for example java.lang.String. Virtual types return the class of the base type.
      Specified by:
      getRepresentationClass in interface Type<T>
      Returns:
      .
    • getEmptyValue

      @Nullable public @Nullable Object getEmptyValue()
      Description copied from interface: Type
      Returns what this type considers to be "empty". Examples are "" for String, 0 for Integer, false for Boolean.
      Specified by:
      getEmptyValue in interface Type<T>
      Returns:
      Null if the type does not support it, for example for Date.
    • cast

      public <C> Type<C> cast(Class<C> representationClass) throws UnsupportedTypeOperationException
      Specified by:
      cast in interface Type<T>
      Throws:
      UnsupportedTypeOperationException
    • unsafe

      public Type<Object> unsafe()
      Description copied from interface: Type
      Casts this type to Type<Object>.
      Specified by:
      unsafe in interface Type<T>
    • asString

      public String asString(T value)
      Specified by:
      asString in interface Type<T>
      Returns:
      null-in-null-out
    • asObject

      @Nullable public T asObject(@Nullable @Nullable String str) throws StringConversionException
      Description copied from interface: Type
      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.

      Specified by:
      asObject in interface Type<T>
      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

      public T isolate(T value)
      Description copied from interface: Type
      Returns the value so that future changes to the value won't write through to the returned value.
      Specified by:
      isolate in interface Type<T>
      Returns:
      The given object if immutable, or a defensive copy of the value, or null-in-null-out.
    • isVirtual

      public boolean isVirtual()
      Specified by:
      isVirtual in interface Type<T>
    • getBaseType

      public Type<T> getBaseType()
      Specified by:
      getBaseType in interface Type<T>
    • getVirtualTypes

      public Collection<Type<T>> getVirtualTypes()
      Specified by:
      getVirtualTypes in interface Type<T>
    • validate

      public void validate(@Nullable T value) throws ValidationException
      Description copied from interface: Type
      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.
      Specified by:
      validate in interface Type<T>
      Throws:
      ValidationException
      See Also:
    • isValid

      public boolean isValid(T value)
      Specified by:
      isValid in interface Type<T>
      See Also:
    • convertTo

      @Nullable public <C> C convertTo(Type<C> target, @Nullable T value) throws TypeConversionException
      Description copied from interface: Type
      Converts the value of this type to the target type.
      Specified by:
      convertTo in interface Type<T>
      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

      public boolean isConvertibleTo(Type<?> target)
      Description copied from interface: Type
      Tells if this value is convertible to the target type specified using Type.convertTo(ch.tocco.nice2.types.api.Type<C>, T). Basically, this is a synonym to getRelationTo(target)!=Relation.UNRELATED
      Specified by:
      isConvertibleTo in interface Type<T>
      Parameters:
      target - The target type to which to convert to.
      See Also:
    • getRelationTo

      public Type.Relation getRelationTo(Type<?> target)
      Specified by:
      getRelationTo in interface Type<T>
    • isComparable

      public boolean isComparable()
      Description copied from interface: Type
      Tells if this value is comparable to another value of the same type using Type.compare(T, T).
      Specified by:
      isComparable in interface Type<T>
      See Also:
    • compare

      public int compare(@Nullable T left, @Nullable T right)
      Description copied from interface: Type
      Compares two values of this type. For example the String class could use left.compareToIgnoreCase(right);
      Specified by:
      compare in interface Type<T>
      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.
      See Also:
    • isEqual

      public boolean isEqual(@Nullable T left, @Nullable T right)
      Specified by:
      isEqual in interface Type<T>
    • isMatchable

      public boolean isMatchable()
      Specified by:
      isMatchable in interface Type<T>
    • matches

      public boolean matches(T value, String pattern)
      Specified by:
      matches in interface Type<T>
    • contains

      public boolean contains(T value, String pattern)
      Specified by:
      contains in interface Type<T>
    • getSize

      public int getSize()
      Description copied from interface: 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
      Specified by:
      getSize in interface Type<T>
      Returns:
      The storage size for Java.
    • getAdapter

      @Nullable public <A> A getAdapter(Class<A> adapterClass)
      Description copied from interface: Type
      Returns the type adapter as contributed in hive for this type.

      Example: MyTypeAdapter mta = type.getAdapter(MyTypeAdapter.class);

      Use this instead of Type.findAdapter(java.lang.Class<A>) if you DO NOT want to fall back to the base type's adapter.

      Specified by:
      getAdapter in interface Type<T>
      Returns:
      null if no such type adapter for that type was contributed.
      See Also:
    • findAdapter

      @Nullable public <A> A findAdapter(Class<A> adapterClass)
      Description copied from interface: Type
      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 Type.getAdapter(java.lang.Class<A>) if you want to fall back to the base type's adapter.

      Specified by:
      findAdapter in interface Type<T>
      Returns:
      null if no such type adapter for that type nor its base type was contributed.
      See Also:
    • initHandler

      public void initHandler()
    • addVirtual

      public void addVirtual(TypeImpl<?> virtual)
    • addConversion

      public <U> boolean addConversion(Type<U> target, Converter<T,U> converter, Type.Relation relation)
    • removeConversion

      public <U> boolean removeConversion(Type<U> target)
    • addAdapter

      public boolean addAdapter(Class<?> adapterClass, Object adapter)
      Returns:
      False if there is already an adapter object for that class set.
    • getGenerationType

      public Type.GenerationType getGenerationType()
      Specified by:
      getGenerationType in interface Type<T>
      Returns:
      If and how values of this type are automatically generated
    • equals

      public boolean equals(Object o)
      Do not call equals() to compare types, rather compare their names: type1.getName().equals( type2.getName() )
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object