Parent: [a7a9ff] (diff)

Download this file

AbstractReader.java    176 lines (159 with data), 5.2 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
/*
* 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.util.ArrayList;
import java.util.List;
import squirrel.model.Annotation;
import squirrel.model.AnnotationFactory;
import squirrel.model.SensorDatum;
import squirrel.util.Range;
/**
* The abstract class Reader for Reading the different files
* (CSV/Binary/DataBase)
* @author marc
*
*/
public abstract class AbstractReader<T extends DataColumn> implements Reader {
protected AnnotationFactory aF;
protected Range bufferRange;
protected List<SensorAnnotationTuple> bufferTuple;
protected DataSource<T> dataSource;
/**
* Constructs a new AbstractReader with the given {@code dataSource}.
* @param dataSource contains descriptions for each column and the path of
* the source file.
*/
public AbstractReader(DataSource<T> dataSource) {
aF = dataSource.getAnnotationFactory();
this.dataSource = dataSource;
}
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#readSensorData(squirrel.util.Range)
*/
@Override
public List<SensorDatum> readSensorData(Range range) {
if (!range.equals(bufferRange)) {
readData(range);
}
assert bufferTuple != null;
List<SensorDatum> listDatum = new ArrayList<>();
if(bufferTuple==null){
return listDatum;
}
for (SensorAnnotationTuple b : bufferTuple) {
listDatum.add(b.getSensorDatum());
}
return listDatum;
}
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#readAnnotations(squirrel.util.Range)
*/
@Override
public List<Annotation[]> readAnnotations(Range range) {
if (!range.equals(bufferRange)) {
readData(range);
}
assert bufferTuple != null;
List<Annotation[]> listAnno = new ArrayList<>();
if(bufferTuple==null){
return listAnno;
}
for (SensorAnnotationTuple b : bufferTuple) {
listAnno.add(b.getAnnotation());
}
return listAnno;
}
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#setaF(squirrel.model.AnnotationFactory)
*/
@Override
public void setaF(AnnotationFactory aF) {
this.aF = aF;
}
/**
* Returns the position of the timestamp in the description
* @return the position of the timestamp in the description
*/
protected int posOfTimestamp() {
for (DataColumn dc : dataSource) {
if (dc.getType().isTimeStamp())
return dataSource.indexOf(dc);
}
throw new IllegalArgumentException("Description needs a Timestamp");
}
/**
* Return the Annotation
* @param range range containing start and end of the Annotation
* @param value the value for the Annotation
* @param dataColumn description of the column
* @param trackNumber the number of the track for the annotation
* @return an Annotation for the value
*/
protected Annotation getAnnotation(Range range, String value, DataColumn dataColumn,
int trackNumber) {
if (value.equals("")) {
//why equals and then null
//return null;
}
switch (dataColumn.getType()) {
case FLOATING_POINT_ANNOTATION:
return aF.createFloatingpointAnnotation(trackNumber, range, Float.parseFloat(value));
case INTEGER_ANNOTATION:
return aF.createIntegerAnnotation(trackNumber, range, Integer.parseInt(value));
case LABEL_ANNOTATION:
return aF.createLabelAnnotation(trackNumber, range, value);
default:
throw new UnknownError("Wrong use of getAnnotation");
}
}
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#readData(squirrel.util.Range)
*/
@Override
public abstract Iterable<SensorAnnotationTuple> readData(Range range);
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#averageDistance()
*/
@Override
public abstract double averageDistance();
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#startTimeStamp()
*/
@Override
public abstract long startTimeStamp();
/*
* (non-Javadoc)
*
* @see squirrel.model.io.IReader#endTimeStamp()
*/
@Override
public abstract long endTimeStamp();
}