View Javadoc
1   package org.opentrafficsim.kpi.sampling.data;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
8   import org.opentrafficsim.kpi.sampling.SamplingException;
9   
10  /**
11   * Extended data type for anything that can be captured in a list. Typically, these are non-numeric objects.
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
15   * <p>
16   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 mrt. 2017 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   * @param <T> type of value
21   * @param <G> gtu data type
22   */
23  public abstract class ExtendedDataTypeList<T, G extends GtuDataInterface> extends ExtendedDataType<T, List<T>, List<T>, G>
24  {
25  
26      /**
27       * Constructor setting the id.
28       * @param id String; id
29       */
30      public ExtendedDataTypeList(final String id)
31      {
32          super(id);
33      }
34  
35      /** {@inheritDoc} */
36      @Override
37      public T getOutputValue(final List<T> output, final int i) throws SamplingException
38      {
39          Throw.when(i < 0 || i >= output.size(), SamplingException.class, "Index %d out of range.", i);
40          return output.get(i);
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public T getStorageValue(final List<T> output, final int i) throws SamplingException
46      {
47          // same format for lists
48          return getOutputValue(output, i);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public List<T> setValue(final List<T> storage, final int index, final T value)
54      {
55          if (index == storage.size())
56          {
57              storage.add(value);
58          }
59          else
60          {
61              storage.set(index, value);
62          }
63          return storage;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public List<T> initializeStorage()
69      {
70          return new ArrayList<>();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public List<T> convert(final List<T> storage, final int size)
76      {
77          return storage;
78      }
79  
80  }