1 package org.simantics.history;
3 import java.util.Arrays;
5 import org.simantics.databoard.accessor.StreamAccessor;
6 import org.simantics.databoard.accessor.error.AccessorException;
7 import org.simantics.history.util.StreamIterator;
9 public class HistorySamplerItem2 implements Comparable<HistorySamplerItem2> {
11 public static class LevelItem implements Comparable<LevelItem> {
12 public final String id;
13 public final double samplingInterval;
15 public LevelItem(String id, double samplingInterval) {
17 this.samplingInterval = samplingInterval;
21 public int compareTo(LevelItem o) {
22 int i = id.compareTo(o.id);
23 return i != 0 ? i : Double.compare(samplingInterval, o.samplingInterval);
28 HistoryManager history; // History source for this item
32 StreamAccessor accessor; // Stream accessor
33 public StreamIterator iter;
34 public int chartDataId;
36 public HistorySamplerItem2(Collector collector, HistoryManager history, LevelItem[] items, int identityHashCode) {
37 if (items.length == 0)
38 throw new IllegalArgumentException("Must have at least one existing history item to sample, zero provided");
39 this.collector = collector;
40 this.history = history;
42 this.chartDataId = identityHashCode;
45 public void flush() throws HistoryException {
46 if (collector != null)
50 public void open() throws HistoryException {
51 accessor = history.openStream(items[0].id, "r");
52 iter = new StreamIterator( accessor );
55 public void open(double pixelsPerSecond) throws HistoryException {
56 LevelItem f = getFormat(pixelsPerSecond);
57 accessor = history.openStream(f.id, "r");
58 iter = new StreamIterator( accessor );
65 } catch (AccessorException e) {
73 public int compareTo(HistorySamplerItem2 o) {
74 int m = Math.min(items.length, o.items.length);
75 for (int j = 0; j < m; ++j) {
76 int i = items[j].compareTo(o.items[j]);
84 public int hashCode() {
86 code = 13*code + Arrays.hashCode(items);
87 code = 13*code + history.hashCode();
92 public boolean equals(Object obj) {
93 if ( obj == null ) return false;
94 if ( obj instanceof HistorySamplerItem2 == false ) return false;
95 HistorySamplerItem2 other = (HistorySamplerItem2) obj;
96 if ( !other.history.equals(history) ) return false;
97 if ( !Arrays.equals(other.items, items) ) return false;
101 private LevelItem getFormat(double secondsPerPixel) throws HistoryException {
102 LevelItem result = null;
103 for (LevelItem format : items) {
104 double interval = format.samplingInterval;
105 if (Double.isNaN( interval ) || interval <= secondsPerPixel) {