polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [07/13] zest-java git commit: spi: EntityFinder returns Stream<EntityReference>
Date Thu, 08 Dec 2016 23:44:16 GMT
spi: EntityFinder returns Stream<EntityReference>


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/2d485e3a
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/2d485e3a
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/2d485e3a

Branch: refs/heads/develop
Commit: 2d485e3ae18dd1e9cac351d780d8b61bc29e03df
Parents: 47cbc7b
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Thu Dec 8 10:32:53 2016 +0100
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Thu Dec 8 10:32:53 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/zest/api/query/Query.java   |   6 +
 .../org/apache/zest/functional/Iterables.java   |  60 ---------
 .../apache/zest/functional/IterablesTest.java   |   9 --
 .../zest/runtime/query/IterableQuerySource.java |  23 ++--
 .../apache/zest/runtime/query/QueryImpl.java    |  24 ++--
 .../runtime/unitofwork/ModuleUnitOfWork.java    |  84 ++++--------
 .../org/apache/zest/spi/query/EntityFinder.java |  17 +--
 .../org/apache/zest/spi/query/QuerySource.java  |  19 +--
 .../test/indexing/AbstractEntityFinderTest.java | 127 +++++++++----------
 .../elasticsearch/ElasticSearchFinder.java      |  30 ++---
 .../zest/index/rdf/query/RdfQueryParser.java    |   3 +-
 .../zest/index/rdf/query/RdfQueryService.java   |  22 ++--
 .../rdf/query/internal/RdfQueryParserImpl.java  |   7 +-
 .../solr/internal/SolrEntityQueryMixin.java     |  25 ++--
 .../index/sql/internal/SQLEntityFinder.java     |  26 ++--
 .../zest/index/sql/support/api/SQLQuerying.java |   2 +-
 .../support/postgresql/PostgreSQLQuerying.java  |   2 +-
 .../support/skeletons/AbstractSQLQuerying.java  |  16 +--
 .../library/rest/admin/EntitiesResource.java    |  50 ++++----
 19 files changed, 226 insertions(+), 326 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/api/src/main/java/org/apache/zest/api/query/Query.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/query/Query.java b/core/api/src/main/java/org/apache/zest/api/query/Query.java
index a64dd03..4e985b8 100644
--- a/core/api/src/main/java/org/apache/zest/api/query/Query.java
+++ b/core/api/src/main/java/org/apache/zest/api/query/Query.java
@@ -20,6 +20,7 @@
 package org.apache.zest.api.query;
 
 import java.io.Serializable;
+import java.util.stream.Stream;
 import org.apache.zest.api.property.Property;
 import org.apache.zest.api.query.grammar.OrderBy;
 
@@ -134,4 +135,9 @@ public interface Query<T>
      */
     long count()
         throws QueryExecutionException;
+
+    /**
+     * @return Stream results
+     */
+    Stream<T> stream();
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java
----------------------------------------------------------------------
diff --git a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java b/core/functional/src/main/java/org/apache/zest/functional/Iterables.java
index ae1d66c..46c26a3 100644
--- a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java
+++ b/core/functional/src/main/java/org/apache/zest/functional/Iterables.java
@@ -19,7 +19,6 @@
  */
 package org.apache.zest.functional;
 
-import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -122,70 +121,11 @@ public final class Iterables
         };
     }
 
-    public static <T> Iterable<T> append( final T item, final Iterable<T> iterable )
-    {
-        return new Iterable<T>()
-        {
-            @Override
-            public Iterator<T> iterator()
-            {
-                final Iterator<T> iterator = iterable.iterator();
-
-                return new Iterator<T>()
-                {
-                    T last = item;
-
-                    @Override
-                    public boolean hasNext()
-                    {
-                        return iterator.hasNext() || last != null;
-                    }
-
-                    @Override
-                    public T next()
-                    {
-                        if( iterator.hasNext() )
-                        {
-                            return iterator.next();
-                        }
-                        else
-                        {
-                            try
-                            {
-                                return last;
-                            }
-                            finally
-                            {
-                                last = null;
-                            }
-                        }
-                    }
-
-                    @Override
-                    public void remove()
-                    {
-                    }
-                };
-            }
-        };
-    }
-
     public static <T> List<T> toList( Iterable<T> iterable )
     {
         return addAll( new ArrayList<>(), iterable );
     }
 
