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/07/23 18:22:27 UTC

[sling-org-apache-sling-resource-filter] branch master updated: minor javadoc and adding consistency between the ResourceStream and ResourceFilterStream

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-resource-filter.git


The following commit(s) were added to refs/heads/master by this push:
     new ea1da9e  minor javadoc and adding consistency between the ResourceStream and ResourceFilterStream
ea1da9e is described below

commit ea1da9e04ff0365abcb1f08bae51e2f67b3122b8
Author: JE Bailey <je...@apache.org>
AuthorDate: Mon Jul 23 14:22:23 2018 -0400

    minor javadoc and adding consistency between the ResourceStream and
    ResourceFilterStream
---
 .../apache/sling/resource/filter/ResourceFilter.java | 14 +++++++++++---
 .../sling/resource/filter/ResourceFilterStream.java  | 20 +++++++++++++++++++-
 .../apache/sling/resource/filter/ResourceStream.java |  4 ++--
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java b/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
index d7fd5c3..52f9a7d 100644
--- a/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
+++ b/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
@@ -26,7 +26,8 @@ import org.apache.sling.resource.filter.impl.ParseException;
 import org.apache.sling.resource.filter.impl.node.Node;
 
 /**
- * Creates a {@link Predicate} of type {@link Resource} to identify matching Resource objects
+ * Creates a {@link Predicate} of type {@link Resource} to identify matching
+ * Resource objects
  *
  */
 public class ResourceFilter implements Predicate<Resource> {
@@ -35,8 +36,15 @@ public class ResourceFilter implements Predicate<Resource> {
 
     private Context context;
 
-    public ResourceFilter(String filter) throws ParseException {
-        Node rootNode = new FilterParser(new ByteArrayInputStream(filter.getBytes())).parse();
+    /**
+     * Creates a {@link Predicate<Resource>} using the input String as the basis of
+     * the selection
+     * 
+     * @param matchDefinition Script used to define the matching requirements for the Predicate
+     * @throws ParseException
+     */
+    public ResourceFilter(String matchDefinition) throws ParseException {
+        Node rootNode = new FilterParser(new ByteArrayInputStream(matchDefinition.getBytes())).parse();
         this.parsedPredicate = rootNode.accept(getContext().getLogicVisitor());
     }
 
diff --git a/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java b/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
index 6f9ca80..c230133 100644
--- a/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
+++ b/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
@@ -13,7 +13,11 @@
  */
 package org.apache.sling.resource.filter;
 
+import java.util.Spliterator;
+import java.util.Spliterators;
+import java.util.function.Predicate;
 import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.resource.filter.impl.ParseException;
@@ -25,7 +29,8 @@ public class ResourceFilterStream extends ResourceStream {
     }
 
     /**
-     * Predicate used to select child resources for traversal
+     * Creates a Stream<Resource> using the branchSelector to create the traversal
+     * predicate to select the appropriate child resources
      * 
      * @param branchSelector
      *            resourceFilter script for traversal control
@@ -35,5 +40,18 @@ public class ResourceFilterStream extends ResourceStream {
     public Stream<Resource> stream(String branchSelector) throws ParseException {
         return stream(new ResourceFilter(branchSelector));
     }
+    
+    /**
+     * Provides a stream of the child resources of the base resource. The predicate
+     * is a filter to determine which of the children are returned
+     * 
+     * @param childSelector
+     * @return
+     * @throws ParseException 
+     */
+    public Stream<Resource> listChildren(String childSelector) throws ParseException {
+        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(resource.listChildren(),
+                Spliterator.ORDERED | Spliterator.IMMUTABLE), false).filter(new ResourceFilter(childSelector));
+    }
 
 }
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/resource/filter/ResourceStream.java b/src/main/java/org/apache/sling/resource/filter/ResourceStream.java
index 64b3930..67adf17 100644
--- a/src/main/java/org/apache/sling/resource/filter/ResourceStream.java
+++ b/src/main/java/org/apache/sling/resource/filter/ResourceStream.java
@@ -32,7 +32,7 @@ import org.apache.sling.api.resource.Resource;
  */
 public class ResourceStream {
 
-    private Resource resource;
+    protected Resource resource;
 
     public ResourceStream(Resource resource) {
         this.resource = resource;
@@ -99,7 +99,7 @@ public class ResourceStream {
     }
 
     /**
-     * Provides a stream of the child resource of the base resource. The predicate
+     * Provides a stream of the child resources of the base resource. The predicate
      * is a filter to determine which of the children are returned
      * 
      * @param childSelector