public abstract class StructuredCoder<T> extends Coder<T>
Coder
that defines equality, hashing, and printing
via the class name and recursively using getComponents()
.
A StructuredCoder
should be defined purely in terms of its component coders, and
contain no additional configuration.
To extend StructuredCoder
, override the following methods as appropriate:
getComponents()
: the default implementation returns Coder.getCoderArguments()
.
Coder.getEncodedElementByteSize(T)
and Coder.isRegisterByteSizeObserverCheap(T)
: the
default implementation encodes values to bytes and counts the bytes, which is considered
expensive. The default element byte size observer uses the value returned by
Coder.getEncodedElementByteSize(T)
.
Coder.Context, Coder.NonDeterministicException
Modifier | Constructor and Description |
---|---|
protected |
StructuredCoder() |
Modifier and Type | Method and Description |
---|---|
boolean |
consistentWithEquals()
|
boolean |
equals(java.lang.Object o) |
java.util.List<? extends Coder<?>> |
getComponents()
|
TypeDescriptor<T> |
getEncodedTypeDescriptor()
Returns the
TypeDescriptor for the type encoded. |
int |
hashCode() |
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() |
decode, decode, encode, encode, getCoderArguments, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic, verifyDeterministic
public java.util.List<? extends Coder<?>> getComponents()
Coders
that are components of this Coder
.
The default components will be equal to the value returned by Coder.getCoderArguments()
.
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
true
if the two StructuredCoder
instances have the
same class and equal components.public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public boolean consistentWithEquals()
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>
false
for StructuredCoder
unless overridden.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(@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>
public TypeDescriptor<T> getEncodedTypeDescriptor()
Coder
TypeDescriptor
for the type encoded.getEncodedTypeDescriptor
in class Coder<T>