-    @SuppressWarnings( "unchecked" )
-    public static <T> T[] toArray( Class<T> componentType, Iterable<T> iterable )
-    {
-        if( iterable == null )
-        {
-            return null;
-        }
-        List<T> list = toList( iterable );
-        return list.toArray( (T[]) Array.newInstance( componentType, list.size() ) );
-    }
-
     private static class MapIterable<FROM, TO>
         implements Iterable<TO>
     {

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java
----------------------------------------------------------------------
diff --git a/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java b/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java
index 063d067..ffa7d88 100644
--- a/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java
+++ b/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java
@@ -25,9 +25,7 @@ import java.util.List;
 import java.util.function.Function;
 import org.junit.Test;
 
-import static java.util.Collections.emptyList;
 import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
 
 /**
@@ -53,13 +51,6 @@ public class IterablesTest
     }
 
     @Test
-    public void testAppend()
-    {
-        assertThat( Iterables.toList( Iterables.append( "C", Iterables.iterable( "A", "B" ) ) ).toString(),
-                    equalTo( "[A, B, C]" ) );
-    }
-
-    @Test
     public void testMap()
     {
         assertThat( Iterables.toList( Iterables.map( new Function<String, String>()

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/runtime/src/main/java/org/apache/zest/runtime/query/IterableQuerySource.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/query/IterableQuerySource.java b/core/runtime/src/main/java/org/apache/zest/runtime/query/IterableQuerySource.java
index c619f18..3d0020d 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/query/IterableQuerySource.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/query/IterableQuerySource.java
@@ -22,7 +22,6 @@ package org.apache.zest.runtime.query;
 
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
@@ -58,24 +57,20 @@ public class IterableQuerySource
     @Override
     public <T> T find( Class<T> resultType,
                        Predicate<Composite> whereClause,
-                       Iterable<OrderBy> orderBySegments,
+                       List<OrderBy> orderBySegments,
                        Integer firstResult,
                        Integer maxResults,
                        Map<String, Object> variables
     )
     {
-        final Iterator<T> iterator = iterator( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
-        if( iterator.hasNext() )
-        {
-            return iterator.next();
-        }
-        return null;
+        return stream( resultType, whereClause, orderBySegments, firstResult, maxResults, variables )
+            .findFirst().orElse( null );
     }
 
     @Override
     public <T> long count( Class<T> resultType,
                            Predicate<Composite> whereClause,
-                           Iterable<OrderBy> orderBySegments,
+                           List<OrderBy> orderBySegments,
                            Integer firstResult,
                            Integer maxResults,
                            Map<String, Object> variables
@@ -85,21 +80,21 @@ public class IterableQuerySource
     }
 
     @Override
-    public <T> Iterator<T> iterator( Class<T> resultType,
+    public <T> Stream<T> stream( Class<T> resultType,
                                      Predicate<Composite> whereClause,
-                                     Iterable<OrderBy> orderBySegments,
+                                     List<OrderBy> orderBySegments,
                                      Integer firstResult,
                                      Integer maxResults,
                                      Map<String, Object> variables
     )
     {
-        return list( resultType, whereClause, orderBySegments, firstResult, maxResults, variables ).iterator();
+        return list( resultType, whereClause, orderBySegments, firstResult, maxResults, variables ).stream();
     }
 
     @SuppressWarnings( {"raw", "unchecked"} )
     private <T> List<T> list( Class<T> resultType,
                               Predicate<Composite> whereClause,
-                              Iterable<OrderBy> orderBySegments,
+                              List<OrderBy> orderBySegments,
                               Integer firstResult,
                               Integer maxResults,
                               Map<String, Object> variables
@@ -112,7 +107,7 @@ public class IterableQuerySource
         if( orderBySegments != null )
         {
             // Sort it
-            Collections.sort( list, new OrderByComparator( orderBySegments ) );
+            list.sort( new OrderByComparator( orderBySegments ) );
         }
 
         // Cut results

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/runtime/src/main/java/org/apache/zest/runtime/query/QueryImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/query/QueryImpl.java b/core/runtime/src/main/java/org/apache/zest/runtime/query/QueryImpl.java
index cd65f49..4846dae 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/query/QueryImpl.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/query/QueryImpl.java
@@ -19,17 +19,20 @@
  */
 package org.apache.zest.runtime.query;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.property.Property;
 import org.apache.zest.api.query.Query;
 import org.apache.zest.api.query.QueryExecutionException;
 import org.apache.zest.api.query.QueryExpressions;
 import org.apache.zest.api.query.grammar.OrderBy;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.spi.query.QuerySource;
 
 /**
@@ -52,7 +55,7 @@ import org.apache.zest.spi.query.QuerySource;
     /**
      * Order by clause segments.
      */
-    private Iterable<OrderBy> orderBySegments;
+    private List<OrderBy> orderBySegments;
     /**
      * First result to be returned.
      */
@@ -88,7 +91,7 @@ import org.apache.zest.spi.query.QuerySource;
     @Override
     public Query<T> orderBy( final OrderBy... segments )
     {
-        orderBySegments = Iterables.iterable( segments );
+        orderBySegments = Arrays.asList( segments );
         return this;
     }
 
@@ -100,12 +103,9 @@ import org.apache.zest.spi.query.QuerySource;
     {
         if( orderBySegments == null )
         {
-            orderBySegments = Iterables.iterable( new OrderBy( QueryExpressions.property( property ), order ) );
-        }
-        else
-        {
-            orderBySegments = Iterables.append( new OrderBy( QueryExpressions.property( property ), order ), orderBySegments );
+            orderBySegments = new ArrayList<>();
         }
+        orderBySegments.add( new OrderBy( QueryExpressions.property( property ), order ) );
         return this;
     }
 
@@ -195,7 +195,13 @@ import org.apache.zest.spi.query.QuerySource;
     @Override
     public Iterator<T> iterator()
     {
-        return querySource.iterator( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
+        return stream().iterator();
+    }
+
+    @Override
+    public Stream<T> stream()
+    {
+        return querySource.stream( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/ModuleUnitOfWork.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/ModuleUnitOfWork.java b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/ModuleUnitOfWork.java
index 0d1e3bb..4f22d69 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/ModuleUnitOfWork.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/ModuleUnitOfWork.java
@@ -23,13 +23,13 @@ package org.apache.zest.runtime.unitofwork;
 import java.time.Instant;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.apache.zest.api.ZestAPI;
 import org.apache.zest.api.association.AssociationDescriptor;
 import org.apache.zest.api.association.AssociationStateHolder;
@@ -68,7 +68,6 @@ import org.apache.zest.api.usecase.Usecase;
 import org.apache.zest.api.util.NullArgumentException;
 import org.apache.zest.api.value.ValueBuilder;
 import org.apache.zest.api.value.ValueComposite;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.runtime.association.AssociationInstance;
 import org.apache.zest.runtime.association.ManyAssociationInstance;
 import org.apache.zest.runtime.association.NamedAssociationInstance;
@@ -529,7 +528,7 @@ public class ModuleUnitOfWork
         @Override
         public <T> T find( Class<T> resultType,
                            Predicate<Composite> whereClause,
-                           Iterable<OrderBy> orderBySegments,
+                           List<OrderBy> orderBySegments,
                            Integer firstResult,
                            Integer maxResults,
                            Map<String, Object> variables
@@ -566,7 +565,7 @@ public class ModuleUnitOfWork
         @Override
         public <T> long count( Class<T> resultType,
                                Predicate<Composite> whereClause,
-                               Iterable<OrderBy> orderBySegments,
+                               List<OrderBy> orderBySegments,
                                Integer firstResult,
                                Integer maxResults,
                                Map<String, Object> variables
@@ -586,69 +585,42 @@ public class ModuleUnitOfWork
         }
 
         @Override
-        public <T> Iterator<T> iterator( final Class<T> resultType,
-                                         Predicate<Composite> whereClause,
-                                         Iterable<OrderBy> orderBySegments,
-                                         Integer firstResult,
-                                         Integer maxResults,
-                                         Map<String, Object> variables
-        )
+        public <T> Stream<T> stream( Class<T> resultType,
+                                     Predicate<Composite> whereClause,
+                                     List<OrderBy> orderBySegments,
+                                     Integer firstResult,
+                                     Integer maxResults,
+                                     Map<String, Object> variables )
         {
             EntityFinder entityFinder = moduleUnitOfWork.module().instance().findService( EntityFinder.class ).get();
 
             try
             {
-                final Iterator<EntityReference> foundEntities =
-                    entityFinder.findEntities(
-                        resultType,
-                        whereClause,
-                        Iterables.toArray( OrderBy.class, orderBySegments ),
-                        firstResult,
-                        maxResults,
-                        variables == null ? Collections.emptyMap() : variables
-                    ).iterator();
-
-                return new Iterator<T>()
-                {
-                    @Override
-                    public boolean hasNext()
-                    {
-                        return foundEntities.hasNext();
-                    }
-
-                    @Override
-                    public T next()
-                    {
-                        final EntityReference foundEntity = foundEntities.next();
-                        try
-                        {
-                            return moduleUnitOfWork.get( resultType, foundEntity.identity() );
-                        }
-                        catch( NoSuchEntityException e )
-                        {
-                            // Index is out of sync - entity has been removed
-                            return null;
-                        }
-                    }
-
-                    @Override
-                    public void remove()
-                    {
-                        throw new UnsupportedOperationException();
-                    }
-                };
+                return entityFinder.findEntities(
+                    resultType,
+                    whereClause,
+                    orderBySegments,
+                    firstResult,
+                    maxResults,
+                    variables == null ? Collections.emptyMap() : variables
+                ).map( ref ->
+                       {
+                           try
+                           {
+                               return moduleUnitOfWork.get( resultType, ref.identity() );
+                           }
+                           catch( NoSuchEntityException e )
+                           {
+                               // Index is out of sync - entity has been removed
+                               return null;
+                           }
+                       } );
             }
             catch( EntityFinderException e )
             {
                 throw new QueryExecutionException( "Query '" + toString() + "' could not be executed", e );
             }
         }
-
-        @Override
-        public String toString()
-        {
-            return "UnitOfWork( " + moduleUnitOfWork.usecase().name() + " )";
-        }
     }
 
     private class ToValuePropertyMappingFunction

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/spi/src/main/java/org/apache/zest/spi/query/EntityFinder.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/zest/spi/query/EntityFinder.java b/core/spi/src/main/java/org/apache/zest/spi/query/EntityFinder.java
index c0be61b..ebe828f 100644
--- a/core/spi/src/main/java/org/apache/zest/spi/query/EntityFinder.java
+++ b/core/spi/src/main/java/org/apache/zest/spi/query/EntityFinder.java
@@ -19,8 +19,10 @@
  */
 package org.apache.zest.spi.query;
 
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import org.apache.zest.api.common.Optional;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.entity.EntityReference;
@@ -40,16 +42,15 @@ public interface EntityFinder
      * @param firstResult       Index of the first returned entity.
      * @param maxResults        Maximum returned entities.
      * @param variables         Query variables
-     * @return Single entity matching the query criterion.
+     * @return Entities matching the query criterion.
      * @throws EntityFinderException on error
      */
-    Iterable<EntityReference> findEntities( Class<?> resultType,
-                                            @Optional Predicate<Composite> whereClause,
-                                            @Optional OrderBy[] orderBySegments,
-                                            @Optional Integer firstResult,
-                                            @Optional Integer maxResults,
-                                            Map<String, Object> variables
-    )
+    Stream<EntityReference> findEntities( Class<?> resultType,
+                                          @Optional Predicate<Composite> whereClause,
+                                          @Optional List<OrderBy> orderBySegments,
+                                          @Optional Integer firstResult,
+                                          @Optional Integer maxResults,
+                                          Map<String, Object> variables )
         throws EntityFinderException;
 
     /**

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/spi/src/main/java/org/apache/zest/spi/query/QuerySource.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/zest/spi/query/QuerySource.java b/core/spi/src/main/java/org/apache/zest/spi/query/QuerySource.java
index e6bd4b9..8ead1b4 100644
--- a/core/spi/src/main/java/org/apache/zest/spi/query/QuerySource.java
+++ b/core/spi/src/main/java/org/apache/zest/spi/query/QuerySource.java
@@ -19,9 +19,10 @@
  */
 package org.apache.zest.spi.query;
 
-import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.query.grammar.OrderBy;
 
@@ -32,7 +33,7 @@ public interface QuerySource
 {
     <T> T find( Class<T> resultType,
                 Predicate<Composite> whereClause,
-                Iterable<OrderBy> orderBySegments,
+                List<OrderBy> orderBySegments,
                 Integer firstResult,
                 Integer maxResults,
                 Map<String, Object> variables
@@ -40,17 +41,17 @@ public interface QuerySource
 
     <T> long count( Class<T> resultType,
                     Predicate<Composite> whereClause,
-                    Iterable<OrderBy> orderBySegments,
+                    List<OrderBy> orderBySegments,
                     Integer firstResult,
                     Integer maxResults,
                     Map<String, Object> variables
     );
 
-    <T> Iterator<T> iterator( Class<T> resultType,
-                              Predicate<Composite> whereClause,
-                              Iterable<OrderBy> orderBySegments,
-                              Integer firstResult,
-                              Integer maxResults,
-                              Map<String, Object> variables
+    <T> Stream<T> stream( Class<T> resultType,
+                          Predicate<Composite> whereClause,
+                          List<OrderBy> orderBySegments,
+                          Integer firstResult,
+                          Integer maxResults,
+                          Map<String, Object> variables
     );
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/core/testsupport/src/main/java/org/apache/zest/test/indexing/AbstractEntityFinderTest.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/zest/test/indexing/AbstractEntityFinderTest.java b/core/testsupport/src/main/java/org/apache/zest/test/indexing/AbstractEntityFinderTest.java
index a7af4c3..2fae4d5 100644
--- a/core/testsupport/src/main/java/org/apache/zest/test/indexing/AbstractEntityFinderTest.java
+++ b/core/testsupport/src/main/java/org/apache/zest/test/indexing/AbstractEntityFinderTest.java
@@ -27,7 +27,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
-import java.util.stream.StreamSupport;
+import java.util.stream.Stream;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.Structure;
@@ -70,6 +70,7 @@ public abstract class AbstractEntityFinderTest
     private static final Predicate<Composite> ALL = null;
 
     private static final OrderBy[] NO_SORTING = null;
+    private static final List<OrderBy> NO_SORTING2 = null;
 
     private static final Integer NO_FIRST_RESULT = null;
 
@@ -115,9 +116,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             ALL,
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE, JACK, ANN );
     }
 
@@ -129,9 +130,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Domain.class,
             eq( nameable.name(), "Gaming" ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, "Gaming" );
     }
 
@@ -142,9 +143,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Nameable.class,
             ALL,
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, NameableAssert.allNames() );
     }
 
@@ -156,9 +157,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             eq( person.placeOfBirth().get().name(), "Kuala Lumpur" ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE, ANN );
     }
 
@@ -170,9 +171,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             eq( person.mother().get().placeOfBirth().get().name(), "Kuala Lumpur" ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE );
     }
 
