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.djutils.exceptions.Throw;
10  import org.opentrafficsim.kpi.interfaces.GtuData;
11  import org.opentrafficsim.kpi.sampling.SamplingException;
12  
13  /**
14   * Class to facilitate JUNITS types in extended data.
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
21   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22   * @param <U> unit
23   * @param <T> type in vector
24   * @param <O> vector type
25   * @param <G> gtu data type
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       * Constructor setting the id.
32       * @param id String; id
33       * @param description String; description
34       * @param type Class&lt;T&gt;; type class
35       */
36      public ExtendedDataFloat(final String id, final String description, final Class<T> type)
37      {
38          super(id, description, type);
39      }
40  
41      /** {@inheritDoc} */
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      /** {@inheritDoc} */
60      @Override
61      public final float[] initializeStorage()
62      {
63          return new float[10];
64      }
65  
66      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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       * Convert float to typed value.
90       * @param value float; float value
91       * @return typed value
92       */
93      protected abstract T convertValue(float value);
94  
95      /** {@inheritDoc} */
96      @Override
97      public O convert(final float[] storage, final int size)
98      {
99          try
100         {
101             // cut array to size and delegate
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      * Convert float array to typed array.
112      * @param storage float[]; float array storage
113      * @return typed array
114      * @throws ValueRuntimeException when float array cannot be converted
115      */
116     protected abstract O convert(float[] storage) throws ValueRuntimeException;
117 
118 }