You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/02/15 13:52:34 UTC

[sling-whiteboard] branch master updated: streamlined api so that there is a single stream()

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-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 03b1817  streamlined api so that there is a single stream()
03b1817 is described below

commit 03b1817468b4d6c4ae14c30bb28cf03646d62ff4
Author: JE Bailey <ja...@sas.com>
AuthorDate: Thu Feb 15 08:52:30 2018 -0500

    streamlined api so that there is a single stream()
---
 .../sling/resource/stream/ResourceStream.java      | 110 ++++++++++++++-------
 .../resource/stream/ResourceFilterDateTest.java    |   4 +-
 .../resource/stream/ResourceFilterLogicTest.java   |   4 +-
 .../sling/resource/stream/ResourceFilterTest.java  |   2 +-
 4 files changed, 79 insertions(+), 41 deletions(-)

diff --git a/streams/src/main/java/org/apache/sling/resource/stream/ResourceStream.java b/streams/src/main/java/org/apache/sling/resource/stream/ResourceStream.java
index 11beeed..ae89143 100644
--- a/streams/src/main/java/org/apache/sling/resource/stream/ResourceStream.java
+++ b/streams/src/main/java/org/apache/sling/resource/stream/ResourceStream.java
@@ -19,6 +19,7 @@ import java.util.Objects;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
 import java.util.function.Predicate;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