@@ -184,9 +185,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             ge( person.yearOfBirth(), 1973 ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE, ANN );
     }
 
@@ -199,9 +200,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Nameable.class,
             and( ge( person.yearOfBirth(), 1900 ), eq( person.placeOfBirth().get().name(), "Penang" ) ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JACK );
     }
 
@@ -214,9 +215,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             or( eq( person.yearOfBirth(), 1970 ), eq( person.yearOfBirth(), 1975 ) ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JACK, ANN );
     }
 
@@ -229,9 +230,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Female.class,
             or( eq( person.yearOfBirth(), 1970 ), eq( person.yearOfBirth(), 1975 ) ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, ANN );
     }
 
@@ -243,9 +244,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             not( eq( person.yearOfBirth(), 1975 ) ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE, JACK );
     }
 
@@ -257,9 +258,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             isNotNull( person.email() ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE );
     }
 
@@ -271,9 +272,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             isNull( person.email() ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, ANN, JACK );
     }
 
@@ -285,9 +286,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             isNotNull( person.wife() ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JACK );
     }
 
@@ -299,9 +300,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Male.class,
             isNull( person.wife() ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JOE );
     }
 
@@ -313,9 +314,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             isNull( person.wife() ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, ANN, JOE );
     }
 
@@ -323,26 +324,26 @@ public abstract class AbstractEntityFinderTest
     public void script16()
     {
         // should return only 2 entities
-        final List<EntityReference> references = StreamSupport.stream(
-            entityFinder.findEntities( Nameable.class,
-                                       ALL,
-                                       NO_SORTING,
-                                       NO_FIRST_RESULT, 2,
-                                       NO_VARIABLES ).spliterator(), false ).collect( toList() );
-        assertEquals( "2 identitities", 2, references.size() );
+        Stream<EntityReference> references = entityFinder.findEntities(
+            Nameable.class,
+            ALL,
+            NO_SORTING2,
+            NO_FIRST_RESULT, 2,
+            NO_VARIABLES );
+        assertEquals( "2 identities", 2, references.count() );
     }
 
     @Test
     public void script17()
     {
         // should return only 2 entities starting with third one
-        final List<EntityReference> references = StreamSupport.stream(
-            entityFinder.findEntities( Nameable.class,
-                                       ALL,
-                                       NO_SORTING,
-                                       3, 2,
-                                       NO_VARIABLES ).spliterator(), false ).collect( toList() );
-        assertEquals( "2 identitities", 2, references.size() );
+        Stream<EntityReference> references = entityFinder.findEntities(
+            Nameable.class,
+            ALL,
+            NO_SORTING2,
+            3, 2,
+            NO_VARIABLES );
+        assertEquals( "2 identitities", 2, references.count() );
     }
 
     @Test
