X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.history%2Fsrc%2Forg%2Fsimantics%2Fhistory%2FHistorySamplerItem2.java;fp=bundles%2Forg.simantics.history%2Fsrc%2Forg%2Fsimantics%2Fhistory%2FHistorySamplerItem2.java;h=73d89fa56a1aed8d73bed10724f12fe4417f4d40;hb=f3617565a8ffe49d63016f0c1d78e0d4282214b0;hp=0000000000000000000000000000000000000000;hpb=f62bab9b78e60b94e055d51db98a03141415323e;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.history/src/org/simantics/history/HistorySamplerItem2.java b/bundles/org.simantics.history/src/org/simantics/history/HistorySamplerItem2.java new file mode 100644 index 000000000..73d89fa56 --- /dev/null +++ b/bundles/org.simantics.history/src/org/simantics/history/HistorySamplerItem2.java @@ -0,0 +1,109 @@ +package org.simantics.history; + +import java.util.Arrays; + +import org.simantics.databoard.accessor.StreamAccessor; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.history.util.StreamIterator; + +public class HistorySamplerItem2 implements Comparable { + + public static class LevelItem implements Comparable { + public final String id; + public final double samplingInterval; + + public LevelItem(String id, double samplingInterval) { + this.id = id; + this.samplingInterval = samplingInterval; + } + + @Override + public int compareTo(LevelItem o) { + int i = id.compareTo(o.id); + return i != 0 ? i : Double.compare(samplingInterval, o.samplingInterval); + } + } + + Collector collector; + HistoryManager history; // History source for this item + LevelItem[] items; + + // State data + StreamAccessor accessor; // Stream accessor + public StreamIterator iter; + public int chartDataId; + + public HistorySamplerItem2(Collector collector, HistoryManager history, LevelItem[] items, int identityHashCode) { + if (items.length == 0) + throw new IllegalArgumentException("Must have at least one existing history item to sample, zero provided"); + this.collector = collector; + this.history = history; + this.items = items; + this.chartDataId = identityHashCode; + } + + public void open() throws HistoryException { + accessor = history.openStream(items[0].id, "r"); + iter = new StreamIterator( accessor ); + } + + public void open(double pixelsPerSecond) throws HistoryException { + LevelItem f = getFormat(pixelsPerSecond); + accessor = history.openStream(f.id, "r"); + iter = new StreamIterator( accessor ); + } + + public void close() { + if (accessor!=null) { + try { + accessor.close(); + } catch (AccessorException e) { + } + } + accessor = null; + iter = null; + } + + @Override + public int compareTo(HistorySamplerItem2 o) { + int m = Math.min(items.length, o.items.length); + for (int j = 0; j < m; ++j) { + int i = items[j].compareTo(o.items[j]); + if (i != 0) + return i; + } + return 0; + } + + @Override + public int hashCode() { + int code = 0x2304; + code = 13*code + Arrays.hashCode(items); + code = 13*code + history.hashCode(); + return code; + } + + @Override + public boolean equals(Object obj) { + if ( obj == null ) return false; + if ( obj instanceof HistorySamplerItem2 == false ) return false; + HistorySamplerItem2 other = (HistorySamplerItem2) obj; + if ( !other.history.equals(history) ) return false; + if ( !Arrays.equals(other.items, items) ) return false; + return true; + } + + private LevelItem getFormat(double pixelsPerSecond) throws HistoryException { + LevelItem result = null; + for (LevelItem format : items) { + double interval = format.samplingInterval; + if (Double.isNaN( interval ) || interval <= pixelsPerSecond) { + result = format; + } else { + break; + } + } + return result; + } + +}