Interface Type

All Known Subinterfaces:
ContainingType, Rangeable, WrappingType
All Known Implementing Classes:
Anything, Bool, CanonicalType, CoverageType, Date, Decimal, EmptyList, Enumeration, Floating, FunctionType, Geom, GeomType, GeomType.Line, GeomType.Point, GeomType.Polygon, IdentifiedType, Integer, LambdaType, LinkedType, LookupTableType, MultiGeom, Nothing, Nullable, OfUnit, Referenced, RelationType, RSList, ScopedLambdaType, SimpleType, Struct, Text, WithinRange, WithinSet, WithMetadata

public interface Type

The root interface for Riskscape's type system. Each instance of type offers a coerce method for checking that a given object corresponds to the type.

  • Method Details

    • coerce

      Object coerce(Object value) throws CoercionException

      Attempt to convert the given value in to one represented by this type.

      Parameters:
      value - The value to coerce
      Returns:
      A value that conforms to this type
      Throws:
      CoercionException - if the given object can not be converted to something represented by this type
    • internalType

      Class<?> internalType()

      The underlying java class used to represent this type.

    • isNumeric

      default boolean isNumeric()

      Indicates if this Type is numeric. E.g Integer, Floating or Decimal.

      Returns:
      true is this Type is a numeric type
    • checkForNull

      default void checkForNull(Object value)
    • getUnwrappedType

      default Type getUnwrappedType()
      Returns:
      the first type that actually holds the value (and is not just adding type metadata)
    • find

      default <T extends Type> Optional<T> find(Class<T> type)

      Type-safe way to find a particular Type by recursing through this type and any types that it wraps. This doesn't (and shouldn't) consider types with member types, such as structs and lists - this is meant as a more sound method of doing instanceof, considering WrappingTypes

    • findAllowNull

      default <T extends Type> Optional<T> findAllowNull(Class<T> type)

      Convenience for Nullable.strip(this).find(type)

    • isA

      default <T extends Type> boolean isA(Class<T> type)
    • isWrapped

      default boolean isWrapped()
      Returns:
      true if this type is a wrapping type
    • asStruct

      Struct asStruct()
    • estimateSize

      int estimateSize(Object entry)

      Estimate the number of bytes used to serialize the given value to an output stream.

      Parameters:
      entry - the value of this type to measure. Must be of internalType() or a ClassCastException is going to happen
      Returns:
      the estimated number of bytes, or zero if no estimation was possible.
    • toBytes

      void toBytes(DataOutputStream os, Object toWrite) throws IOException
      Throws:
      IOException
    • fromBytes

      Object fromBytes(DataInputStream in) throws IOException
      Throws:
      IOException
    • isNullable

      default boolean isNullable()
      Returns:
      true if this type can be set to nothing TODO this might need to be final and do an instanceof check somewhere - we always want nullable to be on the outside
    • toString

      default String toString(Object value)

      A Riskscape specific toString of the given value, as an alternative to the java type's native toString representation.

    • unwrap

      default List<Type> unwrap()
      Returns:
      a List of all the types that are wrapped by this type, including this one, in depth-first-search order. This will mean the simple unwrapped type is the first element of the list.
    • visit

      <T, U> U visit(TypeVisitor<T,U> tv, T data)

      Type specific visiting logic. Types must decide whether they are atomic or compound and call the relevant method on TypeVisitor. It's up to each compound type to decide how they want to structure their children, e.g. order, metadata, and what's considered a child or not.