You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by pa...@apache.org on 2016/12/08 23:44:14 UTC

[05/13] zest-java git commit: api: ServiceFinder return streams

api: ServiceFinder return streams


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

Branch: refs/heads/develop
Commit: f8cd4f58e906b17b662a69704ca317654f15c1e5
Parents: 401f70f
Author: Paul Merlin <pa...@apache.org>
Authored: Wed Dec 7 09:41:59 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Wed Dec 7 09:41:59 2016 +0100

----------------------------------------------------------------------
 .../apache/zest/api/service/ServiceFinder.java  |  9 +--
 .../importer/ServiceInstanceImporter.java       | 20 ++-----
 .../importer/ServiceSelectorImporter.java       | 32 ++++------
 .../api/service/qualifier/ServiceQualifier.java | 14 -----
 .../ServiceInjectionProviderFactory.java        | 28 ++++-----
 .../zest/runtime/structure/ModuleInstance.java  | 18 +++---
 .../apache/zest/runtime/mixin/JDKMixinTest.java | 28 ++++-----
 .../zest/runtime/service/PassivationTest.java   | 63 +++++++++++---------
 .../runtime/service/ServiceIdSelectorTest.java  | 10 ++--
 .../structure/TypeToCompositeLookupTest.java    | 21 +++----
 .../zest/library/http/JettyServiceTest.java     |  4 +-
 .../library/jmx/ApplicationManagerService.java  | 22 ++++---
 .../zest/library/osgi/OSGiEnabledService.java   | 19 +++---
 .../internal/service/ServiceLocator.java        | 14 ++---
 .../spring/importer/ZestImportServiceTest.java  | 18 +++---
 .../zest/sample/rental/web/QuikitServlet.java   | 16 ++---
 16 files changed, 154 insertions(+), 182 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java
index abc6d86..f4290f9 100644
--- a/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java
+++ b/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java
@@ -21,6 +21,7 @@
 package org.apache.zest.api.service;
 
 import java.lang.reflect.Type;
+import java.util.stream.Stream;
 
 /**
  * Interface used to query for ServiceReferences.
@@ -74,9 +75,9 @@ public interface ServiceFinder
      *
      * @param serviceType the type that the Services must implement
      *
-     * @return an iterable of ServiceReferences for the given type. It is empty if none exist
+     * @return a stream of ServiceReferences for the given type. It is empty if none exist
      */
-    <T> Iterable<ServiceReference<T>> findServices( Class<T> serviceType );
+    <T> Stream<ServiceReference<T>> findServices( Class<T> serviceType );
 
     /**
      * Find ServiceReferences that implements the given type.
@@ -87,7 +88,7 @@ public interface ServiceFinder
      *
      * @param serviceType the type that the Services must implement
      *
-     * @return an iterable of ServiceReferences for the given type. It is empty if none exist
+     * @return a stream of ServiceReferences for the given type. It is empty if none exist
      */
