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