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 open() throws HistoryException {
46 accessor = history.openStream(items[0].id, "r");
47 iter = new StreamIterator( accessor );
50 public void open(double pixelsPerSecond) throws HistoryException {
51 LevelItem f = getFormat(pixelsPerSecond);
52 accessor = history.openStream(f.id, "r");
53 iter = new StreamIterator( accessor );
60 } catch (AccessorException e) {
68 public int compareTo(HistorySamplerItem2 o) {
69 int m = Math.min(items.length, o.items.length);
70 for (int j = 0; j < m; ++j) {
71 int i = items[j].compareTo(o.items[j]);
79 public int hashCode() {
81 code = 13*code + Arrays.hashCode(items);
82 code = 13*code + history.hashCode();
87 public boolean equals(Object obj) {
88 if ( obj == null ) return false;
89 if ( obj instanceof HistorySamplerItem2 == false ) return false;
90 HistorySamplerItem2 other = (HistorySamplerItem2) obj;
91 if ( !other.history.equals(history) ) return false;
92 if ( !Arrays.equals(other.items, items) ) return false;
96 private LevelItem getFormat(double secondsPerPixel) throws HistoryException {
97 LevelItem result = null;
98 for (LevelItem format : items) {
99 double interval = format.samplingInterval;
100 if (Double.isNaN( interval ) || interval <= secondsPerPixel) {