]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.accessor.impl;
13
14 import java.util.Collection;\r
15 import java.util.Collections;\r
16 import java.util.concurrent.Executor;\r
17 \r
18 import org.simantics.databoard.accessor.Accessor.Listener;\r
19 import org.simantics.databoard.accessor.event.Event;\r
20 import org.simantics.databoard.accessor.interestset.InterestSet;\r
21 import org.simantics.databoard.accessor.reference.ChildReference;\r
22
23 /**
24  * ListenerEntry is a linked list of <Listener, InterestSet> pairs
25  *
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class ListenerEntry {
29
30         public Listener listener;
31         public InterestSet interestSet;
32         // Path from the listened object 
33         public ChildReference path;\r
34         public Executor executor;
35         public ListenerEntry next;
36         
37         public ListenerEntry(Listener listener, InterestSet interestSet, ChildReference path, Executor executor) {
38                 if (interestSet==null) throw new IllegalArgumentException();
39                 this.listener = listener;
40                 this.interestSet = interestSet;
41                 this.path = path;
42                 this.executor = executor;\r
43         }
44         
45         /**
46          * Add new entry to listener list 
47          * 
48          * @param prevValue
49          * @param listener
50          * @param interestSet
51          * @return the new assignment for the list
52          */
53         public static ListenerEntry link(ListenerEntry prevValue, Listener listener, InterestSet interestSet, ChildReference path, Executor executor) {
54                 ListenerEntry e = new ListenerEntry(listener, interestSet, path, executor);
55                 e.next = prevValue;
56                 return e;
57         }
58         
59         @SuppressWarnings("unchecked")
60         public <T extends InterestSet> T getInterestSet() {
61                 return (T) interestSet;
62         }
63
64         
65         /**
66          * Remove listener entry from linked list
67          * 
68          * @param firstEntry
69          * @param listener the value to be removed
70          * @return the new assignment for the list
71          */
72         public static ListenerEntry remove(ListenerEntry firstEntry, Listener listener) {
73                 ListenerEntry e = firstEntry;
74                 ListenerEntry p = null;
75                 while (e!=null) {
76                         if (e.listener == listener) {
77                                 if (p==null) return e.next;
78                                 p.next = e.next;
79                                 return firstEntry;
80                         }
81                         p = e;
82                         e = e.next;
83                 }
84                 return null;
85         }
86         
87         /**
88          * Emits an event to the listener. 
89          * 
90          * NOTE, Event instance can be emited only once. This method writes to the
91          * e.reference field. 
92          *  
93          * @param e
94          */
95         public void emitEvent(final Event e) {
96 //              e.reference = ChildReference.concatenate(path, e.reference);\r
97                 if (executor == null) {
98                         listener.onEvents( Collections.singleton(e) );\r
99                 } else {\r
100                         executor.execute( new Runnable() {\r
101                                 public void run() {\r
102                                         listener.onEvents( Collections.singleton(e) );\r
103                                 }});\r
104                 }
105         }
106 \r
107         public void emitEvents(final Collection<Event> events) {\r
108 //              for (Event e : events) {\r
109 //                      e.reference = ChildReference.concatenate(path, e.reference);\r
110 //              }\r
111                 if (executor == null) {\r
112                         listener.onEvents( events );\r
113                 } else {\r
114                         executor.execute( new Runnable() {\r
115                                 public void run() {\r
116                                         listener.onEvents( events );\r
117                                 }});\r
118                 }\r
119         }\r
120         
121 }
122