View Javadoc
1   package org.opentrafficsim.kpi.sampling.data;
2   
3   import java.util.Arrays;
4   
5   import org.djutils.exceptions.Throw;
6   import org.opentrafficsim.kpi.interfaces.GtuData;
7   import org.opentrafficsim.kpi.sampling.SamplingException;
8   
9   /**
10   * Class for unitless values.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   * @param <G> gtu data type
19   */
20  public abstract class ExtendedDataNumber<G extends GtuData> extends ExtendedDataType<Float, float[], float[], G>
21  {
22  
23      /**
24       * Constructor.
25       * @param id String; id
26       * @param description String; description
27       */
28      public ExtendedDataNumber(final String id, final String description)
29      {
30          super(id, description, Float.class);
31      }
32  
33      /** {@inheritDoc} */
34      @Override
35      public float[] setValue(final float[] storage, final int index, final Float value)
36      {
37          float[] out;
38          if (index == storage.length)
39          {
40              int cap = (index - 1) + ((index - 1) >> 1);
41              out = Arrays.copyOf(storage, cap);
42          }
43          else
44          {
45              out = storage;
46          }
47          out[index] = value;
48          return out;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public Float getOutputValue(final float[] output, final int index) throws SamplingException
54      {
55          return output[index];
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Float getStorageValue(final float[] storage, final int index) throws SamplingException
61      {
62          Throw.when(index < 0 || index >= storage.length, SamplingException.class, "Index %d out of range.", index);
63          return storage[index];
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public float[] initializeStorage()
69      {
70          return new float[10];
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public float[] convert(final float[] storage, final int size)
76      {
77          return Arrays.copyOf(storage, size);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public Float parseValue(final String string)
83      {
84          return Float.valueOf(string);
85      }
86  
87  }