|
@@ -149,6 +149,10 @@ public final class IteratorUtils {
|
|
|
public static <T> Iterator<T> reverse(ListIterator<? extends T> iterator) {
|
|
public static <T> Iterator<T> reverse(ListIterator<? extends T> iterator) {
|
|
|
return new ReverseIterator<>(iterator);
|
|
return new ReverseIterator<>(iterator);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static <T> ListIterator<T> buffered(Iterator<? extends T> iterator, int max) {
|
|
|
|
|
+ return new BufferedIterator<>(iterator, max);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public static <T> int next(Iterator<? extends T> iterator, int count) {
|
|
public static <T> int next(Iterator<? extends T> iterator, int count) {
|
|
|
int i=0;
|
|
int i=0;
|
|
@@ -553,6 +557,83 @@ public final class IteratorUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private static class BufferedIterator<T> implements ListIterator<T> {
|
|
|
|
|
+
|
|
|
|
|
+ private final int max;
|
|
|
|
|
+
|
|
|
|
|
+ private final Iterator<? extends T> iterator;
|
|
|
|
|
+
|
|
|
|
|
+ private final Deque<T> prev = new LinkedList<T>();
|
|
|
|
|
+
|
|
|
|
|
+ private final Deque<T> next = new LinkedList<T>();
|
|
|
|
|
+
|
|
|
|
|
+ private int index;
|
|
|
|
|
+
|
|
|
|
|
+ public BufferedIterator(Iterator<? extends T> iterator, int max) {
|
|
|
|
|
+ this.max = max;
|
|
|
|
|
+ this.index = 0;
|
|
|
|
|
+ this.iterator = iterator;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean hasNext() {
|
|
|
|
|
+ return !next.isEmpty() || iterator.hasNext();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public T next() {
|
|
|
|
|
+ index++;
|
|
|
|
|
+ T out = !next.isEmpty() ? next.removeLast() : iterator.next();
|
|
|
|
|
+ prev.addLast(out);
|
|
|
|
|
+ if(prev.size() > max) {
|
|
|
|
|
+ prev.removeFirst();
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean hasPrevious() {
|
|
|
|
|
+ return !prev.isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public T previous() {
|
|
|
|
|
+ index--;
|
|
|
|
|
+ T out = prev.removeLast();
|
|
|
|
|
+ next.addLast(out);
|
|
|
|
|
+ if(next.size() > max) {
|
|
|
|
|
+ next.removeFirst();
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int nextIndex() {
|
|
|
|
|
+ return index;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int previousIndex() {
|
|
|
|
|
+ return index-1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void remove() {
|
|
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void set(T t) {
|
|
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void add(T t) {
|
|
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private static final class FlatIterator<T> implements Iterator<T> {
|
|
private static final class FlatIterator<T> implements Iterator<T> {
|
|
|
|
|
|