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.ValueException;
7   import org.djunits.value.vfloat.scalar.AbstractFloatScalar;
8   import org.djunits.value.vfloat.vector.AbstractFloatVector;
9   import org.djutils.exceptions.Throw;
10  import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
11  import org.opentrafficsim.kpi.sampling.SamplingException;
12  
13  /**
14   * Class to facilitate JUNITS types in extended data.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 mrt. 2017 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   * @param <U> unit
24   * @param <T> type in vector
25   * @param <O> vector type
26   * @param <G> gtu data type
27   */
28  public abstract class ExtendedDataTypeFloat<U extends Unit<U>, T extends AbstractFloatScalar<U, T>,
29          O extends AbstractFloatVector<U, O>, G extends GtuDataInterface> extends ExtendedDataType<T, O, float[], G>
30  {
31      /**
32       * Constructor setting the id.
33       * @param id String; id
34       */
35      public ExtendedDataTypeFloat(final String id)
36      {
37          super(id);
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public final float[] setValue(final float[] storage, final int i, final T value)
43      {
44          float[] out;
45          if (i == storage.length)
46          {
47              int cap = (i - 1) + ((i - 1) >> 1);
48              out = Arrays.copyOf(storage, cap);
49          }
50          else
51          {
52              out = storage;
53          }
54          out[i] = value.si;
55          return out;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final float[] initializeStorage()
61      {
62          return new float[10];
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final String formatValue(final String format, final T value)
68      {
69          return String.format(format, value.getSI());
70      }
71  
72      /** {@inheritDoc} */
73      @SuppressWarnings("unchecked")
74      @Override
75      public final T getOutputValue(final O output, final int i) throws SamplingException
76      {
77          try
78          {
79              return (T) output.get(i);
80          }
81          catch (ValueException exception)
82          {
83              throw new SamplingException("Index out of range.", exception);
84          }
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public T getStorageValue(final float[] storage, final int i) throws SamplingException
90      {
91          Throw.when(i < 0 || i >= storage.length, SamplingException.class, "Index %d out of range.", i);
92          return convertValue(storage[i]);
93      }
94  
95      /**
96       * Convert float to typed value.
97       * @param value float; float value
98       * @return typed value
99       */
100     protected abstract T convertValue(final float value);
101 
102     /** {@inheritDoc} */
103     @Override
104     public O convert(final float[] storage, final int size)
105     {
106         try
107         {
108             // cut array to size and delegate
109             return convert(Arrays.copyOf(storage, size));
110         }
111         catch (ValueException exception)
112         {
113             throw new RuntimeException("Could not create typed vector from float array.", exception);
114         }
115     }
116 
117     /**
118      * Convert float array to typed array.
119      * @param storage float[]; float array storage
120      * @return typed array
121      * @throws ValueException when float array cannot be converted
122      */
123     protected abstract O convert(final float[] storage) throws ValueException;
124 
125 }