1 package org.opentrafficsim.kpi.sampling.data;
2
3 import java.util.Arrays;
4
5 import org.djunits.unit.Unit;
6 import org.djunits.value.ValueException;
7 import org.djunits.value.vfloat.scalar.AbstractFloatScalar;
8 import org.djunits.value.vfloat.vector.AbstractFloatVector;
9 import org.djutils.exceptions.Throw;
10 import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
11 import org.opentrafficsim.kpi.sampling.SamplingException;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public abstract class ExtendedDataTypeFloat<U extends Unit<U>, T extends AbstractFloatScalar<U, T>,
29 O extends AbstractFloatVector<U, O>, G extends GtuDataInterface> extends ExtendedDataType<T, O, float[], G>
30 {
31
32
33
34
35 public ExtendedDataTypeFloat(final String id)
36 {
37 super(id);
38 }
39
40
41 @Override
42 public final float[] setValue(final float[] storage, final int i, final T value)
43 {
44 float[] out;
45 if (i == storage.length)
46 {
47 int cap = (i - 1) + ((i - 1) >> 1);
48 out = Arrays.copyOf(storage, cap);
49 }
50 else
51 {
52 out = storage;
53 }
54 out[i] = value.si;
55 return out;
56 }
57
58
59 @Override
60 public final float[] initializeStorage()
61 {
62 return new float[10];
63 }
64
65
66 @Override
67 public final String formatValue(final String format, final T value)
68 {
69 return String.format(format, value.getSI());
70 }
71
72
73 @SuppressWarnings("unchecked")
74 @Override
75 public final T getOutputValue(final O output, final int i) throws SamplingException
76 {
77 try
78 {
79 return (T) output.get(i);
80 }
81 catch (ValueException exception)
82 {
83 throw new SamplingException("Index out of range.", exception);
84 }
85 }
86
87
88 @Override
89 public T getStorageValue(final float[] storage, final int i) throws SamplingException
90 {
91 Throw.when(i < 0 || i >= storage.length, SamplingException.class, "Index %d out of range.", i);
92 return convertValue(storage[i]);
93 }
94
95
96
97
98
99
100 protected abstract T convertValue(final float value);
101
102
103 @Override
104 public O convert(final float[] storage, final int size)
105 {
106 try
107 {
108
109 return convert(Arrays.copyOf(storage, size));
110 }
111 catch (ValueException exception)
112 {
113 throw new RuntimeException("Could not create typed vector from float array.", exception);
114 }
115 }
116
117
118
119
120
121
122
123 protected abstract O convert(final float[] storage) throws ValueException;
124
125 }