T
- The type of objects coded by this Coder.IntermediateT
- The type of objects a T
will be converted to for coding.public final class DelegateCoder<T,IntermediateT> extends CustomCoder<T>
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
.
Modifier and Type | Class and Description |
---|---|
static interface |
DelegateCoder.CodingFunction<InputT,OutputT>
A
CodingFunction<InputT, OutputT> is a serializable
function from InputT to OutputT that may throw any Exception . |
Coder.Context, Coder.NonDeterministicException
Modifier | Constructor and Description |
---|---|
protected |
DelegateCoder(Coder<IntermediateT> coder,
DelegateCoder.CodingFunction<T,IntermediateT> toFn,
DelegateCoder.CodingFunction<IntermediateT,T> fromFn,
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(java.lang.Object o) |
Coder<IntermediateT> |
getCoder()
Returns the coder used to encode/decode the intermediate values produced/consumed by the
coding functions of this
DelegateCoder . |
TypeDescriptor<T> |
getEncodedTypeDescriptor()
Returns the
TypeDescriptor for the type encoded. |
int |
hashCode() |
static <T,IntermediateT> |
of(Coder<IntermediateT> coder,
DelegateCoder.CodingFunction<T,IntermediateT> toFn,
DelegateCoder.CodingFunction<IntermediateT,T> fromFn) |
static <T,IntermediateT> |
of(Coder<IntermediateT> coder,
DelegateCoder.CodingFunction<T,IntermediateT> toFn,
DelegateCoder.CodingFunction<IntermediateT,T> fromFn,
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 DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor)
public static <T,IntermediateT> DelegateCoder<T,IntermediateT> of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
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)
public void encode(T value, java.io.OutputStream outStream) throws CoderException, java.io.IOException
Coder
T
onto the given output stream.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.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 Coder<IntermediateT> getCoder()
DelegateCoder
.public 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>
NonDeterministicException
- when the underlying coder's verifyDeterministic()
throws a Coder.NonDeterministicException
. For this to be safe, the
intermediate CodingFunction<T, IntermediateT>
must also be deterministic.Coder.NonDeterministicException
- if this coder is not deterministic.public java.lang.Object structuralValue(T value)
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(@code 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>
T
obtained by first converting to
IntermediateT
and then obtaining a structural value according to the underlying
coder.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public TypeDescriptor<T> getEncodedTypeDescriptor()
Coder
TypeDescriptor
for the type encoded.getEncodedTypeDescriptor
in class Coder<T>