Parent: [a7a9ff] (diff)

Download this file

DataColumn.java    438 lines (373 with data), 11.9 kB

  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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.model.io;
import java.io.Serializable;
/**
* This class represents a description of a column in a file.
* @author Olivier
*
*/
public class DataColumn implements Serializable, Cloneable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Represents all allowed Types of a column. (Timestamp, Annotation, Sensor,
* Ignore...)
* @author Olivier
*
*/
public enum Type {
TIMESTAMP, FLOATING_POINT_ANNOTATION, INTEGER_ANNOTATION, LABEL_ANNOTATION, SENSOR, IGNORE,
NEW_FLOATING_POINT_ANNOTATION, NEW_INTEGER_ANNOTATION, NEW_LABEL_ANNOTATION, NEW_TRAIN_ANNOTATION;
public boolean isAnnotation() {
return (this == Type.FLOATING_POINT_ANNOTATION ||
this == Type.INTEGER_ANNOTATION ||
this == Type.LABEL_ANNOTATION ||
this == Type.NEW_FLOATING_POINT_ANNOTATION ||
this == Type.NEW_INTEGER_ANNOTATION || this == Type.NEW_LABEL_ANNOTATION ||
this == Type.NEW_TRAIN_ANNOTATION);
}
public boolean isOldAnnotation() {
return (this == Type.FLOATING_POINT_ANNOTATION ||
this == Type.INTEGER_ANNOTATION || this == Type.LABEL_ANNOTATION);
}
public boolean isRangedAnnotation() {
return (this.isFloatingpointAnnotation() || this.isIntegerAnnotation());
}
public boolean isIntegerAnnotation() {
return (this == Type.NEW_INTEGER_ANNOTATION || this == Type.INTEGER_ANNOTATION);
}
public boolean isFloatingpointAnnotation() {
return (this == Type.NEW_FLOATING_POINT_ANNOTATION ||
this == Type.FLOATING_POINT_ANNOTATION);
}
public boolean isLabelAnnotation() {
return (this == Type.LABEL_ANNOTATION || this == Type.NEW_LABEL_ANNOTATION);
}
public boolean isTrainAnnotation() {
return (this == Type.NEW_TRAIN_ANNOTATION);
}
/**
* Returns whether this column contains sensordata.
* @return {@code true} if is a sensordata, {@code false} if not
*/
public boolean isSensor() {
return this == Type.SENSOR;
}
/**
* Returns whether this column contains a timestamp or is part of a
* timestamp.
* @return {@code true} if is timestamp, {@code false} if not
*/
public boolean isTimeStamp() {
return this == Type.TIMESTAMP;
}
/**
* Returns whether this column shall be ignored.
* @return {@code true} if this column shall be ignored, {@code false}
* if not
*/
public boolean ignore() {
return this == Type.IGNORE;
}
/**
* Returns if this column is reserved for a new annotation.
* @return {@code true} if this column is reserved for a new annotation,
* {@code false} if not
*/
public boolean isNewAnnotation() {
return (this == Type.NEW_FLOATING_POINT_ANNOTATION ||
this == Type.NEW_INTEGER_ANNOTATION || this == Type.NEW_LABEL_ANNOTATION || this == Type.NEW_TRAIN_ANNOTATION);
}
}
/**
* This class represents all allowed timeunits.
* @author Olivier
*
*/
public enum Unit {
YEARS {
@Override
public long toMillis(long d) {
return 0;
}
},
MONTHS {
@Override
public long toMillis(long d) {
return 0;
}
},
WEEKS {
@Override
public long toMillis(long d) {
return x(d, C7 / C2, MAX / (C7 / C2));
}
},
DAYS {
@Override
public long toMillis(long d) {
return x(d, C6 / C2, MAX / (C6 / C2));
}
},
HOURS {
@Override
public long toMillis(long d) {
return x(d, C5 / C2, MAX / (C5 / C2));
}
@Override
public long toOldTime(long milli) {
return (long) (milli / (1000 * 60 * 60))%24;
}
@Override
public long toOldTimeMax(long milli){
return (long)(milli / (1000*60*60));
}
},
MINUTES {
@Override
public long toMillis(long d) {
return x(d, C4 / C2, MAX / (C4 / C2));
}
@Override
public long toOldTime(long milli) {
return (milli / (1000 * 60)
) % (60);
}
@Override
public long toOldTimeMax(long milli){
return (long)(milli / (1000*60));
}
},
SECONDS {
@Override
public long toMillis(long d) {
return x(d, C3 / C2, MAX / (C3 / C2));
}
@Override
public long toOldTime(long milli) {
return ((milli / 1000) % (60));
}
@Override
public long toOldTimeMax(long milli){
return (long)(milli / (1000));
}
},
TENTH_SECONDS {
@Override
public long toMillis(long d) {
return x(d, (100L * C2) / C2, MAX / ((100 * C2) / C2));
}
@Override
public long toOldTime(long milli) {
return ((milli / (100)
) % (60));
}
@Override
public long toOldTimeMax(long milli){
return (long)(milli / (100));
}
},
HUNDRETH_SECONDS {
@Override
public long toMillis(long d) {
return x(d, (10L * C2) / C2, MAX / ((10 * C2) / C2));
}
@Override
public long toOldTime(long milli) {
return ((milli / (10)
) % (60));
}
@Override
public long toOldTimeMax(long milli){
return (long)(milli / (10));
}
},
MILLISECONDS {
@Override
public long toMillis(long d) {
return d;
}
@Override
public long toOldTime(long milli) {
return (milli % 1000);
}
@Override
public long toOldTimeMax(long milli){
return milli;
}
},
NONE {
@Override
public long toMillis(long d) {
return 0;
}
};
// Handy constants for conversion methods
static final long C0 = 1L;
static final long C1 = C0 * 1000L;
static final long C2 = C1 * 1000L;
static final long C3 = C2 * 1000L;
static final long C4 = C3 * 60L;
static final long C5 = C4 * 60L;
static final long C6 = C5 * 24L;
static final long C7 = C6 * 7L;
static final long MAX = Long.MAX_VALUE;
/*
* Scale d by m, checking for overflow. This has a short name to make
* above code more readable.
*/
private static long x(long d, long m, long over) {
if (d > over)
return Long.MAX_VALUE;
if (d < -over)
return Long.MIN_VALUE;
return d * m;
}
/**
* Converts the duration to milliseconds.
* @param duration the duration
* @return the converted duration, or Long.MIN_VALUE if conversion would
* negatively overflow, or Long.MAX_VALUE if it would positively
* overflow.
*/
public long toMillis(long duration) {
throw new AbstractMethodError();
}
public long toOldTime(long milli) {
throw new AbstractMethodError();
}
public long toOldTimeMax(long milli){
throw new AbstractMethodError();
}
}
private Type type;
private String name;
private Unit unit;
private int timestampIdx, annotationIdx, sensorIdx;
private boolean isSelected = false;
/**
* Constructs a new DataColumn.
*/
public DataColumn() {}
/**
* Creates a new DataColumn.
* @param type Type of Annotation.
* @param name
*/
public DataColumn(String name, Type type, Unit unit) {
super();
this.type = type;
this.name = name;
this.unit = unit;
this.timestampIdx = -1;
this.annotationIdx = -1;
this.sensorIdx = -1;
if(type == Type.TIMESTAMP)
isSelected = true;
}
public int getTimestampIdx() {
return timestampIdx;
}
public void setTimestampIdx(int timestampIdx) {
this.timestampIdx = timestampIdx;
}
public int getAnnotationIdx() {
return annotationIdx;
}
public void setAnnotationIdx(int annotationIdx) {
this.annotationIdx = annotationIdx;
}
public int getSensorIdx() {
return sensorIdx;
}
public void setSensorIdx(int sensorIdx) {
this.sensorIdx = sensorIdx;
}
/**
* Returns the {@link Type} of this column.
* @return type of this column.
*/
public Type getType() {
return type;
}
/**
* Set the {@link Type} of this column.
* @param type type of this column
*/
public void setType(Type type) {
this.type = type;
if(type == Type.TIMESTAMP)
isSelected = true;
}
/**
* Returns the name of this column.
* @return name of this column
*/
public String getName() {
return name;
}
/**
* Sets the name of this column.
* @param name name of this column
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the {@link Unit} of this column.
* @return unit of this column
*/
public Unit getUnit() {
return unit;
}
/**
* Sets the {@link Unit} of this column.
* @param unit unit of this column
*/
public void setUnit(Unit unit) {
this.unit = unit;
}
@Override
public String toString() {
return String.format(
"[DC> Name: %s | Type: %22s | Unit: %s | #A: %2d | #S: %2d | #T: %2d",
name, type, unit, annotationIdx, sensorIdx, timestampIdx);
}
@Override
public DataColumn clone() {
DataColumn theClone = null;
try {
theClone = (DataColumn) super.clone();
}
catch(CloneNotSupportedException e) {
}
return theClone;
}
public boolean isSelected() {
return isSelected ;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
if(type == Type.TIMESTAMP)
isSelected = true;
}
}