Package com.hazelcast.jet.core
Class AppendableTraverser<T>
- java.lang.Object
-
- com.hazelcast.jet.core.AppendableTraverser<T>
-
- Type Parameters:
T
- item type
- All Implemented Interfaces:
Traverser<T>
public class AppendableTraverser<T> extends java.lang.Object implements Traverser<T>
A traverser with an internalArrayDeque
. You can efficientlyappend(T)
items to it that will be later traversed.It's useful to be returned from a flat-mapping function when an item is flat-mapped to a small number of items:
TheTraverser<Integer> at = new AppendableTraverser(2); Traverser<Integer> t = Traverser.over(10, 20) .flatMap(item -> { at.append(item); at.append(item + 1); return at; });
t
traverser above will output {10, 11, 20, 21}. This approach reduces the GC pressure by avoiding the allocation of a new traverser for each item that will traverse over just a few or even zero items.See
ResettableSingletonTraverser
if you have at most one item to traverse.- Since:
- Jet 3.0
-
-
Constructor Summary
Constructors Constructor Description AppendableTraverser(int initialCapacity)
Creates an appendable traverser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AppendableTraverser<T>
append(T item)
Returns a traverser that will return all the items of this traverser, plus an additional item once this one returnsnull
.boolean
isEmpty()
T
next()
Returns the next item, removing it from this traverser.
-
-
-
Method Detail
-
append
@Nonnull public AppendableTraverser<T> append(@Nonnull T item)
Returns a traverser that will return all the items of this traverser, plus an additional item once this one returnsnull
. After that it continues forwarding the return values of this traverser. It is meant to be used on finite traversers.Default implementations always returns a new traverser instance. If you need to append multiple objects or use this method frequently,
AppendableTraverser
might be a better choice.
-
next
public T next()
Description copied from interface:Traverser
Returns the next item, removing it from this traverser. If no item is available, returnsnull
. If this traverser is null-terminated, getting anull
means it's exhausted and will keep returningnull
forever. Otherwise, trying again later may produce one.
-
isEmpty
public boolean isEmpty()
-
-