]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/HistorySamplerItem2.java
Merge "Added another sampling method to HistorySampler"
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / HistorySamplerItem2.java
1 package org.simantics.history;
2
3 import java.util.Arrays;
4
5 import org.simantics.databoard.accessor.StreamAccessor;
6 import org.simantics.databoard.accessor.error.AccessorException;
7 import org.simantics.history.util.StreamIterator;
8
9 public class HistorySamplerItem2 implements Comparable<HistorySamplerItem2> {
10
11         public static class LevelItem implements Comparable<LevelItem> {
12                 public final String id;
13                 public final double samplingInterval;
14
15                 public LevelItem(String id, double samplingInterval) {
16                         this.id = id;
17                         this.samplingInterval = samplingInterval;
18                 }
19
20                 @Override
21                 public int compareTo(LevelItem o) {
22                         int i = id.compareTo(o.id);
23                         return i != 0 ? i : Double.compare(samplingInterval, o.samplingInterval);
24                 }
25         }
26
27         Collector collector;
28         HistoryManager history;         // History source for this item
29         LevelItem[] items;
30
31         // State data
32         StreamAccessor accessor;                        // Stream accessor
33         public StreamIterator iter;
34         public int chartDataId;
35
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;
41                 this.items = items;
42                 this.chartDataId = identityHashCode;
43         }
44
45         public void open() throws HistoryException {
46                 accessor = history.openStream(items[0].id, "r");
47                 iter = new StreamIterator( accessor );
48         }
49
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 );
54         }
55
56         public void close() {
57                 if (accessor!=null) {
58                         try {
59                                 accessor.close();
60                         } catch (AccessorException e) {
61                         }
62                 }
63                 accessor = null;
64                 iter = null;
65         }
66
67         @Override
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]);
72                         if (i != 0)
73                                 return i;
74                 }
75                 return 0;
76         }
77
78         @Override
79         public int hashCode() {
80                 int code = 0x2304;
81                 code = 13*code + Arrays.hashCode(items);
82                 code = 13*code + history.hashCode();
83                 return code;
84         }
85
86         @Override
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;
93                 return true;
94         }
95
96         private LevelItem getFormat(double pixelsPerSecond) throws HistoryException {
97                 LevelItem result = null;
98                 for (LevelItem format : items) {
99                         double interval = format.samplingInterval;
100                         if (Double.isNaN( interval ) || interval <= pixelsPerSecond) {
101                                 result = format;
102                         } else {
103                                 break;
104                         }
105                 }
106                 return result;
107         }
108
109 }