]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java
Tested and fixed utility for truncating history data.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / ChartData.java
index b44556b633713f976ce7ec437f715d86919dd287..e85c080370e0b2d774aa0b9c174b8424fae84c13 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/
 package org.simantics.charts.editor;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.simantics.databoard.annotations.Optional;
 import org.simantics.db.Resource;
 import org.simantics.history.Collector;
@@ -58,6 +60,13 @@ public final class ChartData {
     @Optional
     public Collector      collector;
 
+    /**
+     * This (shared instance) is used to track the amount of users of this ChartData
+     * instance. The same instance may end up in many ChartData instances through
+     * {@link #readFrom(ChartData)}.
+     */
+    public AtomicInteger  refCount = new AtomicInteger();
+
     public ChartData(Resource model, Resource run, IExperiment experiment, Datasource datasource, HistoryManager history, Collector collector) {
         this.model = model;
         this.run = run;
@@ -75,6 +84,7 @@ public final class ChartData {
                this.datasource = null;
                this.history = null;
                this.collector = null;
+               this.refCount = null;
        } else {
                this.model = other.model;
                this.run = other.run;
@@ -82,6 +92,7 @@ public final class ChartData {
                this.datasource = other.datasource;
                this.history = other.history;
                this.collector = other.collector;
+               this.refCount = other.refCount;
        }
     }
 
@@ -99,4 +110,38 @@ public final class ChartData {
        }
     }
 
+    public int reference() {
+        AtomicInteger i = refCount;
+        if (i == null)
+            return 0;
+        int result = i.incrementAndGet();
+        //System.out.println(this + ": reference: " + (result-1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+        return result;
+    }
+
+    public int dereference() {
+        AtomicInteger i = refCount;
+        if (i == null)
+            return 0;
+        int result = i.decrementAndGet();
+        //System.out.println(this + ": dereference: " + (result+1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+        if (result <= 0) {
+            synchronized (i) {
+                i.notifyAll();
+            }
+        }
+        return result;
+    }
+
+    public void waitUntilNotReferenced() throws InterruptedException {
+        AtomicInteger i = refCount;
+        if (i == null)
+            return;
+        synchronized (i) {
+            while (i.get() > 0) {
+                i.wait();
+            }
+        }
+    }
+
 }