com.google.cloud.dataflow.sdk.coders

## Class DelegateCoder<T,IntermediateT>

- java.lang.Object
- - com.google.cloud.dataflow.sdk.coders.StandardCoder<T>
  - - com.google.cloud.dataflow.sdk.coders.DeterministicStandardCoder<T>
    - - com.google.cloud.dataflow.sdk.coders.AtomicCoder<T>
      - - com.google.cloud.dataflow.sdk.coders.CustomCoder<T>
        - - com.google.cloud.dataflow.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:
  Coder<T>, Serializable

Direct Known Subclasses:
  StringDelegateCoder

    public 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:
  Serialized Form

### Nested Class Summary

Nested Classes
`static interface` `DelegateCoder.CodingFunction<InputT,OutputT>` A `CodingFunction<InputT, OutputT>` is a serializable function from `InputT` to `OutputT` that may throw any `Exception`.

### Nested classes/interfaces inherited from interface com.google.cloud.dataflow.sdk.coders.Coder

`Coder.Context, Coder.NonDeterministicException`

### Constructor Summary

Constructors
`protected` `DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`T` `decode(InputStream inStream, Coder.Context context)` Decodes a value of type `T` from the given input stream in the given context.
`void` `encode(T value, OutputStream outStream, Coder.Context context)` Encodes the given value of type `T` onto the given output stream in the given context.
`Collection<String>` `getAllowedEncodings()` A collection of encodings supported by `Coder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)` in addition to the encoding from `Coder.getEncodingId()` (which is assumed supported).
`Coder<IntermediateT>` `getCoder()` Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of this `DelegateCoder`.
`String` `getEncodingId()` An identifier for the binary format written by `Coder.encode(T, java.io.OutputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)`.
`static <T,IntermediateT> DelegateCoder<T,IntermediateT>` `of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)`
`Object` `structuralValue(T value)` Returns an object with an `Object.equals()` method that represents structural equality on the argument.
`String` `toString()`
`void` `verifyDeterministic()` Throw `Coder.NonDeterministicException` if the coding is not deterministic.

### Methods inherited from class com.google.cloud.dataflow.sdk.coders.CustomCoder

`asCloudObject, of`

### Methods inherited from class com.google.cloud.dataflow.sdk.coders.AtomicCoder

`getCoderArguments, getInstanceComponents`

### Methods inherited from class com.google.cloud.dataflow.sdk.coders.StandardCoder

`consistentWithEquals, equals, getComponents, getEncodedElementByteSize, hashCode, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### DelegateCoder

    protected DelegateCoder(Coder<IntermediateT> coder,
                            DelegateCoder.CodingFunction<T,IntermediateT> toFn,
                            DelegateCoder.CodingFunction<IntermediateT,T> fromFn)

### Method Detail

#### of

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

#### encode

    public void encode(T value,
                       OutputStream outStream,
                       Coder.Context context)
                throws CoderException,
                       IOException

Description copied from interface: `Coder`
Encodes the given value of type `T` onto the given output stream in the given context.

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,
                    Coder.Context context)
             throws CoderException,
                    IOException

Description copied from interface: `Coder`
Decodes a value of type `T` from the given input stream in the given context. Returns the decoded value.

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.

Specified by:
  `verifyDeterministic` in interface `Coder<T>`
Overrides:
  `verifyDeterministic` in class `CustomCoder<T>`
Throws:
  `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.
  `Coder.NonDeterministicException` - if this coder is not deterministic.

#### structuralValue

    public Object structuralValue(T value)
                           throws Exception

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(@code 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()`.

Specified by:
  `structuralValue` in interface `Coder<T>`
Overrides:
  `structuralValue` in class `StandardCoder<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.
Throws:
  `Exception`

#### toString

    public String toString()

Overrides:
  `toString` in class `StandardCoder<T>`

#### getEncodingId

    public String getEncodingId()

An identifier for the binary format written by `Coder.encode(T, java.io.OutputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)`.

This value, along with the fully qualified class name, forms an identifier for the binary format of this coder. Whenever this value changes, the new encoding is considered incompatible with the prior format: It is presumed that the prior version of the coder will be unable to correctly read the new format and the new version of the coder will be unable to correctly read the old format.

If the format is changed in a backwards-compatible way (the Coder can still accept data from the prior format), such as by adding optional fields to a Protocol Buffer or Avro definition, and you want Dataflow to understand that the new coder is compatible with the prior coder, this value must remain unchanged. It is then the responsibility of `Coder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)` to correctly read data from the prior format.

Specified by:
  `getEncodingId` in interface `Coder<T>`
Overrides:
  `getEncodingId` in class `CustomCoder<T>`
Returns:
  a `String` composed from the underlying coder class name and its encoding id. Note that this omits any description of the coding functions. These should be modified with care.

#### getAllowedEncodings

    public Collection<String> getAllowedEncodings()

A collection of encodings supported by `Coder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)` in addition to the encoding from `Coder.getEncodingId()` (which is assumed supported).

This information is not currently used for any purpose. It is descriptive only, and this method is subject to change.

Specified by:
  `getAllowedEncodings` in interface `Coder<T>`
Overrides:
  `getAllowedEncodings` in class `StandardCoder<T>`
Returns:
  allowed encodings which are composed from the underlying coder class and its allowed encoding ids. Note that this omits any description of the coding functions. These should be modified with care.
See Also:
  `Coder.getEncodingId()`