1 package nl.tno.imb.mc;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.opentrafficsim.base.modelproperties.CompoundProperty;
9 import org.opentrafficsim.base.modelproperties.Property;
10 import org.opentrafficsim.imb.IMBException;
11
12 import nl.tno.imb.TByteBuffer;
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class Parameter
27 {
28
29 private final String name;
30
31
32 private final Object value;
33
34
35 private final ParameterType type;
36
37
38 private List<String> valueList = null;
39
40
41
42
43
44
45 public Parameter(final String name, final double value)
46 {
47 this.name = name;
48 this.type = ParameterType.FLOAT;
49 this.value = value;
50 }
51
52
53
54
55
56
57 public Parameter(final String name, final Boolean value)
58 {
59 this.name = name;
60 this.type = ParameterType.BOOLEAN;
61 this.value = value;
62 }
63
64
65
66
67
68
69 public Parameter(final String name, final int value)
70 {
71 this.name = name;
72 this.type = ParameterType.INTEGER;
73 this.value = value;
74 }
75
76
77
78
79
80
81 public Parameter(final String name, final String value)
82 {
83 this.name = name;
84 this.type = ParameterType.STRING;
85 this.value = value;
86 }
87
88
89
90
91
92
93 public Parameter (final TByteBuffer payload) throws IMBException
94 {
95 this.name = payload.readString();
96 int valueCode = payload.readInt32();
97 if (ParameterType.BOOLEAN.value == valueCode)
98 {
99 this.type = ParameterType.BOOLEAN;
100 this.value = payload.readBoolean();
101 }
102 else if (ParameterType.FLOAT.value == valueCode)
103 {
104 this.type = ParameterType.FLOAT;
105 this.value = payload.readDouble();
106 }
107 else if (ParameterType.INTEGER.value == valueCode)
108 {
109 this.type = ParameterType.INTEGER;
110 this.value = payload.readInt32();
111 }
112 else if (ParameterType.STRING.value == valueCode)
113 {
114 this.type = ParameterType.STRING;
115 this.value = payload.readString();
116 }
117 else
118 {
119 throw new IMBException("Inhandled type: " + valueCode);
120 }
121 int optionCount = payload.readInt32();
122 if (optionCount > 0)
123 {
124 this.valueList = new ArrayList<String>(optionCount);
125 for (int optionIndex = 0; optionIndex < optionCount; optionIndex++)
126 {
127 this.valueList.add(payload.readString());
128 }
129 }
130 }
131
132
133
134
135
136 public void prepare (final TByteBuffer payload)
137 {
138 payload.prepare(this.name);
139 payload.prepare(this.type.value);
140 switch (this.type)
141 {
142 case BOOLEAN:
143 payload.prepare((boolean) this.value);
144 break;
145
146 case FLOAT:
147 payload.prepare((double) this.value);
148 break;
149
150 case INTEGER:
151 payload.prepare((int) this.value);
152 break;
153
154 case STRING:
155 payload.prepare((String) this.value);
156 break;
157 }
158 if (null != this.valueList && this.valueList.size() > 0)
159 {
160 payload.prepare(this.valueList.size());
161 for (String option : this.valueList)
162 {
163 payload.prepare(option);
164 }
165 }
166 else
167 {
168 payload.prepare(0);
169 }
170 }
171
172
173
174
175
176 public void qWrite (final TByteBuffer payload)
177 {
178 payload.qWrite(this.name);
179 payload.qWrite(this.type.value);
180 switch (this.type)
181 {
182 case BOOLEAN:
183 payload.qWrite((boolean) this.value);
184 break;
185
186 case FLOAT:
187 payload.qWrite((double) this.value);
188 break;
189
190 case INTEGER:
191 payload.qWrite((int) this.value);
192 break;
193
194 case STRING:
195 payload.qWrite((String) this.value);
196 break;
197 }
198 if (null != this.valueList && this.valueList.size() > 0)
199 {
200 payload.qWrite(this.valueList.size());
201 for (String option : this.valueList)
202 {
203 payload.qWrite(option);
204 }
205 }
206 else
207 {
208 payload.qWrite(0);
209 }
210 }
211
212
213
214
215
216 public void setValueList(final List<String> list)
217 {
218 this.valueList = new ArrayList<String>(list);
219 }
220
221
222 @Override
223 public final String toString()
224 {
225 return "Parameter [name=" + this.name + ", value=" + this.value + ", type=" + this.type + "]";
226 }
227
228
229
230
231 public String getName()
232 {
233 return this.name;
234 }
235
236
237
238
239 public Object getValue()
240 {
241 return this.value;
242 }
243
244
245
246
247 public ParameterType getType()
248 {
249 return this.type;
250 }
251
252
253
254
255 public List<String> getValueList()
256 {
257 return this.valueList;
258 }
259
260
261
262
263 public enum ParameterType
264 {
265
266 FLOAT(0),
267
268 BOOLEAN(1),
269
270 INTEGER(2),
271
272 STRING(3);
273
274
275 public final int value;
276
277
278
279
280
281 ParameterType(final int value)
282 {
283 this.value = value;
284 }
285
286 };
287
288
289
290
291
292
293
294
295
296
297
298 class PropertyIterator implements Iterator<Property<?>>, Serializable
299 {
300
301 private static final long serialVersionUID = 20150000L;
302
303
304 private int currentIndex;
305
306
307 private final ArrayList<Property<?>> list;
308
309
310
311
312
313 PropertyIterator(final Property<?> ap)
314 {
315 this.currentIndex = 0;
316 this.list = new ArrayList<Property<?>>();
317 addToList(ap);
318 }
319
320
321
322
323
324
325 private void addToList(final Property<?> cp)
326 {
327 this.list.add(cp);
328 if (cp instanceof CompoundProperty)
329 {
330 for (Property<?> ap : ((CompoundProperty) cp).getValue())
331 {
332 addToList(ap);
333 }
334 }
335 }
336
337
338 @Override
339 public boolean hasNext()
340 {
341 return this.currentIndex < this.list.size();
342 }
343
344
345 @Override
346 public Property<?> next()
347 {
348 return this.list.get(this.currentIndex++);
349 }
350
351
352 @Override
353 public void remove()
354 {
355 throw new UnsupportedOperationException();
356 }
357
358
359 @Override
360 public String toString()
361 {
362 return "PropertyIterator [currentIndex=" + this.currentIndex + ", list=" + this.list + "]";
363 }
364
365 }
366
367
368 }
369