]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/data/AbstractDatasource.java
Simulator toolkit enhancements
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / data / AbstractDatasource.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.simulation.data;
12
13 import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReentrantLock;
15
16 import org.simantics.utils.datastructures.ListenerList;
17
18 /**
19  * @author Toni Kalajainen
20  */
21 public abstract class AbstractDatasource implements Datasource {
22
23     protected ListenerList<DatasourceListener> listeners = new ListenerList<>(DatasourceListener.class);
24     protected Lock readLock, writeLock;
25
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();
31     }
32
33     @Override
34     public void addListener(DatasourceListener listener) {
35         listeners.add(listener);
36     }
37
38     @Override
39     public void removeListener(DatasourceListener listener) {
40         listeners.remove(listener);
41     }
42
43     protected void notifyStep(Datasource source) {
44         for (DatasourceListener l : listeners.getListeners()) {
45             if (l.getExecutor() == null) {
46                 l.onStep( source );
47             } else {
48                 l.getExecutor().execute(() -> l.onStep(source));
49             }
50         }
51     }
52
53     protected void notifyStep() {
54         notifyStep(AbstractDatasource.this);
55     }
56
57     @Override
58     public Lock readLock() {
59         return readLock;
60     }
61
62 }