aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject svn commit: r1797035 - /aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java
Date Wed, 31 May 2017 13:23:37 GMT
Author: csierra
Date: Wed May 31 13:23:37 2017
New Revision: 1797035

URL: http://svn.apache.org/viewvc?rev=1797035&view=rev
Log:
Correct implementation of distribute

The older one invoked flatMap an unnecessary number of times.

Modified:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java?rev=1797035&r1=1797034&r2=1797035&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java
Wed May 31 13:23:37 2017
@@ -281,25 +281,58 @@ public class OSGiImpl<T> implements OSGi
 	@SafeVarargs
 	final public OSGi<Void> distribute(Function<T, OSGi<?>>... funs) {
 		return new OSGiImpl<>(bundleContext -> {
-			ArrayList<OSGiResult> results = new ArrayList<>();
-
 			Pipe<Tuple<Void>, Tuple<Void>> added = Pipe.create();
 
 			Consumer<Tuple<Void>> addedSource = added.getSource();
 
+			IdentityHashMap<Object, List<OSGiResult<?>>> results =
+				new IdentityHashMap<>();
+
+			Pipe<Tuple<Void>, Tuple<Void>> removed = Pipe.create();
+
+			Consumer<Tuple<Void>> removedSource = removed.getSource();
+
+			AtomicReference<OSGiResult<?>> atomicReference =
+				new AtomicReference<>();
+
 			return new OSGiResultImpl<>(
-				added, Pipe.create(),
+				added, removed,
 				() -> {
-					List<OSGiResult> results2 = Arrays.stream(funs).
-						map(this::flatMap).
-						map(o -> o.run(bundleContext)).
-						collect(Collectors.toList());
+					OSGiResultImpl<T> osgiResult = _operation.run(
+						bundleContext);
+
+					osgiResult.added.map(t -> {
+						results.put(
+							t.original,
+							Arrays.stream(funs).
+								map(f -> f.apply(t.t)).
+								map(o -> o.run(bundleContext)).
+								collect(Collectors.toList())
+						);
+
+						addedSource.accept(Tuple.create(null));
 
-					results.addAll(results2);
+						return null;
+					});
 
-					addedSource.accept(Tuple.create(null));
+					osgiResult.removed.map(t -> {
+						results.get(t.original).forEach(OSGiResult::close);
+
+						removedSource.accept(Tuple.create(null));
+
+						return null;
+					});
+
+					osgiResult.start.run();
+
+					atomicReference.set(osgiResult);
 				},
-				() -> results.stream().forEach(OSGiResult::close)
+				() -> {
+					results.values().forEach(
+						l -> l.forEach(OSGiResult::close));
+
+					atomicReference.get().close();
+				}
 			);
 		});
 	}



Mime
View raw message