aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject svn commit: r1796106 - in /aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional: Event.java OSGi.java internal/OSGiImpl.java internal/Tuple.java
Date Wed, 24 May 2017 21:43:38 GMT
Author: csierra
Date: Wed May 24 21:43:38 2017
New Revision: 1796106

URL: http://svn.apache.org/viewvc?rev=1796106&view=rev
Log:
New route primitive to allow more complex implementations

Added:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/Event.java
      - copied, changed from r1795901, aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java
Modified:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/OSGiImpl.java
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java

Copied: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/Event.java
(from r1795901, aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java)
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/Event.java?p2=aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/Event.java&p1=aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java&r1=1795901&r2=1796106&rev=1796106&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/Event.java
Wed May 24 21:43:38 2017
@@ -15,30 +15,11 @@
  * limitations under the License.
  */
 
-
-package org.apache.aries.osgi.functional.internal;
-
-import java.util.function.Function;
+package org.apache.aries.osgi.functional;
 
 /**
  * @author Carlos Sierra Andrés
  */
-class Tuple<T> {
-
-	public Object original;
-	public T t;
-
-	private Tuple(Object original, T t) {
-		this.original = original;
-		this.t = t;
-	}
-
-	public <S> Tuple<S> map(Function<? super T, ? extends S> fun) {
-		return new Tuple<>(original, fun.apply(t));
-	}
-
-	public static <T> Tuple<T> create(T t) {
-		return new Tuple<>(t, t);
-	}
-
+public interface Event<T> {
+    T getContent();
 }

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java?rev=1796106&r1=1796105&r2=1796106&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
Wed May 24 21:43:38 2017
@@ -148,4 +148,20 @@ public interface OSGi<T> extends OSGiRun
 	OSGi<T> filter(Predicate<T> predicate);
 
 	OSGi<Void> distribute(Function<T, OSGi<?>>... funs);
+
+	OSGi<T> route(Consumer<Router<T>> routerConsumer);
+
+	interface Router<T> {
+
+		void onIncoming(Consumer<Event<T>> adding);
+		void onLeaving(Consumer<Event<T>> removing);
+
+		void onStart(Runnable start);
+		void onClose(Runnable close);
+
+		void signalAdd(Event<T> event);
+		void signalLeave(Event<T> event);
+
+	}
+
 }

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=1796106&r1=1796105&r2=1796106&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 24 21:43:38 2017
@@ -17,6 +17,7 @@
 
 package org.apache.aries.osgi.functional.internal;
 
+import org.apache.aries.osgi.functional.Event;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.aries.osgi.functional.OSGiResult;
 import org.osgi.framework.BundleContext;
@@ -225,6 +226,43 @@ public class OSGiImpl<T> implements OSGi
 	}
 
 	@Override
+	public OSGi<T> route(Consumer<Router<T>> routerConsumer) {
+
+		Pipe<Tuple<T>, Tuple<T>> outgoingAddingPipe = Pipe.create();
+		Pipe<Tuple<T>, Tuple<T>> outgoingRemovingPipe = Pipe.create();
+
+		Consumer<Tuple<T>> outgoingAddingSource =
+			outgoingAddingPipe.getSource();
+		Consumer<Tuple<T>> outgoingRemovingSource =
+			outgoingRemovingPipe.getSource();
+
+		final RouterImpl<T> router =
+			new RouterImpl<>(outgoingAddingSource, outgoingRemovingSource);
+
+		routerConsumer.accept(router);
+
+		return new OSGiImpl<>(((bundleContext) -> {
+			OSGiResultImpl<T> osgiResult = _operation.run(bundleContext);
+
+			osgiResult.added.map(
+				t -> {router._adding.accept(t); return null;});
+			osgiResult.removed.map(
+				t -> {router._leaving.accept(t); return null;});
+
+			return new OSGiResultImpl<>(
+				outgoingAddingPipe, outgoingRemovingPipe,
+				() -> {
+					router._start.run();
+					osgiResult.start.run();
+				},
+				() -> {
+					router._close.run();
+					osgiResult.close.run();
+				});
+		}));
+	}
+
+	@Override
 	@SafeVarargs
 	final public OSGi<Void> distribute(Function<T, OSGi<?>>... funs) {
 		return new OSGiImpl<>(bundleContext -> {
@@ -250,6 +288,56 @@ public class OSGiImpl<T> implements OSGi
 			);
 		});
 	}
+
+	static class RouterImpl<T> implements Router<T> {
+
+		RouterImpl(
+			Consumer<Tuple<T>> signalAdding, Consumer<Tuple<T>> signalLeaving)
{
+
+			_signalAdding = signalAdding;
+			_signalLeaving = signalLeaving;
+		}
+
+		@Override
+		public void onIncoming(Consumer<Event<T>> adding) {
+			_adding = adding;
+		}
+
+		@Override
+		public void onLeaving(Consumer<Event<T>> removing) {
+			_leaving = removing;
+		}
+
+		@Override
+		public void onClose(Runnable close) {
+			_close = close;
+		}
+
+		@Override
+		public void onStart(Runnable start) {
+			_start = start;
+		}
+
+		@Override
+		public void signalAdd(Event<T> event) {
+			_signalAdding.accept((Tuple<T>)event);
+		}
+
+		@Override
+		public void signalLeave(Event<T> event) {
+			_signalLeaving.accept((Tuple<T>)event);
+		}
+
+		Consumer<Event<T>> _adding = (ign) -> {};
+		Consumer<Event<T>> _leaving = (ign) -> {};
+
+		private Runnable _close = NOOP;
+		private final Consumer<Tuple<T>> _signalAdding;
+		private final Consumer<Tuple<T>> _signalLeaving;
+		private Runnable _start = NOOP;
+
+	}
+
 }
 
 

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java?rev=1796106&r1=1796105&r2=1796106&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/Tuple.java
Wed May 24 21:43:38 2017
@@ -18,12 +18,14 @@
 
 package org.apache.aries.osgi.functional.internal;
 
+import org.apache.aries.osgi.functional.Event;
+
 import java.util.function.Function;
 
 /**
  * @author Carlos Sierra Andrés
  */
-class Tuple<T> {
+class Tuple<T> implements Event<T> {
 
 	public Object original;
 	public T t;
@@ -41,4 +43,9 @@ class Tuple<T> {
 		return new Tuple<>(t, t);
 	}
 
+	@Override
+	public T getContent() {
+		return t;
+	}
+
 }



Mime
View raw message