Class ReferenceEncoding

All Implemented Interfaces:
Encoding<ObjId>, NaturalSortAware, Serializable, Comparator<ObjId>

public class ReferenceEncoding extends NullSafeEncoding<ObjId>
The Encoding for ReferenceFields. Instances support object type restriction.

Binary encoding uses the value from ObjId.getBytes(), or 0xff to represent null.

Null values are supported by this class.

See Also:
  • Constructor Details

    • ReferenceEncoding

      public ReferenceEncoding()
      Constructor.

      No restrictions will be placed on encoded references.

    • ReferenceEncoding

      public ReferenceEncoding(Schema schema, Set<ObjType> objectTypes)
      Constructor.
      Parameters:
      schema - associated schema
      objectTypes - allowed object type storage IDs, or null for no restriction
      Throws:
      IllegalArgumentException - if schema is null
  • Method Details

    • getObjectTypes

      public Set<ObjType> getObjectTypes()
      Get the object types this encoding is allowed to reference, if so restricted.
      Returns:
      allowed object types, or null if there is no restriction
    • write

      public void write(ByteWriter writer, ObjId id)
      Description copied from interface: Encoding
      Write a value to the given output.
      Specified by:
      write in interface Encoding<ObjId>
      Overrides:
      write in class NullSafeEncoding<ObjId>
      Parameters:
      writer - byte output
      id - value to write (possibly null)
    • validate

      public ObjId validate(Object obj)
      Description copied from interface: Encoding
      Verify the given object is a valid instance of this Encoding's Java type and cast it to that type.

      Note that this method must throw IllegalArgumentException, not ClassCastException or NullPointerException, if obj does not have the correct type, or is an unsupported value - including null if null is not supported.

      This method is allowed to perform widening conversions of the object that lose no information, e.g., from Integer to Long.

      The implementation in Encoding first verifies the value is not null if this instance does not allow null values, and then attempts to cast the value using this instance's raw Java type. Subclasses should override this method to implement any other restrictions.

      Parameters:
      obj - object to validate
      Returns:
      obj cast to this encoding's type
    • checkAllowed

      public ObjId checkAllowed(ObjId id)
      Verify the reference target is permitted by this instance.
      Parameters:
      id - reference value
      Returns:
      id after successful validation
      Throws:
      InvalidReferenceException - if id has a storage ID that is not allowed by this instance
    • convert

      public <S> ObjId convert(Encoding<S> type, S value)
      Attempt to convert a value from the given Encoding into a value of this Encoding.

      The only conversion supported by ReferenceEncoding is to/from ObjId.

      Specified by:
      convert in interface Encoding<ObjId>
      Overrides:
      convert in class NullSafeEncoding<ObjId>
      Type Parameters:
      S - source encoding
      Parameters:
      type - the Encoding of value
      value - the value to convert
      Returns:
      value converted to this instance's type
    • toString

      public String toString()
      Overrides:
      toString in class AbstractEncoding<ObjId>
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Comparator<ObjId>
      Overrides:
      equals in class NullSafeEncoding<ObjId>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class NullSafeEncoding<ObjId>