summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/util')
-rw-r--r--src/jrummikub/util/Event3.java46
-rw-r--r--src/jrummikub/util/IEvent3.java28
-rw-r--r--src/jrummikub/util/IListener3.java22
3 files changed, 96 insertions, 0 deletions
diff --git a/src/jrummikub/util/Event3.java b/src/jrummikub/util/Event3.java
new file mode 100644
index 0000000..a309111
--- /dev/null
+++ b/src/jrummikub/util/Event3.java
@@ -0,0 +1,46 @@
+package jrummikub.util;
+
+import java.util.HashSet;
+
+/**
+ * Simple single parameter event generator
+ *
+ * @param <T1>
+ * type of the first event parameter
+ * @param <T2>
+ * type of the second event parameter
+ */
+public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
+ private HashSet<IListener3<T1, T2, T3>> listeners = new HashSet<IListener3<T1, T2, T3>>();
+
+ @Override
+ public Connection add(final IListener3<T1, T2, T3> listener) {
+ listeners.add(listener);
+ return new Connection() {
+
+ @Override
+ public void remove() {
+ Event3.this.remove(listener);
+ }
+ };
+ }
+
+ @Override
+ public void remove(IListener3<T1, T2, T3> listener) {
+ listeners.remove(listener);
+ }
+
+ /**
+ * Generate a single event
+ *
+ * @param value1
+ * the first event parameter
+ * @param value2
+ * the second event parameter
+ */
+ public void emit(T1 value1, T2 value2, T3 value3) {
+ for (IListener3<T1, T2, T3> listener : listeners) {
+ listener.handle(value1, value2, value3);
+ }
+ }
+}
diff --git a/src/jrummikub/util/IEvent3.java b/src/jrummikub/util/IEvent3.java
new file mode 100644
index 0000000..fd04364
--- /dev/null
+++ b/src/jrummikub/util/IEvent3.java
@@ -0,0 +1,28 @@
+package jrummikub.util;
+
+/**
+ * Interface for classes that can generate events having a two parameters
+ *
+ * @param <T1>
+ * type of the first event parameter
+ * @param <T2>
+ * type of the second event parameter
+ */
+public interface IEvent3<T1, T2, T3> {
+ /**
+ * Start to publish all events to a given listener
+ *
+ * @param listener
+ * target listener
+ * @return a connection to remove the listener
+ */
+ public Connection add(IListener3<T1, T2, T3> listener);
+
+ /**
+ * Stop publishing events to a given listener
+ *
+ * @param listener
+ * target listener
+ */
+ public void remove(IListener3<T1, T2, T3> listener);
+}
diff --git a/src/jrummikub/util/IListener3.java b/src/jrummikub/util/IListener3.java
new file mode 100644
index 0000000..ae1d6ce
--- /dev/null
+++ b/src/jrummikub/util/IListener3.java
@@ -0,0 +1,22 @@
+package jrummikub.util;
+
+/**
+ * Interface for classes that can receive parameterless events having a two
+ * parameters
+ *
+ * @param <T1>
+ * type of the first event parameter
+ * @param <T2>
+ * type of the first event parameter
+ */
+public interface IListener3<T1, T2, T3> {
+ /**
+ * This method is called whenever a class we're listening to emits an event
+ *
+ * @param value1
+ * the first event parameter
+ * @param value2
+ * the second event parameter
+ */
+ public void handle(T1 value1, T2 value2, T3 value3);
+}