Class StringDelegateCoder<T>
- Type Parameters:
T
- The type of objects coded.
- All Implemented Interfaces:
Serializable
Coder
that wraps a Coder<String>
and encodes/decodes values via string
representations.
To decode, the input byte stream is decoded to a String
, and this is passed to the
single-argument constructor for T
.
To encode, the input value is converted via toString()
, and this string is encoded.
In order for this to operate correctly for a class Clazz
, it must be the case for any
instance x
that x.equals(new Clazz(x.toString()))
.
This method of encoding is not designed for ease of evolution of Clazz
; it should only
be used in cases where the class is stable or the encoding is not important. If evolution of the
class is important, see
invalid reference
AvroCoder
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
StringDelegateCoder
(Class<T> clazz, TypeDescriptor<T> typeDescriptor) -
Method Summary
Modifier and TypeMethodDescriptiondecode
(InputStream inStream) Decodes a value of typeT
from the given input stream in the given context.decode
(InputStream inStream, Coder.Context context) Decodes a value of typeT
from the given input stream in the given context.void
encode
(T value, OutputStream outStream) Encodes the given value of typeT
onto the given output stream.void
encode
(T value, OutputStream outStream, Coder.Context context) Encodes the given value of typeT
onto the given output stream in the given context.boolean
Returns theTypeDescriptor
for the type encoded.int
hashCode()
static <T> StringDelegateCoder
<T> static <T> StringDelegateCoder
<T> of
(Class<T> clazz, TypeDescriptor<T> typeDescriptor) structuralValue
(T value) Returns an object with anObject.equals()
method that represents structural equality on the argument.toString()
void
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, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
StringDelegateCoder
-
-
Method Details
-
of
-
of
-
toString
-
equals
-
hashCode
public int hashCode() -
encode
Description copied from class:Coder
Encodes the given value of typeT
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 classCoder<T>
- Throws:
CoderException
- if the value could not be encoded for some reasonIOException
- if writing to theOutputStream
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 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 reasonIOException
- if writing to theOutputStream
fails for some reason
-
decode
Description copied from class:Coder
Decodes a value of typeT
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 classCoder<T>
- Throws:
CoderException
- if the value could not be decoded for some reasonIOException
- if reading from theInputStream
fails for some reason
-
decode
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 reasonIOException
- if reading from theInputStream
fails for some reason
-
verifyDeterministic
Description copied from class:CustomCoder
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
- aCustomCoder
is presumed nondeterministic.
- two values that compare as equal (via
-
structuralValue
Description copied from class:Coder
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>
-
getEncodedTypeDescriptor
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<T>
-