Polishing.

Use ReverseListIterator instead of Stream API to reduce overhead. ListIterator provides means to iterate backwards so we're wrapping the existing iterator.

See #2857.
Original pull request: #2858.
This commit is contained in:
Mark Paluch
2023-06-16 10:13:36 +02:00
parent d1568198d8
commit 6ca3170bde

View File

@@ -15,8 +15,9 @@
*/
package org.springframework.data.support;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.function.Function;
@@ -83,7 +84,7 @@ public class WindowIterator<T> implements Iterator<T> {
if (currentIterator == null) {
if (currentWindow != null) {
currentIterator = isBackwardsScrolling(currentPosition)
? currentWindow.stream().sorted(Collections.reverseOrder()).iterator()
? new ReverseListIterator<>(currentWindow.getContent())
: currentWindow.iterator();
}
}
@@ -161,4 +162,28 @@ public class WindowIterator<T> implements Iterator<T> {
return new WindowIterator<>(windowFunction, position);
}
}
private static class ReverseListIterator<T> implements Iterator<T> {
private final ListIterator<T> delegate;
public ReverseListIterator(List<T> list) {
this.delegate = list.listIterator(list.size());
}
@Override
public boolean hasNext() {
return delegate.hasPrevious();
}
@Override
public T next() {
return delegate.previous();
}
@Override
public void remove() {
delegate.remove();
}
}
}