-    <T> Iterable<ServiceReference<T>> findServices( Type serviceType );
+    <T> Stream<ServiceReference<T>> findServices( Type serviceType );
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java
index 8f4c37f..01302d0 100644
--- a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java
+++ b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java
@@ -67,21 +67,13 @@ public class ServiceInstanceImporter<T>
     {
         if( service == null )
         {
-            for( ServiceReference<ServiceImporter> reference : finder.findServices( ServiceImporter.class ) )
-            {
-                if( reference.identity().equals( serviceId ) )
-                {
-                    service = reference.get();
-                    break;
-                }
-            }
+            service = finder.findServices( ServiceImporter.class )
+                            .filter( ref -> ref.identity().equals( serviceId ) )
+                            .findFirst().map( ServiceReference::get )
+                            .orElseThrow( () -> new ServiceImporterException(
+                                "No service importer with id '" + serviceId + "' was found" )
+                            );
         }
-
-        if( service == null )
-        {
-            throw new ServiceImporterException( "No service importer with id '" + serviceId + "' was found" );
-        }
-
         return service;
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java
index 30ad631..f2d5de6 100644
--- a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java
+++ b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java
@@ -20,9 +20,8 @@
 
 package org.apache.zest.api.service.importer;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.function.Predicate;
+import java.util.stream.Stream;
 import org.apache.zest.api.injection.scope.Structure;
 import org.apache.zest.api.service.Availability;
 import org.apache.zest.api.service.ImportedServiceDescriptor;
@@ -30,7 +29,6 @@ import org.apache.zest.api.service.ServiceFinder;
 import org.apache.zest.api.service.ServiceImporter;
 import org.apache.zest.api.service.ServiceImporterException;
 import org.apache.zest.api.service.ServiceReference;
-import org.apache.zest.api.service.qualifier.ServiceQualifier;
 
 /**
  * If several services are available with a given type, and you want to constrain
@@ -54,25 +52,19 @@ public final class ServiceSelectorImporter<T>
     {
         Predicate<ServiceReference<?>> selector = serviceDescriptor.metaInfo( Predicate.class );
         Class serviceType = serviceDescriptor.types().findFirst().orElse( null );
-        Iterable<ServiceReference<T>> services = locator.findServices( serviceType );
-        List<ServiceReference<T>> filteredServices = new ArrayList<>();
-        for( ServiceReference<T> service : services )
-        {
-            Predicate selector1 = service.metaInfo( Predicate.class );
-            if( selector1 != null && selector1 == selector )
-            {
-                continue;
-            }
 
-            filteredServices.add( service );
-        }
-        T service = ServiceQualifier.firstService( selector, filteredServices );
-        if( service == null )
+        Stream<ServiceReference<T>> services = locator.findServices( serviceType );
+        Predicate<ServiceReference<T>> filter = ref ->
         {
-            throw new ServiceImporterException( "Could not find any service to import that matches the given specification for " + serviceDescriptor
-                .identity() );
-        }
-        return service;
+            Predicate selector1 = ref.metaInfo( Predicate.class );
+            return selector1 == null || selector == selector1;
+        };
+        return services.filter( filter.and( selector ) )
+                       .findFirst().map( ServiceReference::get )
+                       .orElseThrow(
+                           () -> new ServiceImporterException(
+                               "Could not find any service to import that matches the given specification for "
+                               + serviceDescriptor.identity() ) );
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java
index 1423d66..74f9691 100644
--- a/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java
+++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java
@@ -48,20 +48,6 @@ import org.apache.zest.api.service.ServiceReference;
  */
 public abstract class ServiceQualifier
 {
-    public static <T> T firstService( Predicate<ServiceReference<?>> qualifier,
-                                      Iterable<ServiceReference<T>> services
-    )
-    {
-        for( ServiceReference<T> service : services )
-        {
-            if( qualifier.test( service ) )
-            {
-                return service.get();
-            }
-        }
-        return null;
-    }
-
     public static Predicate<ServiceReference<?>> withId( final String anId )
     {
         return new Predicate<ServiceReference<?>>()

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/main/java/org/apache/zest/runtime/injection/provider/ServiceInjectionProviderFactory.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/injection/provider/ServiceInjectionProviderFactory.java b/core/runtime/src/main/java/org/apache/zest/runtime/injection/provider/ServiceInjectionProviderFactory.java
index 85817d2..69f37ec 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/injection/provider/ServiceInjectionProviderFactory.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/injection/provider/ServiceInjectionProviderFactory.java
@@ -23,6 +23,7 @@ package org.apache.zest.runtime.injection.provider;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
 import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.stream.Stream;
@@ -31,13 +32,13 @@ import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.api.service.qualifier.Qualifier;
 import org.apache.zest.api.util.Classes;
 import org.apache.zest.bootstrap.InvalidInjectionException;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.runtime.injection.DependencyModel;
 import org.apache.zest.runtime.injection.InjectionContext;
 import org.apache.zest.runtime.injection.InjectionProvider;
 import org.apache.zest.runtime.injection.InjectionProviderFactory;
 import org.apache.zest.runtime.model.Resolution;
 
+import static java.util.stream.Collectors.toCollection;
 import static org.apache.zest.api.util.Annotations.typeHasAnnotation;
 
 public final class ServiceInjectionProviderFactory
@@ -98,9 +99,7 @@ public final class ServiceInjectionProviderFactory
     private static class IterableServiceReferenceProvider
         extends ServiceInjectionProvider
     {
-        private IterableServiceReferenceProvider( Type serviceType,
-                                                  Predicate<ServiceReference<?>> serviceQualifier
-        )
+        private IterableServiceReferenceProvider( Type serviceType, Predicate<ServiceReference<?>> serviceQualifier )
         {
             super( serviceType, serviceQualifier );
         }
@@ -109,7 +108,7 @@ public final class ServiceInjectionProviderFactory
         public synchronized Object provideInjection( InjectionContext context )
             throws InjectionProviderException
         {
-            return getServiceReferences( context );
+            return getServiceReferences( context ).collect( toCollection( ArrayList::new ) );
         }
     }
 
@@ -117,9 +116,7 @@ public final class ServiceInjectionProviderFactory
         extends ServiceInjectionProvider
         implements Function<ServiceReference<?>, Object>
     {
-        private IterableServiceProvider( Type serviceType,
-                                         Predicate<ServiceReference<?>> serviceQualifier
-        )
+        private IterableServiceProvider( Type serviceType, Predicate<ServiceReference<?>> serviceQualifier )
         {
             super( serviceType, serviceQualifier );
         }
@@ -128,7 +125,8 @@ public final class ServiceInjectionProviderFactory
         public synchronized Object provideInjection( final InjectionContext context )
             throws InjectionProviderException
         {
-            return Iterables.map( this, getServiceReferences( context ) );
+            return getServiceReferences( context ).map( ServiceReference::get )
+                                                  .collect( toCollection( ArrayList::new ) );
         }
 
         @Override
@@ -185,9 +183,7 @@ public final class ServiceInjectionProviderFactory
         private final Type serviceType;
         private final Predicate<ServiceReference<?>> serviceQualifier;
 
-        private ServiceInjectionProvider( Type serviceType,
-                                          Predicate<ServiceReference<?>> serviceQualifier
-        )
+        private ServiceInjectionProvider( Type serviceType, Predicate<ServiceReference<?>> serviceQualifier )
         {
             this.serviceType = serviceType;
             this.serviceQualifier = serviceQualifier;
@@ -203,8 +199,8 @@ public final class ServiceInjectionProviderFactory
                 }
                 else
                 {
-                    return Iterables.first( Iterables.filter( serviceQualifier, context.module().instance()
-                        .findServices( serviceType ) ) );
+                    return context.module().instance().findServices( serviceType )
+                                  .filter( serviceQualifier ).findFirst().orElse( null );
                 }
             }
             catch( NoSuchServiceException e )
@@ -213,7 +209,7 @@ public final class ServiceInjectionProviderFactory
             }
         }
 
-        protected Iterable<ServiceReference<Object>> getServiceReferences( final InjectionContext context )
+        protected Stream<ServiceReference<Object>> getServiceReferences( final InjectionContext context )
         {
             if( serviceQualifier == null )
             {
@@ -221,7 +217,7 @@ public final class ServiceInjectionProviderFactory
             }
             else
             {
-                return Iterables.filter( serviceQualifier, context.module().instance().findServices( serviceType ) );
+                return context.module().instance().findServices( serviceType ).filter( serviceQualifier );
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
index 36a2f46..d3461d0 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
@@ -22,14 +22,13 @@ package org.apache.zest.runtime.structure;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Function;
-import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.apache.zest.api.activation.Activation;
 import org.apache.zest.api.activation.ActivationEventListener;
 import org.apache.zest.api.activation.ActivationException;
@@ -386,26 +385,25 @@ public class ModuleInstance
     }
 
     @Override
-    public <T> Iterable<ServiceReference<T>> findServices( Class<T> serviceType )
+    public <T> Stream<ServiceReference<T>> findServices( final Class<T> serviceType )
     {
         return findServices( (Type) serviceType );
     }
 
     @Override
-    public <T> Iterable<ServiceReference<T>> findServices( Type serviceType )
+    public <T> Stream<ServiceReference<T>> findServices( final Type serviceType )
     {
         List<? extends ModelDescriptor> serviceModels = typeLookup.lookupServiceModels( serviceType );
         if( serviceModels == null )
         {
-            return Collections.emptyList();
+            return Stream.empty();
         }
         //noinspection unchecked
         return serviceModels.stream()
-            .map( this::findServiceReferenceInstance )
-            .filter( Objects::nonNull )
-            .filter( ref -> ref.hasType( serviceType ) )
-            .map( ref -> (ServiceReference<T>) ref )
-            .collect( Collectors.toList() );
+                            .map( this::findServiceReferenceInstance )
+                            .filter( Objects::nonNull )
+                            .filter( ref -> ref.hasType( serviceType ) )
+                            .map( ref -> (ServiceReference<T>) ref );
     }
 
     private <T> ServiceReference<T> findServiceReferenceInstance( ModelDescriptor model )

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/test/java/org/apache/zest/runtime/mixin/JDKMixinTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/mixin/JDKMixinTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/mixin/JDKMixinTest.java
index aaa9b33..b57e16e 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/mixin/JDKMixinTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/mixin/JDKMixinTest.java
@@ -25,23 +25,23 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Predicate;
-import org.apache.zest.api.identity.Identity;
-import org.apache.zest.api.identity.StringIdentity;
-import org.json.JSONObject;
-import org.junit.Before;
-import org.junit.Test;
+import java.util.stream.Collectors;
 import org.apache.zest.api.common.Visibility;
 import org.apache.zest.api.concern.Concerns;
 import org.apache.zest.api.concern.GenericConcern;
+import org.apache.zest.api.identity.Identity;
+import org.apache.zest.api.identity.StringIdentity;
 import org.apache.zest.api.injection.scope.This;
 import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.test.AbstractZestTest;
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
 
-import static org.hamcrest.core.IsEqual.*;
-import static org.junit.Assert.*;
-import static org.apache.zest.functional.Iterables.*;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
 
 /**
  * Assert that JDK classes are usable as Mixins.
@@ -138,9 +138,9 @@ public class JDKMixinTest
     @Test
     public void testMixinExtendsJDK()
     {
-        List<ServiceReference<JSONSerializableMap>> services = toList(
-            filter( EXTENDS_IDENTITY_SPEC,
-                    serviceFinder.findServices( JSONSerializableMap.class ) ) );
+        List<ServiceReference<JSONSerializableMap>> services = serviceFinder.findServices( JSONSerializableMap.class )
+                                                                            .filter( EXTENDS_IDENTITY_SPEC )
+                                                                            .collect( Collectors.toList() );
 
         assertThat( services.size(), equalTo( 1 ) );
         assertThat( services.get( 0 ).identity(), equalTo( EXTENDS_IDENTITY ) );
@@ -158,9 +158,9 @@ public class JDKMixinTest
     @Test
     public void testComposeJDKMixin()
     {
-        List<ServiceReference<JSONSerializableMap>> services = toList(
-            filter( COMPOSE_IDENTITY_SPEC,
-                    serviceFinder.findServices( JSONSerializableMap.class ) ) );
+        List<ServiceReference<JSONSerializableMap>> services = serviceFinder.findServices( JSONSerializableMap.class )
+                                                                            .filter( COMPOSE_IDENTITY_SPEC )
+                                                                            .collect( Collectors.toList() );
 
         assertThat( services.size(), equalTo( 1 ) );
         assertThat( services.get( 0 ).identity(), equalTo( COMPOSE_IDENTITY ) );

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/test/java/org/apache/zest/runtime/service/PassivationTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/service/PassivationTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/service/PassivationTest.java
index 71b0319..04cdf91 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/service/PassivationTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/service/PassivationTest.java
@@ -52,13 +52,14 @@ public class PassivationTest
             }
         };
 
-        Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices( DataAccess.class );
-        for( ServiceReference<DataAccess> service : iterable )
-        {
-            assertTrue( "Service should not be Active before accessed", !service.isActive() );
-            assertTrue( service.get().data().activated );
-            assertTrue( "Service should be Active after access.", service.isActive() );
-        }
+        assembly.module().findServices( DataAccess.class ).forEach(
+            service ->
+            {
+                assertTrue( "Service should not be Active before accessed", !service.isActive() );
+                assertTrue( service.get().data().activated );
+                assertTrue( "Service should be Active after access.", service.isActive() );
+            }
+        );
         assembly.application().passivate();
     }
 
@@ -89,19 +90,20 @@ public class PassivationTest
 
         ArrayList<Data> datas = new ArrayList<Data>();
 
-        Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices( DataAccess.class );
-        for( ServiceReference<DataAccess> service : iterable )
-        {
-            assertTrue( "Service should not be Active before accessed", !service.isActive() );
-            Data data = service.get().data();
-            if( DataAccessService.class.isInstance( service.get() ) )
+        assembly.module().findServices( DataAccess.class ).forEach(
+            service ->
             {
-                // Collect the expected successes.
-                datas.add( data );
+                assertTrue( "Service should not be Active before accessed", !service.isActive() );
+                Data data = service.get().data();
+                if( DataAccessService.class.isInstance( service.get() ) )
+                {
+                    // Collect the expected successes.
+                    datas.add( data );
+                }
+                assertTrue( "Data should indicate that the service is activated", data.activated );
+                assertTrue( "Service should be Active after access.", service.isActive() );
             }
-            assertTrue( "Data should indicate that the service is activated", data.activated );
-            assertTrue( "Service should be Active after access.", service.isActive() );
-        }
+        );
         try
         {
             assembly.application().passivate();
@@ -113,10 +115,12 @@ public class PassivationTest
         }
 
         // Still ensure that all services have been shutdown.
-        for( ServiceReference<DataAccess> service : iterable )
-        {
-            assertFalse( "All services should have been shutdown", service.isActive() );
-        }
+        assembly.module().findServices( DataAccess.class ).forEach(
+            service ->
+            {
+                assertFalse( "All services should have been shutdown", service.isActive() );
+            }
+        );
     }
 
     @Test(expected = PassivationException.class)
@@ -133,13 +137,14 @@ public class PassivationTest
             }
         };
 
-        Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices( DataAccess.class );
-        for( ServiceReference<DataAccess> service : iterable )
-        {
-            assertTrue( "Service should not be Active before accessed", !service.isActive() );
-            assertTrue( service.get().data().activated );
-            assertTrue( "Service should be Active after access.", service.isActive() );
-        }
+        assembly.module().findServices( DataAccess.class ).forEach(
+            service ->
+            {
+                assertTrue( "Service should not be Active before accessed", !service.isActive() );
+                assertTrue( service.get().data().activated );
+                assertTrue( "Service should be Active after access.", service.isActive() );
+            }
+        );
         assembly.application().passivate();
     }
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/test/java/org/apache/zest/runtime/service/ServiceIdSelectorTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/service/ServiceIdSelectorTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/service/ServiceIdSelectorTest.java
index 60e75d9..6f8c253 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/service/ServiceIdSelectorTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/service/ServiceIdSelectorTest.java
@@ -20,7 +20,7 @@
 
 package org.apache.zest.runtime.service;
 
-import org.junit.Test;
+import java.util.stream.StreamSupport;
 import org.apache.zest.api.activation.ActivationException;
 import org.apache.zest.api.injection.scope.Service;
 import org.apache.zest.api.injection.scope.Uses;
@@ -31,11 +31,11 @@ import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.bootstrap.SingletonAssembler;
+import org.junit.Test;
 
+import static org.apache.zest.api.service.qualifier.ServiceQualifier.withId;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertThat;
-import static org.apache.zest.api.service.qualifier.ServiceQualifier.firstService;
-import static org.apache.zest.api.service.qualifier.ServiceQualifier.withId;
 
 /**
  * JAVADOC
@@ -70,7 +70,9 @@ public class ServiceIdSelectorTest
 
         public ServiceConsumer( @Uses String serviceId, @Service Iterable<ServiceReference<TestService>> serviceRefs )
         {
-            service = firstService( withId( serviceId ), serviceRefs );
+            service = StreamSupport.stream( serviceRefs.spliterator(), false )
+                                   .filter( withId( serviceId ) )
+                                   .findFirst().map( ServiceReference::get ).orElse( null );
         }
 
         public TestService getService()

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/core/runtime/src/test/java/org/apache/zest/runtime/structure/TypeToCompositeLookupTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/structure/TypeToCompositeLookupTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/structure/TypeToCompositeLookupTest.java
index f7da054..cf0a36f 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/structure/TypeToCompositeLookupTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/structure/TypeToCompositeLookupTest.java
@@ -20,25 +20,26 @@
 package org.apache.zest.runtime.structure;
 
 import java.util.Iterator;
-import org.apache.zest.api.identity.HasIdentity;
-import org.apache.zest.api.identity.Identity;
-import org.apache.zest.api.unitofwork.UnitOfWorkFactory;
-import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler;
-import org.junit.Test;
 import org.apache.zest.api.activation.ActivationException;
 import org.apache.zest.api.composite.AmbiguousTypeException;
+import org.apache.zest.api.identity.HasIdentity;
+import org.apache.zest.api.identity.Identity;
 import org.apache.zest.api.mixin.Mixins;
 import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.api.structure.Module;
 import org.apache.zest.api.unitofwork.UnitOfWork;
 import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException;
+import org.apache.zest.api.unitofwork.UnitOfWorkFactory;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.bootstrap.SingletonAssembler;
-import org.apache.zest.functional.Iterables;
+import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler;
 import org.apache.zest.test.EntityTestAssembler;
+import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
 
 /**
  * Theses tests ensure that Type to Composite lookup work as expected for
@@ -379,9 +380,9 @@ public class TypeToCompositeLookupTest
 
         }.module();
 
-        assertEquals( 1, Iterables.count( module.findServices( SomeOtherFoo.class ) ) );
-        assertEquals( 2, Iterables.count( module.findServices( BasicFoo.class ) ) );
-        assertEquals( 2, Iterables.count( module.findServices( Foo.class ) ) );
+        assertEquals( 1, module.findServices( SomeOtherFoo.class ).count() );
+        assertEquals( 2, module.findServices( BasicFoo.class ).count() );
+        assertEquals( 2, module.findServices( Foo.class ).count() );
 
         assertEquals( CATHEDRAL, module.findService( SomeOtherFoo.class ).get().bar() );
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java
----------------------------------------------------------------------
diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java
index 54f2135..9ef83b2 100644
--- a/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java
+++ b/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java
@@ -20,6 +20,7 @@
 package org.apache.zest.library.http;
 
 import java.util.Iterator;
+import java.util.stream.Collectors;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.zest.test.util.FreePortFinder;
 import org.junit.Test;
@@ -71,7 +72,8 @@ public final class JettyServiceTest
     public final void testInstantiation()
         throws Throwable
     {
-        Iterable<ServiceReference<JettyService>> services = serviceFinder.findServices( JettyService.class );
+        Iterable<ServiceReference<JettyService>> services = serviceFinder.findServices( JettyService.class )
+                                                                         .collect( Collectors.toList() );
         assertNotNull( services );
 
         Iterator<ServiceReference<JettyService>> iterator = services.iterator();

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java
----------------------------------------------------------------------
diff --git a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java
index bd49b92..137b428 100644
--- a/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java
+++ b/libraries/jmx/src/main/java/org/apache/zest/library/jmx/ApplicationManagerService.java
@@ -48,8 +48,6 @@ import org.apache.zest.api.structure.ModuleDescriptor;
 import org.apache.zest.functional.HierarchicalVisitorAdapter;
 
 import static org.apache.zest.api.service.qualifier.ServiceQualifier.withId;
-import static org.apache.zest.functional.Iterables.filter;
-import static org.apache.zest.functional.Iterables.first;
 
 /**
  * Expose the Zest app as a "tree" of MBeans.
@@ -310,27 +308,27 @@ public interface ApplicationManagerService
         public boolean isActive()
         {
             Class<?> mainType = serviceDescriptor.types().findFirst().orElse( null );
-            ServiceReference<?> first = first( filter( withId( serviceDescriptor.identity().toString() ),
-                                                       module.findServices( mainType ) )
-            );
+            ServiceReference<?> first = module.findServices( mainType )
+                                              .filter( withId( serviceDescriptor.identity().toString() ) )
+                                              .findFirst().orElse( null );
             return first != null && first.isActive();
         }
 
         public boolean isAvailable()
         {
             Class<?> mainType = serviceDescriptor.types().findFirst().orElse( null );
-            ServiceReference<?> first = first( filter( withId( serviceDescriptor.identity().toString() ),
-                                                       module.findServices( mainType ) )
-            );
+            ServiceReference<?> first = module.findServices( mainType )
+                                              .filter( withId( serviceDescriptor.identity().toString() ) )
+                                              .findFirst().orElse( null );
             return first != null && first.isAvailable();
         }
 
         public String restart()
         {
-            Iterable<?> services = module.findServices( serviceDescriptor.types().findFirst().orElse( null ) );
-            ServiceReference<?> serviceRef = (ServiceReference) first( filter( withId( serviceDescriptor.identity().toString() ),
-                                                                               services )
-            );
+            ServiceReference<?> serviceRef = module.findServices( serviceDescriptor.types()
+                                                                                   .findFirst().orElse( null ) )
+                                                   .filter( withId( serviceDescriptor.identity().toString() ) )
+                                                   .findFirst().orElse( null );
             if( serviceRef != null )
             {
                 try

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/libraries/osgi/src/main/java/org/apache/zest/library/osgi/OSGiEnabledService.java
----------------------------------------------------------------------
diff --git a/libraries/osgi/src/main/java/org/apache/zest/library/osgi/OSGiEnabledService.java b/libraries/osgi/src/main/java/org/apache/zest/library/osgi/OSGiEnabledService.java
index 8a3d6c7..b0a2a55 100644
--- a/libraries/osgi/src/main/java/org/apache/zest/library/osgi/OSGiEnabledService.java
+++ b/libraries/osgi/src/main/java/org/apache/zest/library/osgi/OSGiEnabledService.java
@@ -90,16 +90,17 @@ public interface OSGiEnabledService extends ServiceComposite
             {
                 return;
             }
-            for( ServiceReference ref : module.findServices( descriptor.types().findFirst().orElse( null ) ) )
-            {
-                if( ref.identity().equals( identity().get() ) )
-                {
-                    Stream<? extends Type> classesSet = descriptor.types();
-                    Dictionary properties = descriptor.metaInfo( Dictionary.class );
-                    String[] clazzes = fetchInterfacesImplemented( classesSet );
-                    registration = context.registerService( clazzes, ref.get(), properties );
+            module.findServices( descriptor.types().findFirst().orElse( null ) ).forEach(
+                ref -> {
+                    if( ref.identity().equals( identity().get() ) )
+                    {
+                        Stream<? extends Type> classesSet = descriptor.types();
+                        Dictionary properties = descriptor.metaInfo( Dictionary.class );
+                        String[] clazzes = fetchInterfacesImplemented( classesSet );
+                        registration = context.registerService( clazzes, ref.get(), properties );
+                    }
                 }
-            }
+            );
         }
 
         private String[] fetchInterfacesImplemented( Stream<? extends Type> classesSet )

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/libraries/spring/src/main/java/org/apache/zest/library/spring/bootstrap/internal/service/ServiceLocator.java
----------------------------------------------------------------------
diff --git a/libraries/spring/src/main/java/org/apache/zest/library/spring/bootstrap/internal/service/ServiceLocator.java b/libraries/spring/src/main/java/org/apache/zest/library/spring/bootstrap/internal/service/ServiceLocator.java
index 7bb8d6c..885b515 100644
--- a/libraries/spring/src/main/java/org/apache/zest/library/spring/bootstrap/internal/service/ServiceLocator.java
+++ b/libraries/spring/src/main/java/org/apache/zest/library/spring/bootstrap/internal/service/ServiceLocator.java
@@ -34,7 +34,7 @@ final class ServiceLocator
     implements HierarchicalVisitor<Object, Object, RuntimeException>
 {
     private final String serviceId;
-    private Class serviceType;
+    private Class<?> serviceType;
     private String moduleName;
     private String layerName;
 
@@ -115,16 +115,10 @@ final class ServiceLocator
         if( layerName != null )
         {
             Module module = anApplication.findModule( layerName, moduleName );
-            Iterable<ServiceReference<Object>> serviceRefs = module.findServices( serviceType );
-            for( ServiceReference<Object> serviceRef : serviceRefs )
-            {
-                if( serviceId.equals( serviceRef.identity().toString() ) )
-                {
-                    return serviceRef;
-                }
-            }
+            return module.findServices( serviceType )
+                         .filter( ref -> ref.identity().toString().equals( serviceId ) )
+                         .findFirst().orElse( null );
         }
-
         return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/libraries/spring/src/test/java/org/apache/zest/library/spring/importer/ZestImportServiceTest.java
----------------------------------------------------------------------
diff --git a/libraries/spring/src/test/java/org/apache/zest/library/spring/importer/ZestImportServiceTest.java b/libraries/spring/src/test/java/org/apache/zest/library/spring/importer/ZestImportServiceTest.java
index 04fff95..a68ec3a 100644
--- a/libraries/spring/src/test/java/org/apache/zest/library/spring/importer/ZestImportServiceTest.java
+++ b/libraries/spring/src/test/java/org/apache/zest/library/spring/importer/ZestImportServiceTest.java
@@ -19,26 +19,25 @@
  */
 package org.apache.zest.library.spring.importer;
 
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import java.util.stream.StreamSupport;
 import org.apache.zest.api.activation.ActivationException;
 import org.apache.zest.api.injection.scope.Service;
 import org.apache.zest.api.injection.scope.Structure;
 import org.apache.zest.api.service.ServiceFinder;
 import org.apache.zest.api.service.ServiceReference;
-import org.apache.zest.api.service.qualifier.ServiceQualifier;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.bootstrap.SingletonAssembler;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationContext;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+import static org.apache.zest.api.service.qualifier.ServiceQualifier.withId;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertThat;
-import static org.apache.zest.api.service.qualifier.ServiceQualifier.firstService;
-import static org.apache.zest.api.service.qualifier.ServiceQualifier.withId;
 
 @RunWith( SpringJUnit4ClassRunner.class )
 @ContextConfiguration
@@ -86,7 +85,9 @@ public final class ZestImportServiceTest
 
         assembler.module().injectTo(this);
 
-        CommentService service = firstService( withId( "commentService2" ), services );
+        CommentService service = StreamSupport.stream( services.spliterator(), false )
+                                              .filter( withId( "commentService2" ) )
+                                              .findFirst().map( ServiceReference::get ).orElse( null );
         assertThat( "service with correct id has been selected", service.comment( "pizza" ), equalTo( "pizza is good." ) );
     }
 
@@ -108,7 +109,10 @@ public final class ZestImportServiceTest
 
         assembler.module().injectTo( this );
 
-        CommentService foundService = ServiceQualifier.firstService( withId( "commentService2" ), finder.<CommentService>findServices( CommentService.class ));
+        CommentService foundService = finder.findServices( CommentService.class )
+                                            .filter( withId( "commentService2" ) )
+                                            .findFirst().map( ServiceReference::get )
+                                            .orElse( null );
         assertThat( "service with correct id has been selected", foundService.comment( "pizza" ), equalTo( "pizza is good." ) );
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/zest-java/blob/f8cd4f58/samples/rental/src/main/java/org/apache/zest/sample/rental/web/QuikitServlet.java
----------------------------------------------------------------------
diff --git a/samples/rental/src/main/java/org/apache/zest/sample/rental/web/QuikitServlet.java b/samples/rental/src/main/java/org/apache/zest/sample/rental/web/QuikitServlet.java
index 338f335..b7529e8 100644
--- a/samples/rental/src/main/java/org/apache/zest/sample/rental/web/QuikitServlet.java
+++ b/samples/rental/src/main/java/org/apache/zest/sample/rental/web/QuikitServlet.java
@@ -49,7 +49,6 @@ import javax.xml.validation.SchemaFactory;
 import org.apache.zest.api.ZestAPI;
 import org.apache.zest.api.composite.Composite;
 import org.apache.zest.api.service.ServiceFinder;
-import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.api.structure.Application;
 import org.apache.zest.api.structure.Module;
 import org.apache.zest.bootstrap.ApplicationAssembler;
@@ -98,13 +97,14 @@ public class QuikitServlet
                 DataInitializer initializer = module.newTransient( DataInitializer.class );
                 initializer.initialize();
             }
-            Iterable<ServiceReference<Page>> iterable = finder.findServices( Page.class );
-            for( ServiceReference<Page> page : iterable )
-            {
-                PageMetaInfo pageMetaInfo = page.metaInfo( PageMetaInfo.class );
-                String mountPoint = pageMetaInfo.mountPoint();
-                mountPoints.put( mountPoint, page.get() );
-            }
+            finder.findServices( Page.class ).forEach(
+                page ->
+                {
+                    PageMetaInfo pageMetaInfo = page.metaInfo( PageMetaInfo.class );
+                    String mountPoint = pageMetaInfo.mountPoint();
+                    mountPoints.put( mountPoint, page.get() );
+                }
+            );
         }
         catch( Exception e )
         {