fr.lirmm.graphik.util.stream.CloseableIteratorAccumulator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integraal-graal-ruleset-analysis Show documentation
Show all versions of integraal-graal-ruleset-analysis Show documentation
Rule base analysis for InteGraal. This is imported from Graal
package fr.lirmm.graphik.util.stream;
import java.util.ArrayList;
import java.util.List;
/**
* This Iterator stores the items passed in a buffer memory.
*
* @author Olivier Rodriguez
*/
public class CloseableIteratorAccumulator extends AbstractCloseableIterator {
private CloseableIterator it;
List accu = new ArrayList<>();
boolean hasNext = false;
boolean nextIsStore = false;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
// /////////////////////////////////////////////////////////////////////////
public CloseableIteratorAccumulator(CloseableIterator it) throws IteratorException {
this.it = it;
hasNext = it.hasNext();
}
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
if (nextIsStore) {
hasNext = it.hasNext();
nextIsStore = false;
}
return hasNext;
}
@Override
public E next() throws IteratorException {
E ret = it.next();
if (!nextIsStore) {
accu.add(ret);
nextIsStore = true;
}
return ret;
}
@Override
public void close() {
it.close();
}
/**
* Consume the iterator (iterate until the last element).
*
* @return
* @throws IteratorException
*/
public CloseableIteratorAccumulator consumeAll() throws IteratorException {
while (hasNext())
next();
return this;
}
@SuppressWarnings("unchecked")
public E[] toArray() {
return (E[]) accu.toArray();
}
public List getList() {
// Get a copy for not being able to modify the $accu outside.
return new ArrayList<>(accu);
}
}