sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject [sling-org-apache-sling-query] 01/03: SLING-7540 LastIterator improvements
Date Fri, 09 Mar 2018 19:30:35 GMT
This is an automated email from the ASF dual-hosted git repository.

jeb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-query.git

commit c955cc411a67db7ffd83d390051b5104376a5b57
Author: JE Bailey <jeb@apache.org>
AuthorDate: Fri Mar 9 14:27:52 2018 -0500

    SLING-7540 LastIterator improvements
    
    Removed pre-determination of the last item to make it more "lazy"
---
 .../apache/sling/query/iterator/LastIterator.java  | 62 ++++++++--------------
 1 file changed, 22 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/sling/query/iterator/LastIterator.java b/src/main/java/org/apache/sling/query/iterator/LastIterator.java
index b02621f..2316b97 100644
--- a/src/main/java/org/apache/sling/query/iterator/LastIterator.java
+++ b/src/main/java/org/apache/sling/query/iterator/LastIterator.java
@@ -20,60 +20,42 @@
 package org.apache.sling.query.iterator;
 
 import java.util.Iterator;
-import java.util.ListIterator;
 
 import org.apache.sling.query.api.internal.Option;
-import org.apache.sling.query.util.LazyList;
 
+/**
+ * 
+ * 
+ *
+ * @param <T>
+ */
 public class LastIterator<T> extends AbstractIterator<Option<T>> {
 
-	private final LazyList<Option<T>> lazyList;
-
-	private final ListIterator<Option<T>> iterator;
-
-	private boolean finished;
+	private final Iterator<Option<T>> iterator;
 
-	private boolean initialized;
-
-	private int lastIndex = -1;
+	private Option<T> previous;
 
 	public LastIterator(Iterator<Option<T>> iterator) {
-		this.lazyList = new LazyList<Option<T>>(iterator);
-		this.iterator = lazyList.listIterator();
+		this.iterator = iterator;
 	}
 
 	@Override
 	protected Option<T> getElement() {
-		if (finished) {
-			return null;
-		}
-
-		initializeLastIndex();
-
-		Option<T> candidate;
-		if (iterator.hasNext()) {
-			candidate = iterator.next();
-		} else {
-			finished = true;
-			return null;
-		}
-		if (iterator.previousIndex() == lastIndex) {
-			finished = true;
+		Option<T> candidate = previous;
+		
+		if (!iterator.hasNext()) {
+			previous = null;
 			return candidate;
-		} else {
-			return Option.empty(candidate.getArgumentId());
-		}
-	}
-
-	private void initializeLastIndex() {
-		ListIterator<Option<T>> i = lazyList.listIterator();
-		if (!initialized) {
-			while (i.hasNext()) {
-				if (!i.next().isEmpty()) {
-					lastIndex = i.previousIndex();
-				}
+		} 
+		
+		if (candidate == null) {
+			candidate = iterator.next();
+			if (!iterator.hasNext()) {
+				return candidate;
 			}
 		}
-		initialized = true;
+		previous = iterator.next();
+		return Option.empty(candidate.getArgumentId());
 	}
+
 }

-- 
To stop receiving notification emails like this one, please contact
jeb@apache.org.

Mime
View raw message