Package alluxio.resource
Class CloseableIterator<T>
- java.lang.Object
-
- alluxio.resource.CloseableResource<java.util.Iterator<T>>
-
- alluxio.resource.CloseableIterator<T>
-
- Type Parameters:
T
- the type of elements returned by the iterator
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.util.Iterator<T>
public abstract class CloseableIterator<T> extends CloseableResource<java.util.Iterator<T>> implements java.util.Iterator<T>
ACloseableIterator<T>
is an iterator which requires cleanup when it is no longer in use. When the user createsCloseableIterator
, thecloseResource()
method needs to be implemented to define how the resource can be closed. The iterator is not necessarilyCloseable
so we do not provide a default implementation forcloseResource()
. Users are required to use the factory methods instead of the constructors to create instances.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> CloseableIterator<T>
concat(CloseableIterator<T> a, CloseableIterator<T> b)
Combines two iterators into a single iterator.static <T> CloseableIterator<T>
concat(java.util.List<CloseableIterator<T>> iterators)
Concatenates iterators.static <T> CloseableIterator<T>
create(java.util.Iterator<? extends T> iterator, java.util.function.Consumer<java.lang.Void> closeAction)
Wrap around an iterator with a resource to close.boolean
hasNext()
T
next()
static <T> CloseableIterator<T>
noopCloseable(java.util.Iterator<? extends T> iterator)
Wraps around an iterator with no resource to close.static int
size(CloseableIterator<?> iter)
Consumes the iterator, closes it, and returns its size.-
Methods inherited from class alluxio.resource.CloseableResource
close, closeResource, get
-
-
-
-
Method Detail
-
hasNext
public boolean hasNext()
- Specified by:
hasNext
in interfacejava.util.Iterator<T>
-
create
public static <T> CloseableIterator<T> create(java.util.Iterator<? extends T> iterator, java.util.function.Consumer<java.lang.Void> closeAction)
Wrap around an iterator with a resource to close. How to close the resource is defined by the caller in the lambda expression.- Type Parameters:
T
- the type of the iterable- Parameters:
iterator
- the iterator to wrap aroundcloseAction
- the callback to properly clean up the resource- Returns:
- the closeable iterator
-
noopCloseable
public static <T> CloseableIterator<T> noopCloseable(java.util.Iterator<? extends T> iterator)
Wraps around an iterator with no resource to close.- Type Parameters:
T
- the type of the iterable- Parameters:
iterator
- the iterator to wrap around- Returns:
- the closeable iterator
-
concat
public static <T> CloseableIterator<T> concat(CloseableIterator<T> a, CloseableIterator<T> b)
Combines two iterators into a single iterator.- Type Parameters:
T
- type of iterator- Parameters:
a
- an iteratorb
- another iterator- Returns:
- the concatenated iterator
-
concat
public static <T> CloseableIterator<T> concat(java.util.List<CloseableIterator<T>> iterators)
Concatenates iterators.- Type Parameters:
T
- type of iterator- Parameters:
iterators
- a list of iterators- Returns:
- a concatenated iterator that iterate over all elements from each of the child iterators
-
size
public static int size(CloseableIterator<?> iter)
Consumes the iterator, closes it, and returns its size.- Parameters:
iter
- the iter to get the size of- Returns:
- the size of the iter
-
-