1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.accessor.impl;
14 import java.util.Collection;
\r
15 import java.util.Collections;
\r
16 import java.util.concurrent.Executor;
\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
24 * ListenerEntry is a linked list of <Listener, InterestSet> pairs
26 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28 public class ListenerEntry {
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;
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;
42 this.executor = executor;
\r
46 * Add new entry to listener list
51 * @return the new assignment for the list
53 public static ListenerEntry link(ListenerEntry prevValue, Listener listener, InterestSet interestSet, ChildReference path, Executor executor) {
54 ListenerEntry e = new ListenerEntry(listener, interestSet, path, executor);
59 @SuppressWarnings("unchecked")
60 public <T extends InterestSet> T getInterestSet() {
61 return (T) interestSet;
66 * Remove listener entry from linked list
69 * @param listener the value to be removed
70 * @return the new assignment for the list
72 public static ListenerEntry remove(ListenerEntry firstEntry, Listener listener) {
73 ListenerEntry e = firstEntry;
74 ListenerEntry p = null;
76 if (e.listener == listener) {
77 if (p==null) return e.next;
88 * Emits an event to the listener.
90 * NOTE, Event instance can be emited only once. This method writes to the
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
100 executor.execute( new Runnable() {
\r
101 public void run() {
\r
102 listener.onEvents( Collections.singleton(e) );
\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
111 if (executor == null) {
\r
112 listener.onEvents( events );
\r
114 executor.execute( new Runnable() {
\r
115 public void run() {
\r
116 listener.onEvents( events );
\r