Class DelegateCoder<T,IntermediateT>

java.lang.Object
org.apache.beam.sdk.coders.Coder<T>
org.apache.beam.sdk.coders.CustomCoder<T>
org.apache.beam.sdk.coders.DelegateCoder<T,IntermediateT>
Type Parameters:
T - The type of objects coded by this Coder.
IntermediateT - The type of objects a T will be converted to for coding.
All Implemented Interfaces:
Serializable

public final class DelegateCoder<T,IntermediateT> extends CustomCoder<T>
A DelegateCoder<T, IntermediateT> wraps a Coder for IntermediateT and encodes/decodes values of type T by converting to/from IntermediateT and then encoding/decoding using the underlying Coder<IntermediateT>.

The conversions from T to IntermediateT and vice versa must be supplied as DelegateCoder.CodingFunction, a serializable function that may throw any Exception. If a thrown exception is an instance of CoderException or IOException, it will be re-thrown, otherwise it will be wrapped as a CoderException.

See Also:
  • Constructor Details

  • Method Details

    • of

      public static <T, IntermediateT> DelegateCoder<T,IntermediateT> of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
    • of

      public static <T, IntermediateT> DelegateCoder<T,IntermediateT> of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
    • encode

      public void encode(T value, OutputStream outStream) throws CoderException, IOException
      Description copied from class: Coder
      Encodes the given value of type T onto the given output stream. Multiple elements can be encoded next to each other on the output stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.
      Specified by:
      encode in class Coder<T>
      Throws:
      CoderException - if the value could not be encoded for some reason
      IOException - if writing to the OutputStream fails for some reason
    • encode

      public void encode(T value, OutputStream outStream, Coder.Context context) throws CoderException, IOException
      Description copied from class: Coder
      Encodes the given value of type T onto the given output stream in the given context.
      Overrides:
      encode in class Coder<T>
      Throws:
      CoderException - if the value could not be encoded for some reason
      IOException - if writing to the OutputStream fails for some reason
    • decode

      public T decode(InputStream inStream) throws CoderException, IOException
      Description copied from class: Coder
      Decodes a value of type T from the given input stream in the given context. Returns the decoded value. Multiple elements can be encoded next to each other on the input stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.
      Specified by:
      decode in class Coder<T>
      Throws:
      CoderException - if the value could not be decoded for some reason
      IOException - if reading from the InputStream fails for some reason
    • decode

      public T decode(InputStream inStream, Coder.Context context) throws CoderException, IOException
      Description copied from class: Coder
      Decodes a value of type T from the given input stream in the given context. Returns the decoded value.
      Overrides:
      decode in class Coder<T>
      Throws:
      CoderException - if the value could not be decoded for some reason
      IOException - if reading from the InputStream fails for some reason
    • getCoder

      public Coder<IntermediateT> getCoder()
      Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of this DelegateCoder.
    • verifyDeterministic

      public void verifyDeterministic() throws Coder.NonDeterministicException
      Throw Coder.NonDeterministicException if the coding is not deterministic.

      In order for a Coder to be considered deterministic, the following must be true:

      • two values that compare as equal (via Object.equals() or Comparable.compareTo(), if supported) have the same encoding.
      • the Coder always produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
      Overrides:
      verifyDeterministic in class CustomCoder<T>
      Throws:
      Coder.NonDeterministicException - when the underlying coder's verifyDeterministic() throws a Coder.NonDeterministicException. For this to be safe, the intermediate CodingFunction<T, IntermediateT> must also be deterministic.
    • structuralValue

      public Object structuralValue(T value)
      Returns an object with an Object.equals() method that represents structural equality on the argument.

      For any two values x and y of type T, if their encoded bytes are the same, then it must be the case that structuralValue(x).equals(structuralValue(y)).

      Most notably:

      • The structural value for an array coder should perform a structural comparison of the contents of the arrays, rather than the default behavior of comparing according to object identity.
      • The structural value for a coder accepting null should be a proper object with an equals() method, even if the input value is null.

      See also Coder.consistentWithEquals().

      By default, if this coder is Coder.consistentWithEquals(), and the value is not null, returns the provided object. Otherwise, encodes the value into a byte[], and returns an object that performs array equality on the encoded bytes.

      Overrides:
      structuralValue in class Coder<T>
      Returns:
      a structural for a value of type T obtained by first converting to IntermediateT and then obtaining a structural value according to the underlying coder.
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

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

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

      public TypeDescriptor<T> getEncodedTypeDescriptor()
      Description copied from class: Coder
      Returns the TypeDescriptor for the type encoded.
      Overrides:
      getEncodedTypeDescriptor in class Coder<T>