Google Cloud Dataflow SDK for Java, version 1.9.1
Class IterableLikeCoder<T,IterableT extends Iterable<T>>
- java.lang.Object
-
- com.google.cloud.dataflow.sdk.coders.StandardCoder<IterableT>
-
- com.google.cloud.dataflow.sdk.coders.IterableLikeCoder<T,IterableT>
-
- Type Parameters:
T
- the type of the elements of theIterable
s being transcodedIterableT
- the type of the Iterables being transcoded
- All Implemented Interfaces:
- Coder<IterableT>, Serializable
- Direct Known Subclasses:
- CollectionCoder, IterableCoder, ListCoder, SetCoder
public abstract class IterableLikeCoder<T,IterableT extends Iterable<T>> extends StandardCoder<IterableT>
An abstract base class with functionality for assembling aCoder
for a class that implementsIterable
.To complete a subclass, implement the
decodeToIterable(java.util.List<T>)
method. This superclass will decode the elements in the input stream into aList
and then pass them to that method to be converted into the appropriate iterable type. Note that this means the input iterables must fit into memory.The format of this coder is as follows:
- If the input
Iterable
has a known and finite size, then the size is written to the output stream in big endian format, followed by all of the encoded elements. - If the input
Iterable
is not known to have a finite size, then each element of the input is preceded bytrue
encoded as a byte (indicating "more data") followed by the encoded element, and terminated byfalse
encoded as a byte.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.google.cloud.dataflow.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Constructor Summary
Constructors Modifier Constructor and Description protected
IterableLikeCoder(Coder<T> elementCoder, String iterableName)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description IterableT
decode(InputStream inStream, Coder.Context context)
Decodes a value of typeT
from the given input stream in the given context.protected abstract IterableT
decodeToIterable(List<T> decodedElements)
Builds an instance ofIterableT
, this coder's associatedIterable
-like subtype, from a list of decoded elements.void
encode(IterableT iterable, OutputStream outStream, Coder.Context context)
Encodes the given value of typeT
onto the given output stream in the given context.List<? extends Coder<?>>
getCoderArguments()
If this is aCoder
for a parameterized type, returns the list ofCoder
s being used for each of the parameters, or returnsnull
if this cannot be done or this is not a parameterized type.Coder<T>
getElemCoder()
protected static <T,IterableT extends Iterable<T>>
List<Object>getInstanceComponentsHelper(IterableT exampleValue)
Returns the first element in the iterable-likeexampleValue
if it is non-empty, otherwise returnsnull
.boolean
isRegisterByteSizeObserverCheap(IterableT iterable, Coder.Context context)
Returns whetherCoder.registerByteSizeObserver(T, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver, com.google.cloud.dataflow.sdk.coders.Coder.Context)
cheap enough to call for every element, that is, if thisCoder
can calculate the byte size of the element to be coded in roughly constant time (or lazily).void
registerByteSizeObserver(IterableT iterable, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver observer, Coder.Context context)
Notifies theElementByteSizeObserver
about the byte size of the encoded value using thisCoder
.void
verifyDeterministic()
ThrowCoder.NonDeterministicException
if the coding is not deterministic.-
Methods inherited from class com.google.cloud.dataflow.sdk.coders.StandardCoder
asCloudObject, consistentWithEquals, equals, getAllowedEncodings, getComponents, getEncodedElementByteSize, getEncodingId, hashCode, structuralValue, toString, verifyDeterministic, verifyDeterministic
-
-
-
-
Method Detail
-
decodeToIterable
protected abstract IterableT decodeToIterable(List<T> decodedElements)
Builds an instance ofIterableT
, this coder's associatedIterable
-like subtype, from a list of decoded elements.
-
getInstanceComponentsHelper
protected static <T,IterableT extends Iterable<T>> List<Object> getInstanceComponentsHelper(IterableT exampleValue)
Returns the first element in the iterable-likeexampleValue
if it is non-empty, otherwise returnsnull
.
-
encode
public void encode(IterableT iterable, OutputStream outStream, Coder.Context context) throws IOException, CoderException
Description copied from interface:Coder
Encodes the given value of typeT
onto the given output stream in the given context.- Throws:
IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
decode
public IterableT decode(InputStream inStream, Coder.Context context) throws IOException, CoderException
Description copied from interface:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Throws:
IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
getCoderArguments
public List<? extends Coder<?>> getCoderArguments()
Description copied from interface:Coder
If this is aCoder
for a parameterized type, returns the list ofCoder
s being used for each of the parameters, or returnsnull
if this cannot be done or this is not a parameterized type.
-
verifyDeterministic
public void verifyDeterministic() throws Coder.NonDeterministicException
ThrowCoder.NonDeterministicException
if the coding is not deterministic.In order for a
Coder
to 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
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.
- Throws:
NonDeterministicException
- always. Encoding is not deterministic for the generalIterable
case, as it depends upon the type of iterable. This may allow two objects to compare as equal while the encoding differs.Coder.NonDeterministicException
- if this coder is not deterministic.
- two values that compare as equal (via
-
isRegisterByteSizeObserverCheap
public boolean isRegisterByteSizeObserverCheap(IterableT iterable, Coder.Context context)
Returns whetherCoder.registerByteSizeObserver(T, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver, com.google.cloud.dataflow.sdk.coders.Coder.Context)
cheap enough to call for every element, that is, if thisCoder
can 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
PipelineRunner
implementations.- Specified by:
isRegisterByteSizeObserverCheap
in interfaceCoder<IterableT extends Iterable<T>>
- Overrides:
isRegisterByteSizeObserverCheap
in classStandardCoder<IterableT extends Iterable<T>>
- Returns:
true
if the iterable is of a known class that supports lazy counting of byte size, since that requires minimal extra computation.
-
registerByteSizeObserver
public void registerByteSizeObserver(IterableT iterable, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver observer, Coder.Context context) throws Exception
Description copied from class:StandardCoder
Notifies theElementByteSizeObserver
about the byte size of the encoded value using thisCoder
.Not intended to be called by user code, but instead by
PipelineRunner
implementations.For
StandardCoder
subclasses, this notifiesobserver
about the byte size of the encoded value using this coder as returned byStandardCoder.getEncodedElementByteSize(T, com.google.cloud.dataflow.sdk.coders.Coder.Context)
.- Specified by:
registerByteSizeObserver
in interfaceCoder<IterableT extends Iterable<T>>
- Overrides:
registerByteSizeObserver
in classStandardCoder<IterableT extends Iterable<T>>
- Throws:
Exception
-
-