T
- the type of elements handled by this coderpublic class SerializableCoder<T extends java.io.Serializable> extends CustomCoder<T>
Coder
for Java classes that implement Serializable
.
SerializableCoder
should be used only for objects that have proper Object.equals(java.lang.Object)
and Object.hashCode()
implementations.
To use, specify the coder type on a PCollection:
PCollection<MyRecord> records =
foo.apply(...).setCoder(SerializableCoder.of(MyRecord.class));
SerializableCoder
does not guarantee a deterministic encoding, as Java serialization
may produce different binary encodings for two equivalent objects.
Modifier and Type | Class and Description |
---|---|
static class |
SerializableCoder.SerializableCoderProviderRegistrar
A
CoderProviderRegistrar which registers a CoderProvider which can handle
serializable types. |
Coder.Context, Coder.NonDeterministicException
Modifier | Constructor and Description |
---|---|
protected |
SerializableCoder(java.lang.Class<T> type,
TypeDescriptor<T> typeDescriptor) |
Modifier and Type | Method and Description |
---|---|
boolean |
consistentWithEquals()
|
T |
decode(java.io.InputStream inStream)
Decodes a value of type
T from the given input stream in the given context. |
void |
encode(T value,
java.io.OutputStream outStream)
Encodes the given value of type
T onto the given output stream. |
boolean |
equals(@Nullable java.lang.Object other) |
static CoderProvider |
getCoderProvider()
Returns a
CoderProvider which uses the SerializableCoder if possible for all
types. |
TypeDescriptor<T> |
getEncodedTypeDescriptor()
Returns the
TypeDescriptor for the type encoded. |
java.lang.Class<T> |
getRecordType() |
int |
hashCode() |
static <T extends java.io.Serializable> |
of(java.lang.Class<T> clazz)
Returns a
SerializableCoder instance for the provided element class. |
static <T extends java.io.Serializable> |
of(TypeDescriptor<T> type)
Returns a
SerializableCoder instance for the provided element type. |
java.lang.Object |
structuralValue(T value)
The structural value of the object is the object itself.
|
java.lang.String |
toString() |
void |
verifyDeterministic()
Throw
Coder.NonDeterministicException if the coding is not deterministic. |
getCoderArguments
decode, encode, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
protected SerializableCoder(java.lang.Class<T> type, TypeDescriptor<T> typeDescriptor)
public static <T extends java.io.Serializable> SerializableCoder<T> of(TypeDescriptor<T> type)
SerializableCoder
instance for the provided element type.T
- the element typepublic boolean consistentWithEquals()
Coder
true
if this Coder
is injective with respect to Object.equals(java.lang.Object)
.
Whenever the encoded bytes of two values are equal, then the original values are equal
according to Objects.equals()
. Note that this is well-defined for null
.
This condition is most notably false for arrays. More generally, this condition is false
whenever equals()
compares object identity, rather than performing a
semantic/structural comparison.
By default, returns false.
consistentWithEquals
in class Coder<T extends java.io.Serializable>
public java.lang.Object structuralValue(T value)
SerializableCoder
should
be only used for objects with a proper Object.equals(java.lang.Object)
implementation.structuralValue
in class Coder<T extends java.io.Serializable>
public static <T extends java.io.Serializable> SerializableCoder<T> of(java.lang.Class<T> clazz)
SerializableCoder
instance for the provided element class.T
- the element typepublic static CoderProvider getCoderProvider()
CoderProvider
which uses the SerializableCoder
if possible for all
types.
This method is invoked reflectively from DefaultCoder
.
public java.lang.Class<T> getRecordType()
public void encode(T value, java.io.OutputStream outStream) throws java.io.IOException
Coder
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.encode
in class Coder<T extends java.io.Serializable>
java.io.IOException
- if writing to the OutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reasonpublic T decode(java.io.InputStream inStream) throws java.io.IOException, CoderException
Coder
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.decode
in class Coder<T extends java.io.Serializable>
java.io.IOException
- if reading from the InputStream
fails for some reasonCoderException
- if the value could not be decoded for some reasonpublic void verifyDeterministic() throws Coder.NonDeterministicException
Coder.NonDeterministicException
if the coding is not deterministic.
In order for a Coder
to be considered deterministic, the following must be true:
Object.equals()
or Comparable.compareTo()
, if supported) have the same encoding.
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.
verifyDeterministic
in class CustomCoder<T extends java.io.Serializable>
NonDeterministicException
- always. Java serialization is not deterministic with respect
to Object.equals(java.lang.Object)
for all types.Coder.NonDeterministicException
- if this coder is not deterministic.public boolean equals(@Nullable java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public TypeDescriptor<T> getEncodedTypeDescriptor()
Coder
TypeDescriptor
for the type encoded.getEncodedTypeDescriptor
in class Coder<T extends java.io.Serializable>
public java.lang.String toString()
toString
in class java.lang.Object