@@ -356,12 +357,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Nameable.class,
             ALL,
-            new OrderBy[]
-        {
-            orderBy( nameable.name() )
-            },
+            Arrays.asList(orderBy( nameable.name() )),
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( false, entities, allNames );
     }
 
@@ -382,12 +380,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Nameable.class,
             gt( nameable.name(), "B" ),
-            new OrderBy[]
-        {
-            orderBy( nameable.name() )
-            },
+            Arrays.asList( orderBy( nameable.name() ) ),
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( false, entities, largerThanB.toArray( new String[ largerThanB.size() ] ) );
     }
 
@@ -399,12 +394,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             gt( person.yearOfBirth(), 1973 ),
-            new OrderBy[]
-        {
-            orderBy( person.name(), OrderBy.Order.DESCENDING )
-            },
+            Arrays.asList( orderBy( person.name(), OrderBy.Order.DESCENDING ) ),
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( false, entities, JOE, ANN );
     }
 
@@ -416,12 +408,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Person.class,
             ALL,
-            new OrderBy[]
-        {
-            orderBy( person.placeOfBirth().get().name() ), orderBy( person.name() )
-            },
+            Arrays.asList( orderBy( person.placeOfBirth().get().name() ), orderBy( person.name() ) ),
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( false, entities, ANN, JOE, JACK );
     }
 
