]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/ListenerEntry.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / ListenerEntry.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/ListenerEntry.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/ListenerEntry.java
new file mode 100644 (file)
index 0000000..f21cdf1
--- /dev/null
@@ -0,0 +1,122 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.accessor.impl;
+
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.concurrent.Executor;\r
+\r
+import org.simantics.databoard.accessor.Accessor.Listener;\r
+import org.simantics.databoard.accessor.event.Event;\r
+import org.simantics.databoard.accessor.interestset.InterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+
+/**
+ * ListenerEntry is a linked list of <Listener, InterestSet> pairs
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class ListenerEntry {
+
+       public Listener listener;
+       public InterestSet interestSet;
+       // Path from the listened object 
+       public ChildReference path;\r
+       public Executor executor;
+       public ListenerEntry next;
+       
+       public ListenerEntry(Listener listener, InterestSet interestSet, ChildReference path, Executor executor) {
+               if (interestSet==null) throw new IllegalArgumentException();
+               this.listener = listener;
+               this.interestSet = interestSet;
+               this.path = path;
+               this.executor = executor;\r
+       }
+       
+       /**
+        * Add new entry to listener list 
+        * 
+        * @param prevValue
+        * @param listener
+        * @param interestSet
+        * @return the new assignment for the list
+        */
+       public static ListenerEntry link(ListenerEntry prevValue, Listener listener, InterestSet interestSet, ChildReference path, Executor executor) {
+               ListenerEntry e = new ListenerEntry(listener, interestSet, path, executor);
+               e.next = prevValue;
+               return e;
+       }
+       
+       @SuppressWarnings("unchecked")
+       public <T extends InterestSet> T getInterestSet() {
+               return (T) interestSet;
+       }
+
+       
+       /**
+        * Remove listener entry from linked list
+        * 
+        * @param firstEntry
+        * @param listener the value to be removed
+        * @return the new assignment for the list
+        */
+       public static ListenerEntry remove(ListenerEntry firstEntry, Listener listener) {
+               ListenerEntry e = firstEntry;
+               ListenerEntry p = null;
+               while (e!=null) {
+                       if (e.listener == listener) {
+                               if (p==null) return e.next;
+                               p.next = e.next;
+                               return firstEntry;
+                       }
+                       p = e;
+                       e = e.next;
+               }
+               return null;
+       }
+       
+       /**
+        * Emits an event to the listener. 
+        * 
+        * NOTE, Event instance can be emited only once. This method writes to the
+        * e.reference field. 
+        *  
+        * @param e
+        */
+       public void emitEvent(final Event e) {
+//             e.reference = ChildReference.concatenate(path, e.reference);\r
+               if (executor == null) {
+                       listener.onEvents( Collections.singleton(e) );\r
+               } else {\r
+                       executor.execute( new Runnable() {\r
+                               public void run() {\r
+                                       listener.onEvents( Collections.singleton(e) );\r
+                               }});\r
+               }
+       }
+\r
+       public void emitEvents(final Collection<Event> events) {\r
+//             for (Event e : events) {\r
+//                     e.reference = ChildReference.concatenate(path, e.reference);\r
+//             }\r
+               if (executor == null) {\r
+                       listener.onEvents( events );\r
+               } else {\r
+                       executor.execute( new Runnable() {\r
+                               public void run() {\r
+                                       listener.onEvents( events );\r
+                               }});\r
+               }\r
+       }\r
+       
+}
+