View Javadoc
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   * Class to facilitate JUNITS types in extended data.
13   * <p>
14   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
19   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
20   * @param <U> unit
21   * @param <T> type in vector
22   * @param <O> output vector type
23   * @param <G> GTU data type
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       * Constructor.
30       * @param id id
31       * @param description description
32       * @param type type class
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       * Convert float to typed value.
83       * @param value float value
84       * @return typed value
85       */
86      protected abstract T convertValue(float value);
87  
88      @Override
89      public O convert(final float[] storage, final int size)
90      {
91          // cut array to size and delegate
92          return convert(Arrays.copyOf(storage, size));
93      }
94  
95      /**
96       * {@inheritDoc}
97       * @param string stored string representation without unit
98       */
99      @Override
100     public abstract T parseValue(String string);
101 
102     /**
103      * Convert float array to typed array.
104      * @param storage float array storage
105      * @return typed array
106      */
107     protected abstract O convert(float[] storage);
108 
109 }