T
- The type of objects coded.public final class StringDelegateCoder<T> extends CustomCoder<T>
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 AvroCoder
or any other evolution safe encoding.
Coder.Context, Coder.NonDeterministicException
Modifier | Constructor and Description |
---|---|
protected |
StringDelegateCoder(java.lang.Class<T> clazz,
TypeDescriptor<T> typeDescriptor) |
Modifier and Type | Method and Description |
---|---|
T |
decode(java.io.InputStream inStream)
Decodes a value of type
T from the given input stream in the given context. |
T |
decode(java.io.InputStream inStream,
Coder.Context context)
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. |
void |
encode(T value,
java.io.OutputStream outStream,
Coder.Context context)
Encodes the given value of type
T onto the given output stream in the given context. |
boolean |
equals(@Nullable java.lang.Object o) |
TypeDescriptor<T> |
getEncodedTypeDescriptor()
Returns the
TypeDescriptor for the type encoded. |
int |
hashCode() |
static <T> StringDelegateCoder<T> |
of(java.lang.Class<T> clazz) |
static <T> StringDelegateCoder<T> |
of(java.lang.Class<T> clazz,
TypeDescriptor<T> typeDescriptor) |
java.lang.Object |
structuralValue(T value)
Returns an object with an
Object.equals() method that represents structural equality on
the argument. |
java.lang.String |
toString() |
void |
verifyDeterministic()
Throw
Coder.NonDeterministicException if the coding is not deterministic. |
getCoderArguments
consistentWithEquals, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
protected StringDelegateCoder(java.lang.Class<T> clazz, TypeDescriptor<T> typeDescriptor)
public static <T> StringDelegateCoder<T> of(java.lang.Class<T> clazz)
public static <T> StringDelegateCoder<T> of(java.lang.Class<T> clazz, TypeDescriptor<T> typeDescriptor)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(@Nullable java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public void encode(T value, java.io.OutputStream outStream) throws CoderException, 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>
CoderException
- if the value could not be encoded for some reasonjava.io.IOException
- if writing to the OutputStream
fails for some reasonpublic void encode(T value, java.io.OutputStream outStream, Coder.Context context) throws CoderException, java.io.IOException
Coder
T
onto the given output stream in the given context.encode
in class Coder<T>
CoderException
- if the value could not be encoded for some reasonjava.io.IOException
- if writing to the OutputStream
fails for some reasonpublic T decode(java.io.InputStream inStream) throws CoderException, java.io.IOException
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>
CoderException
- if the value could not be decoded for some reasonjava.io.IOException
- if reading from the InputStream
fails for some reasonpublic T decode(java.io.InputStream inStream, Coder.Context context) throws CoderException, java.io.IOException
Coder
T
from the given input stream in the given context. Returns the
decoded value.decode
in class Coder<T>
CoderException
- if the value could not be decoded for some reasonjava.io.IOException
- if reading from the InputStream
fails for some reasonpublic void verifyDeterministic() throws Coder.NonDeterministicException
CustomCoder
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>
Coder.NonDeterministicException
- if this coder is not deterministic.public java.lang.Object structuralValue(T value)
Coder
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(structuralValue(y))
.
Most notably:
null
should be a proper object with an
equals()
method, even if the input value is null
.
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 a byte[]
, and returns an
object that performs array equality on the encoded bytes.
structuralValue
in class Coder<T>
public TypeDescriptor<T> getEncodedTypeDescriptor()
Coder
TypeDescriptor
for the type encoded.getEncodedTypeDescriptor
in class Coder<T>