-
Notifications
You must be signed in to change notification settings - Fork 90
/
FifoDoubleErrorDataSet.java
286 lines (255 loc) · 9.9 KB
/
FifoDoubleErrorDataSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package de.gsi.dataset.spi;
import java.util.ArrayList;
import java.util.List;
import de.gsi.dataset.AxisDescription;
import de.gsi.dataset.DataSet;
import de.gsi.dataset.DataSet2D;
import de.gsi.dataset.DataSetError;
import de.gsi.dataset.event.AddedDataEvent;
import de.gsi.dataset.event.RemovedDataEvent;
import de.gsi.dataset.event.UpdatedDataEvent;
import de.gsi.dataset.spi.utils.DoublePointError;
import de.gsi.dataset.utils.AssertUtils;
import de.gsi.dataset.utils.LimitedQueue;
/**
* Limited Fifo DoubleErrorDataSet.
* Maximum number of samples and maximum horizontal span are configurable
* @author rstein
*/
public class FifoDoubleErrorDataSet extends AbstractErrorDataSet<DoubleErrorDataSet> implements DataSetError, DataSet2D {
private static final int SAFE_BET = 1;
private static final long serialVersionUID = -7153702141838930486L;
protected final transient LimitedQueue<DataBlob> data;
protected double maxDistance;
/**
* Creates a new instance of <code>FifoDoubleErrorDataSet</code>.
*
* @param name name of this DataSet.
* @param initialSize maximum circular buffer capacity
* @throws IllegalArgumentException if <code>name</code> is <code>null</code>
*/
public FifoDoubleErrorDataSet(final String name, final int initialSize) {
this(name, initialSize, Double.MAX_VALUE);
}
/**
* Creates a new instance of <code>FifoDoubleErrorDataSet</code>.
*
* @param name name of this DataSet.
* @param initialSize maximum circular buffer capacity
* @param maxDistance maximum range before data points are being dropped
* @throws IllegalArgumentException if <code>name</code> is <code>null</code>
*/
public FifoDoubleErrorDataSet(final String name, final int initialSize, final double maxDistance) {
super(name, 2, ErrorType.NO_ERROR, ErrorType.SYMMETRIC);
if (initialSize <= 0) {
throw new IllegalArgumentException("negative or zero initialSize = " + initialSize);
}
if (maxDistance <= 0) {
throw new IllegalArgumentException("negative or zero maxDistance = " + maxDistance);
}
this.maxDistance = maxDistance;
data = new LimitedQueue<>(initialSize);
}
/**
* Add point to the DoublePoints object
*
* @param x the new x coordinate
* @param y the new y coordinate
* @param yErrorNeg the -dy error
* @param yErrorPos the +dy error
* @return itself
*/
public FifoDoubleErrorDataSet add(final double x, final double y, final double yErrorNeg, final double yErrorPos) {
return add(x, y, yErrorNeg, yErrorPos, null);
}
/**
* Add point to the DoublePoints object
*
* @param x the new x coordinate
* @param y the new y coordinate
* @param yErrorNeg the -dy error
* @param yErrorPos the +dy error
* @param tag the data tag
* @return itself
*/
public FifoDoubleErrorDataSet add(final double x, final double y, final double yErrorNeg, final double yErrorPos,
final String tag) {
return add(x, y, yErrorNeg, yErrorPos, tag, null);
}
/**
* Add point to the DoublePoints object
*
* @param x the new x coordinate
* @param y the new y coordinate
* @param yErrorNeg the -dy error
* @param yErrorPos the +dy error
* @param tag the data tag
* @param style the data point style
* @return itself
*/
public FifoDoubleErrorDataSet add(final double x, final double y, final double yErrorNeg, final double yErrorPos,
final String tag, final String style) {
lock().writeLockGuard(() -> {
data.add(new DataBlob(x, y, yErrorNeg, yErrorPos, tag, style));
this.getAxisDescription(DIM_X).add(x);
this.getAxisDescription(DIM_Y).add(y - yErrorNeg);
this.getAxisDescription(DIM_Y).add(y + yErrorPos);
// remove old fields if necessary
expire(x);
});
fireInvalidated(new AddedDataEvent(this));
return this;
}
/**
* <p>
* Initialises the data set with specified data.
* </p>
* Note: The method copies values from specified double arrays.
*
* @param xVals the new x coordinates
* @param yVals the new y coordinates
* @param yErrNeg the +dy errors
* @param yErrPos the -dy errors
* @return itself
*/
public FifoDoubleErrorDataSet add(final double[] xVals, final double[] yVals, final double[] yErrNeg, final double[] yErrPos) {
AssertUtils.notNull("X coordinates", xVals);
AssertUtils.notNull("Y coordinates", yVals);
AssertUtils.notNull("Y error neg", yErrNeg);
AssertUtils.notNull("Y error pos", yErrPos);
final int dataCount = Math.min(Math.min(xVals.length, yVals.length), Math.min(yErrNeg.length, yErrPos.length));
return add(xVals, yVals, yErrNeg, yErrPos, dataCount);
}
/**
* <p>
* Initialises the data set with specified data.
* </p>
* Note: The method copies values from specified double arrays.
*
* @param xValues the new x coordinates
* @param yValues the new y coordinates
* @param yErrorsNeg the -dy errors
* @param yErrorsPos the +dy errors
* @param dataCount maximum number of data points to copy (e.g. in case array store more than needs to be copied)
* @return itself
*/
public FifoDoubleErrorDataSet add(final double[] xValues, final double[] yValues, final double[] yErrorsNeg, final double[] yErrorsPos, final int dataCount) {
lock().writeLockGuard(() -> {
for (int i = 0; i < dataCount; i++) {
this.add(xValues[i], yValues[i], yErrorsNeg[i], yErrorsPos[i]);
}
});
fireInvalidated(new AddedDataEvent(this));
return this;
}
/**
* expire data points that are older than now minus length of the buffer, notifies a 'fireInvalidated()' in case
* data has been removed
*
* @param now the newest time-stamp
* @return number of items that have been removed
*/
public int expire(final double now) {
final int dataPointsToRemove = lock().writeLockGuard(() -> {
final List<DataBlob> toRemoveList = new ArrayList<>(SAFE_BET);
for (final DataBlob blob : data) {
final double x = blob.getX();
if (!Double.isFinite(x) || Math.abs(now - x) > maxDistance) {
toRemoveList.add(blob);
}
}
if (!toRemoveList.isEmpty()) {
// remove elements and invalidate ranges if necessary
data.removeAll(toRemoveList);
getAxisDescriptions().forEach(AxisDescription::clear);
}
return toRemoveList.size();
});
if (dataPointsToRemove != 0) {
fireInvalidated(new RemovedDataEvent(this, "expired data"));
}
return dataPointsToRemove;
}
@Override
public final double get(final int dimIndex, final int index) {
return dimIndex == DataSet.DIM_X ? data.get(index).getX() : data.get(index).getY();
}
/**
* @return the internal data container (N.B. this is not thread-safe)
*/
public LimitedQueue<DataBlob> getData() {
return data;
}
@Override
public int getDataCount() {
return data.size();
}
@Override
public String getDataLabel(final int index) {
return data.get(index).getDataLabel();
}
@Override
public double getErrorNegative(final int dimIndex, final int index) {
return dimIndex == DIM_X ? 0.0 : data.get(index).getErrorX();
}
@Override
public double getErrorPositive(final int dimIndex, final int index) {
return dimIndex == DIM_X ? 0.0 : data.get(index).getErrorY();
}
/**
* @return maximum range before data points are being dropped
*/
public double getMaxDistance() {
return maxDistance;
}
@Override
public String getStyle(final int index) {
return data.get(index).getStyle();
}
/**
* remove all data points
*/
public void reset() {
data.clear();
fireInvalidated(new RemovedDataEvent(this, "reset"));
}
/**
* @param maxDistance maximum range before data points are being dropped
*/
public void setMaxDistance(final double maxDistance) {
this.maxDistance = maxDistance;
}
protected static class DataBlob extends DoublePointError {
protected String style;
protected String tag;
protected DataBlob(final double x, final double y, final double errorYNeg, final double errorYPos, final String tag, final String style) {
//noinspection SuspiciousNameCombination
super(x, y, errorYNeg, errorYPos); // NOPMD NOSONAR - super's x/y error is reinterpreted as +ey -ey in this class
this.tag = tag;
this.style = style;
}
public String getDataLabel() {
return tag;
}
public String getStyle() {
return style;
}
}
@Override
public DataSet set(final DataSet other, final boolean copy) {
lock().writeLockGuard(() -> other.lock().writeLockGuard(() -> {
this.reset();
// copy data
final int count = other.getDataCount();
if (other instanceof DataSetError) {
this.add(other.getValues(DIM_X), other.getValues(DIM_Y), ((DataSetError) other).getErrorsNegative(DIM_Y), ((DataSetError) other).getErrorsPositive(DIM_Y), other.getDataCount());
} else {
this.add(other.getValues(DIM_X), other.getValues(DIM_Y), new double[count], new double[count], other.getDataCount());
}
copyMetaData(other);
copyDataLabelsAndStyles(other, copy);
copyAxisDescription(other);
}));
return fireInvalidated(new UpdatedDataEvent(this, "set(DataSet, boolean=" + copy + ")"));
}
}