Browse Source

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.
pull/2866/head
Mark Paluch 3 years ago
parent
commit
6ca3170bde
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 29
      src/main/java/org/springframework/data/support/WindowIterator.java

29
src/main/java/org/springframework/data/support/WindowIterator.java

@ -15,8 +15,9 @@ @@ -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> { @@ -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> { @@ -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();
}
}
}

Loading…
Cancel
Save