Class DelegateCoder<T,IntermediateT>
- Type Parameters:
T- The type of objects coded by this Coder.IntermediateT- The type of objects aTwill be converted to for coding.
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceACodingFunction<InputT, OutputT>is a serializable function fromInputTtoOutputTthat may throw anyException.Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T, IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT, T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor) -
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.decode(InputStream inStream, Coder.Context context) Decodes a value of typeTfrom the given input stream in the given context.voidencode(T value, OutputStream outStream) Encodes the given value of typeTonto the given output stream.voidencode(T value, OutputStream outStream, Coder.Context context) Encodes the given value of typeTonto the given output stream in the given context.booleangetCoder()Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of thisDelegateCoder.Returns theTypeDescriptorfor the type encoded.inthashCode()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) structuralValue(T value) Returns an object with anObject.equals()method that represents structural equality on the argument.toString()voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.CustomCoder
getCoderArgumentsMethods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
DelegateCoder
protected DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T, IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT, T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
-
-
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
Description copied from class:CoderEncodes the given value of typeTonto 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:
encodein classCoder<T>- Throws:
CoderException- if the value could not be encoded for some reasonIOException- if writing to theOutputStreamfails for some reason
-
encode
public void encode(T value, OutputStream outStream, Coder.Context context) throws CoderException, IOException Description copied from class:CoderEncodes the given value of typeTonto the given output stream in the given context.- Overrides:
encodein classCoder<T>- Throws:
CoderException- if the value could not be encoded for some reasonIOException- if writing to theOutputStreamfails for some reason
-
decode
Description copied from class:CoderDecodes a value of typeTfrom 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:
decodein classCoder<T>- Throws:
CoderException- if the value could not be decoded for some reasonIOException- if reading from theInputStreamfails for some reason
-
decode
Description copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value.- Overrides:
decodein classCoder<T>- Throws:
CoderException- if the value could not be decoded for some reasonIOException- if reading from theInputStreamfails for some reason
-
getCoder
Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of thisDelegateCoder. -
verifyDeterministic
ThrowCoder.NonDeterministicExceptionif the coding is not deterministic.In order for a
Coderto 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
Coderalways produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Overrides:
verifyDeterministicin 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
Returns an object with anObject.equals()method that represents structural equality on the argument.For any two values
xandyof 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
nullshould 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:
structuralValuein classCoder<T>- Returns:
- a structural for a value of type
Tobtained by first converting toIntermediateTand then obtaining a structural value according to the underlying coder.
-
equals
-
hashCode
public int hashCode() -
toString
-
getEncodedTypeDescriptor
Description copied from class:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
getEncodedTypeDescriptorin classCoder<T>
-