1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.simulation.data;
13 import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReentrantLock;
16 import org.simantics.utils.datastructures.ListenerList;
19 * @author Toni Kalajainen
21 public abstract class AbstractDatasource implements Datasource {
23 protected ListenerList<DatasourceListener> listeners = new ListenerList<>(DatasourceListener.class);
24 protected Lock readLock, writeLock;
26 public AbstractDatasource() {
27 // ReentrantReadWriteLock l = new ReentrantReadWriteLock();
28 // this.readLock = l.readLock();
29 // this.writeLock = l.writeLock();
30 this.readLock = this.writeLock = new ReentrantLock();
34 public void addListener(DatasourceListener listener) {
35 listeners.add(listener);
39 public void removeListener(DatasourceListener listener) {
40 listeners.remove(listener);
43 protected void notifyStep(Datasource source) {
44 for (DatasourceListener l : listeners.getListeners()) {
45 if (l.getExecutor() == null) {
48 l.getExecutor().execute(() -> l.onStep(source));
53 protected void notifyStep() {
54 notifyStep(AbstractDatasource.this);
58 public Lock readLock() {