@@ -433,9 +422,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Nameable.class,
             matches( nameable.name(), "J.*Doe" ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            NO_VARIABLES );
+            NO_VARIABLES ).collect( toList() );
         assertNames( entities, JACK, JOE );
     }
 
@@ -449,9 +438,9 @@ public abstract class AbstractEntityFinderTest
         Iterable<EntityReference> entities = entityFinder.findEntities(
             Domain.class,
             eq( nameable.name(), variable( "domain" ) ),
-            NO_SORTING,
+            NO_SORTING2,
             NO_FIRST_RESULT, NO_MAX_RESULTS,
-            variables );
+            variables ).collect( toList() );
         assertNames( entities, "Gaming" );
     }
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-elasticsearch/src/main/java/org/apache/zest/index/elasticsearch/ElasticSearchFinder.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-elasticsearch/src/main/java/org/apache/zest/index/elasticsearch/ElasticSearchFinder.java b/extensions/indexing-elasticsearch/src/main/java/org/apache/zest/index/elasticsearch/ElasticSearchFinder.java
index ebec4bd..ff568d3 100644
--- a/extensions/indexing-elasticsearch/src/main/java/org/apache/zest/index/elasticsearch/ElasticSearchFinder.java
+++ b/extensions/indexing-elasticsearch/src/main/java/org/apache/zest/index/elasticsearch/ElasticSearchFinder.java
@@ -21,8 +21,11 @@ package org.apache.zest.index.elasticsearch;
 
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.This;
@@ -51,7 +54,6 @@ import org.apache.zest.api.query.grammar.PropertyNotNullPredicate;
 import org.apache.zest.api.query.grammar.PropertyNullPredicate;
 import org.apache.zest.api.query.grammar.QuerySpecification;
 import org.apache.zest.api.value.ValueComposite;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.index.elasticsearch.ElasticSearchFinderSupport.ComplexTypeSupport;
 import org.apache.zest.spi.query.EntityFinder;
 import org.apache.zest.spi.query.EntityFinderException;
@@ -59,7 +61,6 @@ import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.index.query.BoolQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilder;
-import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.sort.SortOrder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -87,12 +88,12 @@ public interface ElasticSearchFinder
         private ElasticSearchSupport support;
 
         @Override
-        public Iterable<EntityReference> findEntities( Class<?> resultType,
-                                                       Predicate<Composite> whereClause,
-                                                       OrderBy[] orderBySegments,
-                                                       Integer firstResult, Integer maxResults,
-                                                       Map<String, Object> variables )
-            throws EntityFinderException
+        public Stream<EntityReference> findEntities( Class<?> resultType,
+                                                     Predicate<Composite> whereClause,
+                                                     List<OrderBy> orderBySegments,
+                                                     Integer firstResult,
+                                                     Integer maxResults,
+                                                     Map<String, Object> variables ) throws EntityFinderException
         {
             // Prepare request
             SearchRequestBuilder request = support.client().prepareSearch( support.index() );
@@ -128,7 +129,8 @@ public interface ElasticSearchFinder
             // Execute
             SearchResponse response = request.execute().actionGet();
 
-            return Iterables.map( (SearchHit from) -> EntityReference.parseEntityReference( from.id() ), response.getHits() );
+            return StreamSupport.stream( response.getHits().spliterator(), false )
+                                .map( hit -> EntityReference.parseEntityReference( hit.id() ) );
         }
 
         @Override
@@ -167,12 +169,12 @@ public interface ElasticSearchFinder
             throws EntityFinderException
         {
             // Prepare request
-            SearchRequestBuilder request = support.client().prepareSearch(support.index()).setSize(0);
+            SearchRequestBuilder request = support.client().prepareSearch( support.index() ).setSize( 0 );
 
             BoolQueryBuilder baseQueryBuilder = baseQuery( resultType );
             QueryBuilder whereQueryBuilder = processWhereSpecification( baseQueryBuilder, whereClause, variables );
 
-            request.setQuery( boolQuery().must(whereQueryBuilder).filter(baseQueryBuilder) );
+            request.setQuery( boolQuery().must( whereQueryBuilder ).filter( baseQueryBuilder ) );
 
             // Log
             LOGGER.debug( "Will count Entities: {}", request );
@@ -272,14 +274,13 @@ public interface ElasticSearchFinder
 
                 NamedAssociationContainsPredicate<?> namedAssContSpec = (NamedAssociationContainsPredicate) spec;
                 processNamedAssociationContainsSpecification( queryBuilder, namedAssContSpec, variables );
-
             }
             else if( spec instanceof NamedAssociationContainsNamePredicate )
             {
 
-                NamedAssociationContainsNamePredicate<?> namedAssContNameSpec = (NamedAssociationContainsNamePredicate) spec;
+                NamedAssociationContainsNamePredicate<?> namedAssContNameSpec
+                    = (NamedAssociationContainsNamePredicate) spec;
                 processNamedAssociationContainsNameSpecification( queryBuilder, namedAssContNameSpec, variables );
-
             }
             else
             {
@@ -519,5 +520,4 @@ public interface ElasticSearchFinder
             queryBuilder.must( termQuery( name, value ) );
         }
     }
-
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryParser.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryParser.java b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryParser.java
index b9d9806..b8236f0 100644
--- a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryParser.java
+++ b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryParser.java
@@ -20,6 +20,7 @@
 
 package org.apache.zest.index.rdf.query;
 
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
 import org.apache.zest.api.composite.Composite;
