Interface Type<T>

All Known Implementing Classes:
TypeImpl

public interface Type<T>
  • 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

      Class<T> getRepresentationClass()
      The class this type represents, for example java.lang.String. Virtual types return the class of the base type.
      Returns:
      .
    • getEmptyValue

      @Nullable @Nullable Object 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

      <C> Type<C> cast(Class<C> representationClass) throws UnsupportedTypeOperationException
      Throws:
      UnsupportedTypeOperationException
    • unsafe

      Type<Object> unsafe()
      Casts this type to Type<Object>.
    • asString

      String asString(T value)
      Returns:
      null-in-null-out
    • asObject

      @Nullable T asObject(@Nullable @Nullable String str) throws StringConversionException
      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

      @Nullable T isolate(@Nullable T value)
      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

      Type<T> getBaseType()
    • getVirtualTypes

      Collection<Type<T>> getVirtualTypes()
    • validate

      void validate(@Nullable T value) throws ValidationException
      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

      boolean isValid(@Nullable T value)
      See Also:
    • convertTo

      @Nullable <C> C convertTo(Type<C> target, @Nullable T value) throws TypeConversionException
      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

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

      Type.Relation getRelationTo(Type<?> target)
    • isComparable

      boolean isComparable()
      Tells if this value is comparable to another value of the same type using compare(T, T).
      See Also:
    • compare

      int compare(@Nullable T left, @Nullable T right) throws UnsupportedTypeOperationException
      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 not isComparable().
      See Also:
    • isEqual

      boolean isEqual(@Nullable T left, @Nullable T right)
    • isMatchable

      boolean isMatchable()
    • matches

      boolean matches(@Nullable T value, String pattern)
    • contains

      boolean contains(@Nullable T value, String pattern)
    • getSize

      @Deprecated int getSize()
      Deprecated.
      Replaced with getRelationTo(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

      @Nullable <A> A getAdapter(Class<A> adapterClass)
      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

      @Nullable <A> A findAdapter(Class<A> adapterClass)
      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