Parent: [a7a9ff] (diff)

Download this file

AnnotationFactory.java    262 lines (233 with data), 9.1 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
/*
* 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;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import squirrel.util.Range;
/**
* This class knows the allowed ranges for the different annotation types and
* provides methods to create certain annotations. This assures, that no invalid
* annotation is existent.
*
* @author Olivier
*
*/
public class AnnotationFactory implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Integer: Track-Index, Double: value
*/
private Map<Integer, Double> minValueFloating;
private Map<Integer, Double> maxValueFloating;
private Map<Integer, Integer> minValueInteger;
private Map<Integer, Integer> maxValueInteger;
/**
* Integer: track-index, List: allowed values
*/
private Map<Integer, List<String>> allowedValues;
/**
* Constructs a new AnnotationFactory() and initializes the private maps.
*/
public AnnotationFactory() {
minValueFloating = new HashMap<Integer, Double>();
maxValueFloating = new HashMap<Integer, Double>();
minValueInteger = new HashMap<Integer, Integer>();
maxValueInteger = new HashMap<Integer, Integer>();
allowedValues = new HashMap<Integer, List<String>>();
}
/**
* Set the minimum floating point {@code value} for the track
* {@code trackNo}.
* @param trackNo track number
* @param value min floating point value
*/
public void setMinValueFloating(int trackNo, double value) {
this.minValueFloating.put(trackNo, value);
}
public double getMinValueFloating(int trackNo) {
return this.minValueFloating.get(trackNo);
}
/**
* Set the maximum floating point {@code value} for the track
* {@code trackNo}.
* @param trackNo track number
* @param value max floating point value
*/
public void setMaxValueFloating(int trackNo, double value) {
this.maxValueFloating.put(trackNo, value);
}
public double getMaxValueFloating(int trackNo) {
return this.maxValueFloating.get(trackNo);
}
/**
* Set the minimum Integer {@code value} for the track {@code trackNo}.
* @param trackNo track number
* @param value min integer value
*/
public void setMinValueInteger(int trackNo, int value) {
this.minValueInteger.put(trackNo, value);
}
public int getMinValueInteger(int trackNo) {
return this.minValueInteger.get(trackNo);
}
/**
* Set the maximum Integer {@code value} for the track {@code trackNo}.
* @param trackNo track number
* @param value max integer value
*/
public void setMaxValueInteger(int trackNo, int value) {
this.maxValueInteger.put(trackNo, value);
}
public int getMaxValueInteger(int trackNo) {
return this.maxValueInteger.get(trackNo);
}
/**
* Add a list of Strings which represent the allowed {@code values} to a
* specific {@code trackNo}. If the given track number does not exist, it
* will be created with the given list of string values.
*
* @param trackNo track number
* @param values list of strings
*/
public void addAllowedValues(int trackNo, List<String> values) {
if (this.allowedValues.containsKey(trackNo)) {
List<String> vals = this.allowedValues.get(trackNo);
for (String s : values) {
if (!vals.contains(s)) {
this.allowedValues.get(trackNo).add(s);
}
}
} else {
this.allowedValues.put(trackNo, values);
}
}
/**
* Returns a map which contains a list of allowed values for each track.
* @return {@code Map<Integer, List<String>>}, a list of allowed values for
* each track.
*/
public Map<Integer, List<String>> getAllowedValues() {
return allowedValues;
}
/**
* Returns a list of allowed values for the given {@code trackNo}.
* @param trackNo track number
* @return {@code List<String>} of allowed values for {@code trackNo}.
*/
public List<String> getAllowedValues(int trackNo) {
return this.allowedValues.get(trackNo);
}
/**
* Checks whether the float is in a valid range and if the track number
* exists.
* @param trackNo track number
* @param val float value
* @return if {@code val} is in valid range and if {@code trackNo} exists
*/
private boolean isValidFloat(int trackNo, double val) {
return (minValueFloating.containsKey(trackNo) && val >= minValueFloating.get(trackNo)
&& maxValueFloating.containsKey(trackNo) && val <= maxValueFloating.get(trackNo));
}
/**
* Checks whether the integer is in a valid range and if the track number
* exists.
* @param trackNo track number
* @param val integer value
* @return if {@code val} is in valid range and if {@code trackNo} exists
*/
private boolean isValidInt(int trackNo, int val) {
return (minValueInteger.containsKey(trackNo) && val >= minValueInteger.get(trackNo)
&& maxValueInteger.containsKey(trackNo) && val <= maxValueInteger.get(trackNo));
}
/**
* Checks whether the string label is allowed and if the track number
* exists.
* @param trackNo track number
* @param label string label
* @return if {@code label} is an allowed label and if {@code trackNo}
* exists
*/
private boolean isValidLabel(int trackNo, String label) {
return (this.allowedValues.containsKey(trackNo)
&& this.allowedValues.get(trackNo).contains(label));
}
/**
* Creates a floating point annotation of type
* {@link FloatingpointAnnotation} with the given track number, start and
* end range for the annotation and the floating point value as annotation.
* @param trackNo track number
* @param range start and end of the annotation
* @param value value of the floating point annotation
* @return {@code FloatingpointAnnotation}, {@code null} if invalid input
*/
public FloatingpointAnnotation createFloatingpointAnnotation(int trackNo, Range range,
double value) {
FloatingpointAnnotation fpAnno = null;
if (isValidFloat(trackNo, value)) {
fpAnno = new FloatingpointAnnotation(range, value, trackNo);
}
return fpAnno;
}
/**
* Creates an integer annotation of type {@link IntegerAnnotation} with the
* given track number, start and end range for the annotation and the
* integer value as annotation.
* @param trackNo track number
* @param range start and end of the annotation
* @param value value of the integer annotation
* @return {@code IntegerAnnotation}, {@code null} if invalid input
*/
public IntegerAnnotation createIntegerAnnotation(int trackNo, Range range, int value) {
IntegerAnnotation intAnno = null;
if (isValidInt(trackNo, value)) {
intAnno = new IntegerAnnotation(range, value, trackNo);
}
return intAnno;
}
/**
* Creates a label annotation of type {@link LabelAnnotation} with the given
* track number, start and end range for the annotation and the label as
* annotation.
* @param trackNo track number
* @param range start and end of the annotation
* @param value string value of the label annotation
* @return {@code LabelAnnotation}, {@code null} if invalid input
*/
public LabelAnnotation createLabelAnnotation(int trackNo, Range range, String label) {
LabelAnnotation labelAnno = null;
if (isValidLabel(trackNo, label)) {
labelAnno = new LabelAnnotation(range, label, trackNo);
}
return labelAnno;
}
public TrainAnnotation createTrainAnnotation(int trackNo, Range range) {
return new TrainAnnotation(range, trackNo);
}
@Override
public String toString() {
return String.format(
"[AF> minF: %s | maxF: %s | minI: %s | maxI: %s | aVal: %s]",
minValueFloating, maxValueFloating, minValueInteger, maxValueInteger, allowedValues);
}
}