From b67b3b08e8ebb46497ab2932185d6144e4cde604 Mon Sep 17 00:00:00 2001
From: Christian Wulf <chw@informatik.uni-kiel.de>
Date: Tue, 12 Aug 2014 06:02:23 +0200
Subject: [PATCH] added pipe factory concept

---
 dependency-reduced-pom.xml                    | 69 +++++++++++++++++++
 .../framework/core/pipe/IPipeFactory.java     |  6 ++
 .../framework/core/pipe/PipeFactory.java      | 51 ++++++++++++++
 .../framework/core/pipe/SpScPipe.java         | 10 ++-
 .../framework/core/pipe/SpScPipeFactory.java  | 10 +++
 5 files changed, 143 insertions(+), 3 deletions(-)
 create mode 100644 dependency-reduced-pom.xml
 create mode 100644 src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/IPipeFactory.java
 create mode 100644 src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java
 create mode 100644 src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipeFactory.java

diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml
new file mode 100644
index 00000000..578da31d
--- /dev/null
+++ b/dependency-reduced-pom.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>net.sourceforge.teetime</groupId>
+  <artifactId>teetime</artifactId>
+  <name>teetime</name>
+  <version>1.0-SNAPSHOT</version>
+  <url>http://maven.apache.org</url>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>versions-maven-plugin</artifactId>
+        <version>2.1</version>
+      </plugin>
+      <plugin>
+        <artifactId>maven-shade-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <finalName>${project.artifactId}-${project.version}</finalName>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>sonatype.oss.snapshots</id>
+      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-core</artifactId>
+      <version>1.3</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-library</artifactId>
+      <version>1.3</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+</project>
+
diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/IPipeFactory.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/IPipeFactory.java
new file mode 100644
index 00000000..b6dbc5f9
--- /dev/null
+++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/IPipeFactory.java
@@ -0,0 +1,6 @@
+package teetime.variant.methodcallWithPorts.framework.core.pipe;
+
+public interface IPipeFactory {
+
+	<T> IPipe<T> create(int capacity);
+}
diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java
new file mode 100644
index 00000000..5534a69d
--- /dev/null
+++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java
@@ -0,0 +1,51 @@
+package teetime.variant.methodcallWithPorts.framework.core.pipe;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class PipeFactory {
+
+	public enum ThreadCommunication {
+		INTER, INTRA
+	}
+
+	public enum Ordering {
+		/**
+		 * FIFO
+		 */
+		QUEUE_BASED,
+		/**
+		 * LIFO
+		 */
+		STACK_BASED,
+		ARBITRARY
+	}
+
+	private final Map<String, IPipeFactory> pipeFactories = new HashMap<String, IPipeFactory>();
+
+	/**
+	 * Creates a new FIFO-ordered, growable pipe with an initial capacity of 1. <br>
+	 * <i>This method is suitable for most programmers.</i>
+	 *
+	 * @param tc
+	 * @return
+	 */
+	public <T> IPipe<T> create(final ThreadCommunication tc) {
+		return this.create(tc, Ordering.QUEUE_BASED, true, 1);
+	}
+
+	public <T> IPipe<T> create(final ThreadCommunication tc, final Ordering ordering, final boolean growable, final int capacity) {
+		String key = this.buildKey(tc, ordering, growable);
+		IPipeFactory pipeClass = this.pipeFactories.get(key);
+		return pipeClass.create(capacity);
+	}
+
+	private String buildKey(final ThreadCommunication tc, final Ordering ordering, final boolean growable) {
+		return tc.toString() + ordering.toString() + growable;
+	}
+
+	public void register(final IPipeFactory pipeFactory, final ThreadCommunication tc, final Ordering ordering, final boolean growable) {
+		String key = this.buildKey(tc, ordering, growable);
+		this.pipeFactories.put(key, pipeFactory);
+	}
+}
diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipe.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipe.java
index 58dc26fc..1c8994cc 100644
--- a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipe.java
+++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipe.java
@@ -26,10 +26,14 @@ public class SpScPipe<T> extends AbstractPipe<T> {
 
 	public static <T> SpScPipe<T> connect(final OutputPort<T> sourcePort, final InputPort<T> targetPort, final int capacity) {
 		SpScPipe<T> pipe = new SpScPipe<T>(capacity);
-		targetPort.setPipe(pipe);
-		sourcePort.setPipe(pipe);
+		return pipe.connect(sourcePort, targetPort);
+	}
+
+	public SpScPipe<T> connect(final OutputPort<T> sourcePort, final InputPort<T> targetPort) {
+		targetPort.setPipe(this);
+		sourcePort.setPipe(this);
 		sourcePort.setCachedTargetStage(targetPort.getOwningStage());
-		return pipe;
+		return this;
 	}
 
 	@Override
diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipeFactory.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipeFactory.java
new file mode 100644
index 00000000..745823c1
--- /dev/null
+++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/SpScPipeFactory.java
@@ -0,0 +1,10 @@
+package teetime.variant.methodcallWithPorts.framework.core.pipe;
+
+public class SpScPipeFactory implements IPipeFactory {
+
+	@Override
+	public <T> IPipe<T> create(final int capacity) {
+		return new SpScPipe<T>(capacity);
+	}
+
+}
-- 
GitLab