@@ -43,6 +44,10 @@ public class ResourceStream {
 	private long limit = 0;
 
 	private long startOfRange;
+	
+	private Predicate<Resource> branchSelector = resource -> true;
+
+	private Predicate<Resource> childSelector = resource -> true;
 
 	/**
 	 * Starting point to locate resources. resources of the start resource.
@@ -65,22 +70,6 @@ public class ResourceStream {
 	}
 
 	/**
-	 * Rests the starting path for the query to be the provided path. This can be
-	 * used to limit the possible branching options beneath a resource tree, or to
-	 * use a parent the resource as the basis of permissions for another resource
-	 * structure
-	 * 
-	 * 
-	 * @param path
-	 *            set the internal resource to path
-	 * @return this locator
-	 */
-	public ResourceStream startingFrom(String path) {
-		this.resource = Objects.requireNonNull(resource.getResourceResolver().getResource(path));
-		return this;
-	}
-
-	/**
 	 * Sets the maximum number of items to be returned or processed. Starting from
 	 * the first matching resource. This method is mutually exclusive to the range
 	 * method
@@ -119,40 +108,83 @@ public class ResourceStream {
 		this.limit = limit;
 		return this;
 	}
+	
 
 	/**
-	 * Provides a stream of resources starting from the initiator resource and
-	 * traversing through it's descendants The only fluent api check it performs is
-	 * of the traversal predicate.
+	 * Resets the starting path for the query to be the provided path. This can be
+	 * used to limit the possible branching options beneath a resource tree, or to
+	 * use a parent the resource as the basis of permissions for another resource
+	 * structure
 	 * 
-	 * @return self closing {@code Stream<Resource>} of unknown size.
+	 * 
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
 	 */
-	public Stream<Resource> stream() {
-		return stream(resource -> true);
+	public ResourceStream startingFrom(String path) {
+		this.resource = Objects.requireNonNull(resource.getResourceResolver().getResource(path));
+		return this;
 	}
-
+	
 	/**
-	 * Provides a stream of resources starting from the initiator resource and
-	 * traversing through it's descendants The only fluent api check it performs is
-	 * of the traversal predicate.
+	 * Sets the branch selector for the traversal process. 
 	 * 
-	 * @return self closing {@code Stream<Resource>} of unknown size.
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
+	 * @throws ParseException 
 	 */
-	public Stream<Resource> stream(String branchSelector) throws ParseException {
-		return stream(new ResourceFilter(branchSelector));
+	public ResourceStream setBranchSelector(String branchSelector) throws ParseException {
+		return setBranchSelector(new ResourceFilter(branchSelector));
+	}
+	
+	/**
+	 * Sets the branch selector for the traversal process. 
+	 * 
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
+	 */
+	public ResourceStream setBranchSelector(Predicate<Resource> branchSelector) {
+		this.branchSelector = Objects.requireNonNull(branchSelector);
+		return this;
+	}
+	
+	/**
+	 * Sets the branch selector for the traversal process. 
+	 * 
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
+	 * @throws ParseException 
+	 */
+	public ResourceStream setChildSelector(String childSelector) throws ParseException {
+		return setChildSelector(new ResourceFilter(childSelector));
+	}
+	
+	/**
+	 * Sets a child selector which defines whether a given reosource should be part of the stream 
+	 * 
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
+	 */
+	public ResourceStream setChildSelector(Predicate<Resource> childSelector) {
+		this.childSelector = Objects.requireNonNull(childSelector);
+		return this;
 	}
 
 	/**
 	 * Provides a stream of resources starting from the initiator resource and
-	 * traversing through it's descendants The only fluent api check it performs is
-	 * of the traversal predicate.
+	 * traversing through it's descendants
 	 * 
 	 * @return self closing {@code Stream<Resource>} of unknown size.
 	 */
-	public Stream<Resource> stream(Predicate<Resource> branchSelector) {
+	public Stream<Resource> stream() {
 		return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<Resource>() {
 
-			final LinkedList<Resource> resourcesToCheck = new LinkedList<>();
+			private final LinkedList<Resource> resourcesToCheck = new LinkedList<>();
+
 			final AtomicInteger index = new AtomicInteger(0);
 
 			{
@@ -186,7 +218,7 @@ public class ResourceStream {
 							resourcesToCheck.clear();
 						}
 					}
-				} while (startOfRange > 0);
+				} while (startOfRange > 0 || !childSelector.test(current));
 				return true;
 			}
 
@@ -197,4 +229,14 @@ public class ResourceStream {
 		}, Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
 	}
 
+	/**
+	 * Sets a child selector which defines whether a given reosource should be part of the stream 
+	 * 
+	 * @param path
+	 *            set the internal resource to path
+	 * @return this locator
+	 */
+	public void forEach(Consumer<Resource> consumer) {
+		stream().forEach(consumer);
+	}
 }
diff --git a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterDateTest.java b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterDateTest.java
index c8150ba..7eef5a4 100644
--- a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterDateTest.java
+++ b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterDateTest.java
@@ -19,8 +19,6 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.resource.stream.ResourceFilter;
-import org.apache.sling.resource.stream.ResourceStream;
 import org.apache.sling.resource.stream.parser.ParseException;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -97,6 +95,6 @@ public class ResourceFilterDateTest {
 	
 	private List<Resource> handle(String path, String filter) throws ParseException {
 		Resource resource = context.resourceResolver().getResource(path);
-		return ResourceStream.from(resource).stream().filter(new ResourceFilter(filter)).collect(Collectors.toList());
+		return ResourceStream.from(resource).setChildSelector(filter).stream().collect(Collectors.toList());
 	}
 }
diff --git a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterLogicTest.java b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterLogicTest.java
index e34bf5a..183e98f 100644
--- a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterLogicTest.java
+++ b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterLogicTest.java
@@ -19,8 +19,6 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.resource.stream.ResourceFilter;
-import org.apache.sling.resource.stream.ResourceStream;
 import org.apache.sling.resource.stream.parser.ParseException;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -63,6 +61,6 @@ public class ResourceFilterLogicTest {
 	
 	private List<Resource> handle(String path, String filter) throws ParseException {
 		Resource resource = context.resourceResolver().getResource(path);
-		return ResourceStream.from(resource).stream().filter(new ResourceFilter(filter)).collect(Collectors.toList());
+		return ResourceStream.from(resource).setChildSelector(filter).stream().collect(Collectors.toList());
 	}
 }
diff --git a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterTest.java b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterTest.java
index 06deb05..7951a28 100644
--- a/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterTest.java
+++ b/streams/src/test/java/org/apache/sling/resource/stream/ResourceFilterTest.java
@@ -261,6 +261,6 @@ public class ResourceFilterTest {
 	private List<Resource> handle(String path, String filter) throws ParseException {
 		Resource resource = context.resourceResolver().getResource(path);
 		Predicate<Resource> f = new ResourceFilter(filter);
-		return ResourceStream.from(resource).stream().filter(f).collect(Collectors.toList());
+		return ResourceStream.from(resource).setChildSelector(f).stream().collect(Collectors.toList());
 	}
 }

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