]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/ListenerEntry.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / ListenerEntry.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.accessor.impl;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.concurrent.Executor;
17
18 import org.simantics.databoard.accessor.Accessor.Listener;
19 import org.simantics.databoard.accessor.event.Event;
20 import org.simantics.databoard.accessor.interestset.InterestSet;
21 import org.simantics.databoard.accessor.reference.ChildReference;
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;
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;
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);
97                 if (executor == null) {
98                         listener.onEvents( Collections.singleton(e) );
99                 } else {
100                         executor.execute( new Runnable() {
101                                 public void run() {
102                                         listener.onEvents( Collections.singleton(e) );
103                                 }});
104                 }
105         }
106
107         public void emitEvents(final Collection<Event> events) {
108 //              for (Event e : events) {
109 //                      e.reference = ChildReference.concatenate(path, e.reference);
110 //              }
111                 if (executor == null) {
112                         listener.onEvents( events );
113                 } else {
114                         executor.execute( new Runnable() {
115                                 public void run() {
116                                         listener.onEvents( events );
117                                 }});
118                 }
119         }
120         
121 }
122