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 aT
will be converted to for coding.
- All Implemented Interfaces:
java.io.Serializable
public final class DelegateCoder<T,IntermediateT> extends CustomCoder<T>
ADelegateCoder<T, IntermediateT>
wraps aCoder
forIntermediateT
and encodes/decodes values of typeT
by converting to/fromIntermediateT
and then encoding/decoding using the underlyingCoder<IntermediateT>
.The conversions from
T
toIntermediateT
and vice versa must be supplied asDelegateCoder.CodingFunction
, a serializable function that may throw anyException
. If a thrown exception is an instance ofCoderException
orIOException
, it will be re-thrown, otherwise it will be wrapped as aCoderException
.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DelegateCoder.CodingFunction<InputT,OutputT>
ACodingFunction<InputT, OutputT>
is a serializable function fromInputT
toOutputT
that may throw anyException
.-
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
decode(java.io.InputStream inStream)
Decodes a value of typeT
from the given input stream in the given context.T
decode(java.io.InputStream inStream, Coder.Context context)
Decodes a value of typeT
from the given input stream in the given context.void
encode(T value, java.io.OutputStream outStream)
Encodes the given value of typeT
onto the given output stream.void
encode(T value, java.io.OutputStream outStream, Coder.Context context)
Encodes the given value of typeT
onto the given output stream in the given context.boolean
equals(@Nullable java.lang.Object o)
Coder<IntermediateT>
getCoder()
Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of thisDelegateCoder
.TypeDescriptor<T>
getEncodedTypeDescriptor()
Returns theTypeDescriptor
for the type encoded.int
hashCode()
static <T,IntermediateT>
DelegateCoder<T,IntermediateT>of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
static <T,IntermediateT>
DelegateCoder<T,IntermediateT>of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
java.lang.Object
structuralValue(T value)
Returns an object with anObject.equals()
method that represents structural equality on the argument.java.lang.String
toString()
void
verifyDeterministic()
ThrowCoder.NonDeterministicException
if the coding is not deterministic.-
Methods inherited from class org.apache.beam.sdk.coders.CustomCoder
getCoderArguments
-
Methods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
-
-
-
Constructor Detail
-
DelegateCoder
protected DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
-
-
Method Detail
-
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, java.io.OutputStream outStream) throws CoderException, java.io.IOException
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream.- Specified by:
encode
in classCoder<T>
- Throws:
CoderException
- if the value could not be encoded for some reasonjava.io.IOException
- if writing to theOutputStream
fails for some reason
-
encode
public void encode(T value, java.io.OutputStream outStream, Coder.Context context) throws CoderException, java.io.IOException
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream in the given context.- Overrides:
encode
in classCoder<T>
- Throws:
CoderException
- if the value could not be encoded for some reasonjava.io.IOException
- if writing to theOutputStream
fails for some reason
-
decode
public T decode(java.io.InputStream inStream) throws CoderException, java.io.IOException
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Specified by:
decode
in classCoder<T>
- Throws:
CoderException
- if the value could not be decoded for some reasonjava.io.IOException
- if reading from theInputStream
fails for some reason
-
decode
public T decode(java.io.InputStream inStream, Coder.Context context) throws CoderException, java.io.IOException
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Overrides:
decode
in classCoder<T>
- Throws:
CoderException
- if the value could not be decoded for some reasonjava.io.IOException
- if reading from theInputStream
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 thisDelegateCoder
.
-
verifyDeterministic
public void verifyDeterministic() throws Coder.NonDeterministicException
ThrowCoder.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()
orComparable.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 classCustomCoder<T>
- Throws:
Coder.NonDeterministicException
- when the underlying coder'sverifyDeterministic()
throws aCoder.NonDeterministicException
. For this to be safe, the intermediateCodingFunction<T, IntermediateT>
must also be deterministic.
- two values that compare as equal (via
-
structuralValue
public java.lang.Object structuralValue(T value)
Returns an object with anObject.equals()
method that represents structural equality on the argument.For any two values
x
andy
of typeT
, if their encoded bytes are the same, then it must be the case thatstructuralValue(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 anequals()
method, even if the input value isnull
.
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 abyte[]
, and returns an object that performs array equality on the encoded bytes.- Overrides:
structuralValue
in classCoder<T>
- Returns:
- a structural for a value of type
T
obtained by first converting toIntermediateT
and then obtaining a structural value according to the underlying coder.
-
equals
public boolean equals(@Nullable java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getEncodedTypeDescriptor
public TypeDescriptor<T> getEncodedTypeDescriptor()
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<T>
-
-