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.GtuDataInterface;
7   import org.opentrafficsim.kpi.sampling.SamplingException;
8   
9   /**
10   * Class for unitless values.
11   * <p>
12   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 23 mei 2018 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19   * @param <G> gtu data type
20   */
21  public abstract class ExtendedDataTypeNumber<G extends GtuDataInterface> extends ExtendedDataType<Float, float[], float[], G>
22  {
23  
24      /**
25       * Constructor.
26       * @param id String; id
27       */
28      public ExtendedDataTypeNumber(final String id)
29      {
30          super(id);
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 String formatValue(final String format, final Float value)
83      {
84          return String.format(format, value);
85      }
86  
87  }