Class Struct

java.lang.Object
nz.org.riskscape.engine.types.Struct
All Implemented Interfaces:
Type

public class Struct extends Object implements Type

Permissive struct-like type that allows a c-like struct (like a java class with only fields) to be declared with members being other riskscape types.

Struct should be constructed using the builder. eg:

java Struct.of("name", Types.TEXT).and("value", Types.INTEGER).build();

  • Field Details

    • INTERNAL_TYPE

      public static final Class<?> INTERNAL_TYPE
    • TYPE_INFORMATION

      public static final TypeInformation TYPE_INFORMATION
    • EMPTY_STRUCT

      public static final Struct EMPTY_STRUCT
  • Method Details

    • of

      public static Struct of()

      Create an empty struct

    • of

      public static Struct of(String key, Type type)

      Define a struct, starting with the given key and type

      Returns:
      a builder to use to define the struct.
    • of

      public static Struct of(String k0, Type t0, String k1, Type t1)
    • of

      public static Struct of(String k0, Type t0, String k1, Type t1, String k2, Type t2)
    • of

      public static Struct of(String k0, Type t0, String k1, Type t1, String k2, Type t2, String k3, Type t3)
    • of

      public static Struct of(String k0, Type t0, String k1, Type t1, String k2, Type t2, String k3, Type t3, String k4, Type t4)
    • builder

      public static Struct.StructBuilder builder()
    • flattenMemberKeys

      public static List<String> flattenMemberKeys(Struct struct)
      Returns:
      a list of member keys in DFS order, where children have their key appended to their parent's with a dot between them, so they are in the style a user would enter in an expression, e.g. foo.bar
    • getMemberKeys

      public List<String> getMemberKeys()
      Returns:
      a List of the names/keys of all the members in this struct, sorted alphabetically.
    • getMembers

      public List<Struct.StructMember> getMembers()
    • coerce

      public Object coerce(Object value)
      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
    • internalType

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

      The underlying java class used to represent this type.

      Specified by:
      internalType in interface Type
    • build

      @Deprecated public Struct build()
      Deprecated.

      Compatability shim to cope with API change to Struct.of/Struct.and

    • toString

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

      public Optional<Struct.StructMember> getMember(String key)
      Returns:
      a StructMember identified by the given key, or Optional.empty() if non exists
    • getEntry

      public Struct.StructMember getEntry(String key)

      null-returning variation of getMember TODO consider making this package level or change it's name or something

    • parent

      public Struct parent(String asKey)
    • add

      public Struct add(String asKey, Type otherType)

      Returns this with the extra member added on TODO using some kind of optimized copying immutable map might cut down on garbage collection etc (if guava isn't already doing that)

    • replace

      public Struct replace(String key, Type newType)
      Returns:
      a new Struct with the given member replaced with one of a new type.
    • and

      public Struct and(Struct struct)

      Synonym of add(String, Type) to support change in builder API

    • and

      public Struct and(String newKey, Type newType)

      Synonym of add(String, Type) to support change in builder API

    • asStruct

      public Struct asStruct()
      Specified by:
      asStruct in interface Type
    • size

      public int size()
    • contains

      public boolean contains(Struct.StructMember entry)
    • isUnion

      public boolean isUnion(Struct rhs)
      Returns:
      true if the two structs have the same ordered list of member types - e.g. their tuples could be combined in a union
    • isEquivalent

      public boolean isEquivalent(Struct rhs)
      Returns:
      true if the two structs have the same members, considering only the keys and the types.
    • isSupersetOf

      public boolean isSupersetOf(Struct rhs)
      Returns:
      true if this struct is a superset, that is contains at least the same members as rhs.
    • hasMember

      public boolean hasMember(String key)
    • hashCode

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

      public boolean equals(Object rhs)
      Overrides:
      equals in class Object
    • estimateSize

      public int estimateSize(Object entry)
      Description copied from interface: Type

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

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

      public void toBytes(DataOutputStream os, Object toWrite) throws IOException
      Specified by:
      toBytes in interface Type
      Throws:
      IOException
    • fromBytes

      public Object fromBytes(DataInputStream in) throws IOException
      Specified by:
      fromBytes in interface Type
      Throws:
      IOException
    • addOrReplace

      public Struct addOrReplace(String key, Type type)
    • visit

      public <T, U> U visit(TypeVisitor<T,U> tv, T data)
      Description copied from interface: Type

      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.

      Specified by:
      visit in interface Type