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