Class NullableCoder<T>
- Type Parameters:
T- the type of the values being transcoded
- All Implemented Interfaces:
Serializable
NullableCoder encodes nullable values of type T using a nested Coder<T>
that does not tolerate null values. NullableCoder uses exactly 1 byte per entry
to indicate whether the value is null, then adds the encoding of the inner coder for
non-null values.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Method Summary
Modifier and TypeMethodDescriptionbooleanNullableCoderis consistent with equals if the nestedCoderis.decode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.decode(InputStream inStream, Coder.Context context) Decodes a value of typeTfrom the given input stream in the given context.voidencode(@Nullable T value, OutputStream outStream) Encodes the given value of typeTonto the given output stream.voidencode(@Nullable T value, OutputStream outStream, Coder.Context context) Encodes the given value of typeTonto the given output stream in the given context.protected longgetEncodedElementByteSize(@Nullable T value) Overridden to short-circuit the defaultStructuredCoderbehavior of encoding and counting the bytes.Returns theTypeDescriptorfor the type encoded.Returns the innerCoderwrapped by thisNullableCoderinstance.booleanNullableCoderis cheap ifvalueCoderis cheap.static <T> NullableCoder<T> voidregisterByteSizeObserver(@Nullable T value, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) Overridden to short-circuit the defaultStructuredCoderbehavior of encoding and counting the bytes.structuralValue(@Nullable T value) Returns an object with anObject.equals()method that represents structural equality on the argument.voidNullableCoderis deterministic if the nestedCoderis.Methods inherited from class org.apache.beam.sdk.coders.StructuredCoder
equals, getComponents, hashCode, toStringMethods inherited from class org.apache.beam.sdk.coders.Coder
getEncodedElementByteSizeUsingCoder, verifyDeterministic, verifyDeterministic
-
Method Details
-
of
-
getValueCoder
Returns the innerCoderwrapped by thisNullableCoderinstance. -
encode
Description copied from class:CoderEncodes the given value of typeTonto 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:
encodein classCoder<T>- Throws:
IOException- if writing to theOutputStreamfails for some reasonCoderException- if the value could not be encoded for some reason
-
encode
public void encode(@Nullable T value, OutputStream outStream, Coder.Context context) throws IOException, CoderException Description copied from class:CoderEncodes the given value of typeTonto the given output stream in the given context.- Overrides:
encodein classCoder<T>- Throws:
IOException- if writing to theOutputStreamfails for some reasonCoderException- if the value could not be encoded for some reason
-
decode
Description copied from class:CoderDecodes a value of typeTfrom 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:
decodein classCoder<T>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
decode
public @Nullable T decode(InputStream inStream, Coder.Context context) throws IOException, CoderException Description copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value.- Overrides:
decodein classCoder<T>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
getCoderArguments
Description copied from class:CoderIf this is aCoderfor a parameterized type, returns the list ofCoders being used for each of the parameters in the same order they appear within the parameterized type's type signature. If this cannot be done, or thisCoderdoes not encode/decode a parameterized type, returns the empty list.- Specified by:
getCoderArgumentsin classCoder<T>
-
verifyDeterministic
NullableCoderis deterministic if the nestedCoderis.Throw
Coder.NonDeterministicExceptionif the coding is not deterministic.In order for a
Coderto 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
Coderalways produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Specified by:
verifyDeterministicin classCoder<T>- Throws:
Coder.NonDeterministicException- if this coder is not deterministic.
- two values that compare as equal (via
-
consistentWithEquals
public boolean consistentWithEquals()NullableCoderis consistent with equals if the nestedCoderis.Returns
trueif thisCoderis injective with respect toObject.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 fornull.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.
- Overrides:
consistentWithEqualsin classCoder<T>
-
structuralValue
Description copied from class:CoderReturns an object with anObject.equals()method that represents structural equality on the argument.For any two values
xandyof 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
nullshould 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:
structuralValuein classCoder<T>
-
registerByteSizeObserver
public void registerByteSizeObserver(@Nullable T value, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) throws Exception Overridden to short-circuit the defaultStructuredCoderbehavior of encoding and counting the bytes. The size is known (1 byte) whenvalueisnull, otherwise the size is 1 byte plus the size of nestedCoder's encoding ofvalue.Notifies the
ElementByteSizeObserverabout the byte size of the encoded value using thisCoder.Not intended to be called by user code, but instead by
PipelineRunnerimplementations.By default, this notifies
observerabout the byte size of the encoded value using this coder as returned byCoder.getEncodedElementByteSize(T).- Overrides:
registerByteSizeObserverin classCoder<T>- Throws:
Exception
-
getEncodedElementByteSize
Overridden to short-circuit the defaultStructuredCoderbehavior of encoding and counting the bytes. The size is known (1 byte) whenvalueisnull, otherwise the size is 1 byte plus the size of nestedCoder's encoding ofvalue.Returns the size in bytes of the encoded value using this coder.
- Overrides:
getEncodedElementByteSizein classCoder<T>- Throws:
Exception
-
isRegisterByteSizeObserverCheap
NullableCoderis cheap ifvalueCoderis cheap.Returns whether
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)cheap enough to call for every element, that is, if thisCodercan calculate the byte size of the element to be coded in roughly constant time (or lazily).Not intended to be called by user code, but instead by
PipelineRunnerimplementations.By default, returns false. The default
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)implementation invokesCoder.getEncodedElementByteSize(T)which requires re-encoding an element unless it is overridden. This is considered expensive.- Overrides:
isRegisterByteSizeObserverCheapin classCoder<T>
-
getEncodedTypeDescriptor
Description copied from class:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
getEncodedTypeDescriptorin classCoder<T>
-