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/01: SLING-7524 updated unique function to only return unique items
Date Thu, 01 Mar 2018 17:30:23 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 69b331dce08e915397f20d5ca5bb1e843515c3d0
Author: JE Bailey <jeb@apache.org>
AuthorDate: Thu Mar 1 12:27:50 2018 -0500

    SLING-7524 updated unique function to only return unique items
    
    Method is similar to how the distinct method works in the Stream api and
    utilizes a HashSet of previously seen items
---
 .../java/org/apache/sling/query/AbstractQuery.java |  2 +-
 .../sling/query/function/UniqueFunction.java       |  9 +-------
 .../sling/query/iterator/UniqueIterator.java       | 26 ++++++++++------------
 .../java/org/apache/sling/query/UniqueTest.java    |  2 +-
 4 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/src/main/java/org/apache/sling/query/AbstractQuery.java b/src/main/java/org/apache/sling/query/AbstractQuery.java
index 52643a8..ea37084 100644
--- a/src/main/java/org/apache/sling/query/AbstractQuery.java
+++ b/src/main/java/org/apache/sling/query/AbstractQuery.java
@@ -757,7 +757,7 @@ public abstract class AbstractQuery<T, Q extends AbstractQuery<T,
Q>> implements
 	 * @return new SlingQuery object transformed by this operation
 	 */
 	public Q unique() {
-		return function(new UniqueFunction<>(provider));
+		return function(new UniqueFunction<>());
 	}
 
 	private Q function(Function<?, ?> function, Iterable<T> iterable) {
diff --git a/src/main/java/org/apache/sling/query/function/UniqueFunction.java b/src/main/java/org/apache/sling/query/function/UniqueFunction.java
index b9a95d3..b5332bf 100644
--- a/src/main/java/org/apache/sling/query/function/UniqueFunction.java
+++ b/src/main/java/org/apache/sling/query/function/UniqueFunction.java
@@ -23,20 +23,13 @@ import java.util.Iterator;
 
 import org.apache.sling.query.api.internal.IteratorToIteratorFunction;
 import org.apache.sling.query.api.internal.Option;
-import org.apache.sling.query.api.internal.TreeProvider;
 import org.apache.sling.query.iterator.UniqueIterator;
 
 public class UniqueFunction<T> implements IteratorToIteratorFunction<T> {
 
-	private final TreeProvider<T> treeProvider;
-
-	public UniqueFunction(TreeProvider<T> treeProvider) {
-		this.treeProvider = treeProvider;
-	}
-
 	@Override
 	public Iterator<Option<T>> apply(Iterator<Option<T>> input) {
-		return new UniqueIterator<>(input, treeProvider);
+		return new UniqueIterator<>(input);
 	}
 
 }
diff --git a/src/main/java/org/apache/sling/query/iterator/UniqueIterator.java b/src/main/java/org/apache/sling/query/iterator/UniqueIterator.java
index 48b65b0..3692d59 100644
--- a/src/main/java/org/apache/sling/query/iterator/UniqueIterator.java
+++ b/src/main/java/org/apache/sling/query/iterator/UniqueIterator.java
@@ -19,22 +19,21 @@
 
 package org.apache.sling.query.iterator;
 
+import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Set;
 
 import org.apache.sling.query.api.internal.Option;
-import org.apache.sling.query.api.internal.TreeProvider;
 
 public class UniqueIterator<T> extends AbstractIterator<Option<T>> {
 
 	private final Iterator<Option<T>> iterator;
+	
+	private Set<T> seen;
 
-	private final TreeProvider<T> treeProvider;
-
-	private T lastElement;
-
-	public UniqueIterator(Iterator<Option<T>> input, TreeProvider<T> treeProvider)
{
+	public UniqueIterator(Iterator<Option<T>> input) {
 		this.iterator = input;
-		this.treeProvider = treeProvider;
+		seen = new HashSet<>();
 	}
 
 	@Override
@@ -43,14 +42,13 @@ public class UniqueIterator<T> extends AbstractIterator<Option<T>>
{
 			return null;
 		}
 		Option<T> candidate = iterator.next();
-		Option<T> result;
-		if (treeProvider.sameElement(lastElement, candidate.getElement())) {
-			result = Option.empty(candidate.getArgumentId());
-		} else {
-			result = candidate;
+		if (candidate.isEmpty()) {
+			return candidate;
+		}
+		if (!seen.add(candidate.getElement())) {
+			return Option.empty(candidate.getArgumentId());
 		}
-		lastElement = candidate.getElement();
-		return result;
+		return candidate;
 	}
 
 }
diff --git a/src/test/java/org/apache/sling/query/UniqueTest.java b/src/test/java/org/apache/sling/query/UniqueTest.java
index 78966a1..1c58674 100644
--- a/src/test/java/org/apache/sling/query/UniqueTest.java
+++ b/src/test/java/org/apache/sling/query/UniqueTest.java
@@ -36,6 +36,6 @@ public class UniqueTest {
 		Resource r3 = tree.getChild("home/java");
 
 		SlingQuery query = $(r1, r1, r1, r2, r2, r3, r3, r3, r1).unique();
-		assertResourceListEquals(query.iterator(), "home", "application", "java", "home");
+		assertResourceListEquals(query.iterator(), "home", "application", "java");
 	}
 }

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

Mime
View raw message