Class WithMetadata

java.lang.Object
nz.org.riskscape.engine.types.WithMetadata
All Implemented Interfaces:
Type, WrappingType

public class WithMetadata extends Object implements WrappingType

Wrapping type that stores extra metadata along with a given type. For example, this lets you potentially store the expression that created the type (e.g. bookmark('foo')), along with the type itself (e.g. RelationType.

Multiple metadata objects can be associated with the underlying type. We assume that each metadata object is of a unique class, but this is not enforced at all.

  • Method Details

    • wrap

      public static Type wrap(Type given, Object... metadata)

      Creates a new WithMetadata that wraps the given type and associates the metadata with it

    • find

      public static <T> Optional<T> find(Type type, Class<T> metadataClass)
      Returns:
      metadata matching the class given, if any
    • getMetadata

      public <T> Optional<T> getMetadata(Class<T> target)
      Returns:
      metadata of the given target class, if any. If there are multiple metadata objects that match (e.g. Identified), then the first match found is returned
    • coerce

      public Object coerce(Object value) throws CoercionException
      Description copied from interface: Type

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

      Specified by:
      coerce in interface 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

      public Class<?> internalType()
      Description copied from interface: Type

      The underlying java class used to represent this type.

      Specified by:
      internalType in interface Type
    • toString

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getUnderlyingType

      public Type getUnderlyingType()
      Specified by:
      getUnderlyingType in interface WrappingType
      Returns:
      the type that we immediately wrap. Used by Type.getUnwrappedType() to descend through all wrapped Type to find the first unwrapped one.