@@ -29,7 +30,7 @@ public interface RdfQueryParser
 {
     String constructQuery( Class<?> resultType,
                            Predicate<Composite> whereClause,
-                           OrderBy[] orderBySegments,
+                           List<OrderBy> orderBySegments,
                            Integer firstResult,
                            Integer maxResults,
                            Map<String, Object> variables

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryService.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryService.java b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryService.java
index b0c3341..f8c08e6 100644
--- a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryService.java
+++ b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/RdfQueryService.java
@@ -20,9 +20,10 @@
 
 package org.apache.zest.index.rdf.query;
 
+import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
-import org.openrdf.query.QueryLanguage;
+import java.util.stream.Stream;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.Service;
@@ -33,6 +34,7 @@ import org.apache.zest.api.query.grammar.QuerySpecification;
 import org.apache.zest.api.service.ServiceComposite;
 import org.apache.zest.spi.query.EntityFinder;
 import org.apache.zest.spi.query.EntityFinderException;
+import org.openrdf.query.QueryLanguage;
 
 /**
  * JAVADOC Add JavaDoc
@@ -57,14 +59,12 @@ public interface RdfQueryService
         TupleQueryExecutor tupleExecutor;
 
         @Override
-        public Iterable<EntityReference> findEntities( Class<?> resultType,
-                                                       Predicate<Composite> whereClause,
-                                                       OrderBy[] orderBySegments,
-                                                       Integer firstResult,
-                                                       Integer maxResults,
-                                                       Map<String, Object> variables
-        )
-            throws EntityFinderException
+        public Stream<EntityReference> findEntities( Class<?> resultType,
+                                                     Predicate<Composite> whereClause,
+                                                     List<OrderBy> orderBySegments,
+                                                     Integer firstResult,
+                                                     Integer maxResults,
+                                                     Map<String, Object> variables ) throws EntityFinderException
         {
             CollectingQualifiedIdentityResultCallback collectingCallback = new CollectingQualifiedIdentityResultCallback();
 
@@ -72,7 +72,7 @@ public interface RdfQueryService
             {
                 String query = ((QuerySpecification)whereClause).query();
                 tupleExecutor.performTupleQuery( QueryLanguage.SERQL, query, variables, collectingCallback );
-                return collectingCallback.entities();
+                return collectingCallback.entities().stream();
 
             } else
             {
@@ -80,7 +80,7 @@ public interface RdfQueryService
                 String query = rdfQueryParser.constructQuery( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
 
                 tupleExecutor.performTupleQuery( language, query, variables, collectingCallback );
-                return collectingCallback.entities();
+                return collectingCallback.entities().stream();
             }
         }
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/internal/RdfQueryParserImpl.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/internal/RdfQueryParserImpl.java b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/internal/RdfQueryParserImpl.java
index 8163237..d665c9d 100644
--- a/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/internal/RdfQueryParserImpl.java
+++ b/extensions/indexing-rdf/src/main/java/org/apache/zest/index/rdf/query/internal/RdfQueryParserImpl.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Predicate;
@@ -99,7 +100,7 @@ public class RdfQueryParserImpl
     @Override
     public String constructQuery( final Class<?> resultType,
                                   final Predicate<Composite> specification,
-                                  final OrderBy[] orderBySegments,
+                                  final List<OrderBy> orderBySegments,
                                   final Integer firstResult,
                                   final Integer maxResults,
                                   final Map<String, Object> variables
@@ -497,9 +498,9 @@ public class RdfQueryParserImpl
         }
     }
 
-    private void processOrderBy( OrderBy[] orderBySegments, StringBuilder builder )
+    private void processOrderBy( List<OrderBy> orderBySegments, StringBuilder builder )
     {
-        if( orderBySegments != null && orderBySegments.length > 0 )
+        if( orderBySegments != null && orderBySegments.size() > 0 )
         {
             for( OrderBy orderBySegment : orderBySegments )
             {

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-solr/src/main/java/org/apache/zest/index/solr/internal/SolrEntityQueryMixin.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-solr/src/main/java/org/apache/zest/index/solr/internal/SolrEntityQueryMixin.java b/extensions/indexing-solr/src/main/java/org/apache/zest/index/solr/internal/SolrEntityQueryMixin.java
index db57c6f..ef26c04 100644
--- a/extensions/indexing-solr/src/main/java/org/apache/zest/index/solr/internal/SolrEntityQueryMixin.java
+++ b/extensions/indexing-solr/src/main/java/org/apache/zest/index/solr/internal/SolrEntityQueryMixin.java
@@ -22,10 +22,10 @@ package org.apache.zest.index.solr.internal;
 
 import java.lang.reflect.Member;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import org.apache.solr.client.solrj.SolrServer;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.response.QueryResponse;
@@ -39,7 +39,6 @@ import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.Service;
 import org.apache.zest.api.query.grammar.OrderBy;
 import org.apache.zest.api.query.grammar.QuerySpecification;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.index.solr.EmbeddedSolrService;
 import org.apache.zest.index.solr.SolrSearch;
 import org.apache.zest.spi.query.EntityFinder;
@@ -59,7 +58,12 @@ public class SolrEntityQueryMixin
     private Logger logger = LoggerFactory.getLogger( SolrEntityQueryMixin.class );
 
     @Override
-    public Iterable<EntityReference> findEntities( Class<?> resultType, @Optional Predicate<Composite> whereClause, @Optional OrderBy[] orderBySegments, @Optional Integer firstResult, @Optional Integer maxResults, Map<String, Object> variables ) throws EntityFinderException
+    public Stream<EntityReference> findEntities( Class<?> resultType,
+                                                 Predicate<Composite> whereClause,
+                                                 List<OrderBy> orderBySegments,
+                                                 Integer firstResult,
+                                                 Integer maxResults,
+                                                 Map<String, Object> variables ) throws EntityFinderException
     {
         try
         {
@@ -73,7 +77,7 @@ public class SolrEntityQueryMixin
             list.add( "rows", maxResults != 0 ? maxResults : 10000 );
             list.add( "start", firstResult );
 
-            if( orderBySegments != null && orderBySegments.length > 0 )
+            if( orderBySegments != null && orderBySegments.size() > 0 )
             {
                 for( OrderBy orderBySegment : orderBySegments )
                 {
@@ -96,7 +100,7 @@ public class SolrEntityQueryMixin
             {
                 references.add( EntityReference.parseEntityReference( result.getFirstValue( "id" ).toString() ) );
             }
-            return references;
+            return references.stream();
 
         } catch( SolrServerException e )
         {
@@ -107,18 +111,15 @@ public class SolrEntityQueryMixin
     @Override
     public EntityReference findEntity( Class<?> resultType, @Optional Predicate<Composite> whereClause, Map<String, Object> variables ) throws EntityFinderException
     {
-        Iterator<EntityReference> iter = findEntities( resultType, whereClause, null, 0, 1, variables ).iterator();
-
-        if( iter.hasNext() )
-            return iter.next();
-        else
-            return null;
+        return findEntities( resultType, whereClause, null, 0, 1, variables )
+            .findFirst().orElse( null );
     }
 
     @Override
     public long countEntities( Class<?> resultType, @Optional Predicate<Composite> whereClause, Map<String, Object> variables ) throws EntityFinderException
     {
-        return Iterables.count( findEntities( resultType, whereClause, null, 0, 1, variables ) );
+        return findEntities( resultType, whereClause, null, 0, 1, variables )
+            .count();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java
index 54e3baf..db2dd3a 100644
--- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java
+++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java
@@ -27,6 +27,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import javax.sql.DataSource;
 import org.apache.zest.api.common.Optional;
 import org.apache.zest.api.composite.Composite;
@@ -93,17 +94,15 @@ public class SQLEntityFinder
     }
 
     @Override
-    public Iterable<EntityReference> findEntities( Class<?> resultType,
-                                                   @Optional Predicate<Composite> whereClause,
-                                                   @Optional OrderBy[] orderBySegments,
-                                                   @Optional final Integer firstResult,
-                                                   @Optional final Integer maxResults,
-                                                   Map<String, Object> variables )
-        throws EntityFinderException
+    public Stream<EntityReference> findEntities( Class<?> resultType,
+                                                 Predicate<Composite> whereClause,
+                                                 List<OrderBy> orderBySegments,
+                                                 Integer firstResult,
+                                                 Integer maxResults,
+                                                 Map<String, Object> variables ) throws EntityFinderException
     {
         // TODO what is Zest's policy on negative firstResult and/or maxResults? JDBC has its own way of interpreting
         // these values - does it match with Zest's way?
-        Iterable<EntityReference> result;
         if( maxResults == null || maxResults > 0 )
         {
             final List<Object> values = new ArrayList<>();
@@ -111,10 +110,10 @@ public class SQLEntityFinder
             final String query = this.parser.constructQuery( resultType, whereClause, orderBySegments, firstResult,
                                                              maxResults, variables, values, valueSQLTypes, false );
 
-            result = this.performQuery( new DoQuery<Iterable<EntityReference>>()
+            List<EntityReference> result = this.performQuery( new DoQuery<List<EntityReference>>()
             {
                 @Override
-                public Iterable<EntityReference> doIt( Connection connection )
+                public List<EntityReference> doIt( Connection connection )
                     throws SQLException
                 {
                     PreparedStatement ps = null;
@@ -148,16 +147,13 @@ public class SQLEntityFinder
 
                     return resultList;
                 }
-
             } );
-
+            return result.stream();
         }
         else
         {
-            result = new ArrayList<>( 0 );
+            return Stream.of();
         }
-
-        return result;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/api/SQLQuerying.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/api/SQLQuerying.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/api/SQLQuerying.java
index 694b08e..13ce4fe 100644
--- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/api/SQLQuerying.java
+++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/api/SQLQuerying.java
@@ -56,7 +56,7 @@ public interface SQLQuerying
     public String constructQuery( //
         Class<?> resultType, //
         @Optional Predicate<Composite> whereClause, //
-        @Optional OrderBy[] orderBySegments, //
+        @Optional List<OrderBy> orderBySegments, //
         @Optional Integer firstResult, //
         @Optional Integer maxResults, //
         Map<String, Object> variables,

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLQuerying.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLQuerying.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLQuerying.java
index b83c928..7fe6514 100644
--- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLQuerying.java
+++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLQuerying.java
@@ -38,7 +38,7 @@ public class PostgreSQLQuerying
             SQLVendor sqlVendor, QuerySpecificationBuilder specBuilder,
             Class<?> resultType,
             Predicate<Composite> whereClause,
-            OrderBy[] orderBySegments,
+            List<OrderBy> orderBySegments,
             Integer firstResult,
             Integer maxResults,
             Map<String, Object> variables,

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLQuerying.java
----------------------------------------------------------------------
diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLQuerying.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLQuerying.java
index 38ba1f8..587bf30 100644
--- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLQuerying.java
+++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLQuerying.java
@@ -779,7 +779,7 @@ public abstract class AbstractSQLQuerying
     @Override
     public String constructQuery( Class<?> resultType, //
                                   Predicate<Composite> whereClause, //
-                                  OrderBy[] orderBySegments, //
+                                  List<OrderBy> orderBySegments, //
                                   Integer firstResult, //
                                   Integer maxResults, //
                                   Map<String, Object> variables, //
@@ -849,7 +849,7 @@ public abstract class AbstractSQLQuerying
         SQLVendor sqlVendor, QuerySpecificationBuilder specBuilder,
         Class<?> resultType,
         Predicate<Composite> whereClause,
-        OrderBy[] orderBySegments,
+        List<OrderBy> orderBySegments,
         Integer firstResult,
         Integer maxResults,
         Map<String, Object> variables,
@@ -1454,23 +1454,23 @@ public abstract class AbstractSQLQuerying
         return javaRegexp;
     }
 
-    protected void processOrderBySegments( OrderBy[] orderBy, SQLVendor vendor,
+    protected void processOrderBySegments( List<OrderBy> orderBy, SQLVendor vendor,
                                            QuerySpecificationBuilder builder
     )
     {
         if( orderBy != null )
         {
-            QNameInfo[] qNames = new QNameInfo[ orderBy.length ];
+            QNameInfo[] qNames = new QNameInfo[ orderBy.size() ];
 
             QueryFactory q = vendor.getQueryFactory();
             ColumnsFactory c = vendor.getColumnsFactory();
 
             Integer tableIndex = 0;
-            for( Integer idx = 0; idx < orderBy.length; ++idx )
+            for( Integer idx = 0; idx < orderBy.size(); ++idx )
             {
-                if( orderBy[ idx ] != null )
+                if( orderBy.get( idx ) != null )
                 {
-                    PropertyFunction<?> ref = orderBy[ idx ].property();
+                    PropertyFunction<?> ref = orderBy.get( idx ).property();
                     QualifiedName qName = QualifiedName.fromAccessor( ref.accessor() );
                     QNameInfo info = this._state.qNameInfos().get().get( qName );
                     qNames[ idx ] = info;
@@ -1496,7 +1496,7 @@ public abstract class AbstractSQLQuerying
                         tableIdx = tableIndex;
                     }
                     Ordering ordering = Ordering.ASCENDING;
-                    if( orderBy[ idx ].order() == Order.DESCENDING )
+                    if( orderBy.get( idx ).order() == Order.DESCENDING )
                     {
                         ordering = Ordering.DESCENDING;
                     }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/2d485e3a/libraries/rest/src/main/java/org/apache/zest/library/rest/admin/EntitiesResource.java
----------------------------------------------------------------------
diff --git a/libraries/rest/src/main/java/org/apache/zest/library/rest/admin/EntitiesResource.java b/libraries/rest/src/main/java/org/apache/zest/library/rest/admin/EntitiesResource.java
index 22bff3e..f379a78 100644
--- a/libraries/rest/src/main/java/org/apache/zest/library/rest/admin/EntitiesResource.java
+++ b/libraries/rest/src/main/java/org/apache/zest/library/rest/admin/EntitiesResource.java
@@ -27,6 +27,7 @@ import java.io.Writer;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.stream.Stream;
 import org.apache.zest.api.entity.EntityComposite;
 import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.Service;
@@ -48,6 +49,8 @@ import org.restlet.representation.WriterRepresentation;
 import org.restlet.resource.ResourceException;
 import org.restlet.resource.ServerResource;
 
+import static java.util.stream.Collectors.toList;
+
 /**
  * Listing of all Entities.
  * <p>
@@ -106,7 +109,8 @@ public class EntitiesResource
     {
         try
         {
-            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap() );
+            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.emptyMap() )
+                                                                .collect( toList() );
             return new OutputRepresentation( MediaType.APPLICATION_JSON )
             {
                 @Override
@@ -128,7 +132,7 @@ public class EntitiesResource
     {
         try
         {
-            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap() );
+            final Stream<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.emptyMap() );
 
             WriterRepresentation representation = new WriterRepresentation( MediaType.APPLICATION_RDF_XML )
             {
@@ -141,11 +145,9 @@ public class EntitiesResource
                                  + "\txmlns=\"urn:zest:\"\n" + "\txmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n"
                                  + "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"
                                  + "\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">" );
-                    for( EntityReference qualifiedIdentity : query )
-                    {
-                        out.println( "<zest:entity rdf:about=\"" + getRequest().getResourceRef().getPath() + "/"
-                                     + qualifiedIdentity.identity() + ".rdf\"/>" );
-                    }
+                    query.forEach( qualifiedIdentity -> out.println( "<zest:entity rdf:about=\""
+                                                                     + getRequest().getResourceRef().getPath() + "/"
+                                                                     + qualifiedIdentity.identity() + ".rdf\"/>" ) );
 
                     out.println( "</rdf:RDF>" );
                 }
@@ -165,7 +167,7 @@ public class EntitiesResource
     {
         try
         {
-            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap() );
+            Stream<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.emptyMap() );
             Representation representation = new WriterRepresentation( MediaType.TEXT_HTML )
             {
                 @Override
@@ -175,12 +177,9 @@ public class EntitiesResource
                     PrintWriter out = new PrintWriter( buf );
                     out.println( "<html><head><title>All entities</title></head><body><h1>All entities</h1><ul>" );
 
-                    for( EntityReference entity : query )
-                    {
-                        out.println( "<li><a href=\""
-                                     + getRequest().getResourceRef().clone().addSegment( entity.identity() + ".html" )
-                                     + "\">" + entity.identity() + "</a></li>" );
-                    }
+                    query.forEach( entity -> out.println( "<li><a href=\""
+                                                          + getRequest().getResourceRef().clone().addSegment( entity.identity() + ".html" )
+                                                          + "\">" + entity.identity() + "</a></li>" ) );
                     out.println( "</ul></body></html>" );
                 }
             };
@@ -201,17 +200,18 @@ public class EntitiesResource
             Feed feed = new Feed();
             feed.setTitle( new Text( MediaType.TEXT_PLAIN, "All entities" ) );
             List<Entry> entries = feed.getEntries();
-            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap() );
-            for( EntityReference entityReference : query )
-            {
-                Entry entry = new Entry();
-                entry.setTitle( new Text( MediaType.TEXT_PLAIN, entityReference.toString() ) );
-                Link link = new Link();
-                link.setHref( getRequest().getResourceRef().clone().addSegment( entityReference.identity().toString() ) );
-                entry.getLinks().add( link );
-                entries.add( entry );
-            }
-
+            entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.emptyMap() )
+                        .forEach(
+                            entityReference ->
+                            {
+                                Entry entry = new Entry();
+                                entry.setTitle( new Text( MediaType.TEXT_PLAIN, entityReference.toString() ) );
+                                Link link = new Link();
+                                link.setHref( getRequest().getResourceRef().clone()
+                                                          .addSegment( entityReference.identity().toString() ) );
+                                entry.getLinks().add( link );
+                                entries.add( entry );
+                            } );
             return feed;
         }
         catch( Exception e )


Mime
View raw message