AbstractHistoricalList.java

  1. package org.opentrafficsim.core.perception.collections;

  2. import java.util.Collection;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.ListIterator;

  6. import org.opentrafficsim.core.perception.HistoryManager;

  7. /**
  8.  * List-valued historical state. The current list is always maintained, and past states of the list are obtained by applying the
  9.  * events between now and the requested time in reverse.<br>
  10.  * <br>
  11.  * The {@code Iterator} returned by this class does not support the {@code remove()}, {@code add()} and {@code set()} methods.
  12.  * Any returned sublist is unmodifiable.
  13.  * <p>
  14.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  16.  * <p>
  17.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 2 jan. 2018 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  21.  * @param <E> element type
  22.  * @param <L> list type
  23.  */
  24. public abstract class AbstractHistoricalList<E, L extends List<E>> extends AbstractHistoricalCollection<E, L>
  25.         implements HistoricalList<E>
  26. {

  27.     /**
  28.      * Constructor.
  29.      * @param historyManager HistoryManager; history manager
  30.      * @param list L; initial list
  31.      */
  32.     protected AbstractHistoricalList(final HistoryManager historyManager, final L list)
  33.     {
  34.         super(historyManager, list);
  35.     }

  36.     // Altering List methods

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public synchronized void add(final int index, final E value)
  40.     {
  41.         addEvent(new AddEvent<>(now().si, value, index));
  42.         getCollection().add(index, value);
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public synchronized boolean add(final E value)
  47.     {
  48.         addEvent(new AddEvent<>(now().si, value, getCollection().size()));
  49.         return getCollection().add(value);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public synchronized E remove(final int index)
  54.     {
  55.         addEvent(new RemoveEvent<>(now().si, getCollection().get(index), index));
  56.         return getCollection().remove(index);
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     @SuppressWarnings("unchecked")
  61.     public synchronized boolean remove(final Object value)
  62.     {
  63.         int index = getCollection().indexOf(value);
  64.         if (index >= 0)
  65.         {
  66.             addEvent(new RemoveEvent<>(now().si, (E) value, index)); // contains, so safe cast
  67.             getCollection().remove(index);
  68.             return true;
  69.         }
  70.         return false;
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public synchronized E set(final int index, final E value)
  75.     {
  76.         E previousValue = getCollection().get(index);
  77.         if (!getCollection().get(index).equals(value))
  78.         {
  79.             remove(index);
  80.             add(index, value);
  81.         }
  82.         return previousValue;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public boolean addAll(final int index, final Collection<? extends E> c)
  87.     {
  88.         int ind = index;
  89.         for (E e : c)
  90.         {
  91.             add(ind, e);
  92.             ind++;
  93.         }
  94.         return !c.isEmpty();
  95.     }

  96.     // Non-altering List methods

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public E get(final int index)
  100.     {
  101.         return getCollection().get(index);
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public int indexOf(final Object o)
  106.     {
  107.         return getCollection().indexOf(o);
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public int lastIndexOf(final Object o)
  112.     {
  113.         return getCollection().lastIndexOf(o);
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     public ListIterator<E> listIterator()
  118.     {
  119.         return listIterator(0);
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     public ListIterator<E> listIterator(final int index)
  124.     {
  125.         return Collections.unmodifiableList(getCollection()).listIterator(index);
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public List<E> subList(final int fromIndex, final int toIndex)
  130.     {
  131.         return Collections.unmodifiableList(getCollection().subList(fromIndex, toIndex));
  132.     }

  133.     // Events

  134.     /**
  135.      * Abstract super class for events that add or remove a value from the list.
  136.      * <p>
  137.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  138.      * <br>
  139.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  140.      * <p>
  141.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 jan. 2018 <br>
  142.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  143.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  144.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  145.      * @param <E> element type
  146.      * @param <L> list type
  147.      */
  148.     public abstract static class EventList<E, L extends List<E>> extends EventCollection<E, L>
  149.     {

  150.         /** Index of the value. */
  151.         private final int index;

  152.         /**
  153.          * Constructor.
  154.          * @param time double; time of event
  155.          * @param value E; value of event
  156.          * @param index int; index
  157.          */
  158.         public EventList(final double time, final E value, final int index)
  159.         {
  160.             super(time, value);
  161.             this.index = index;
  162.         }

  163.         /**
  164.          * Returns the index.
  165.          * @return index
  166.          */
  167.         final int getIndex()
  168.         {
  169.             return this.index;
  170.         }

  171.     }

  172.     /**
  173.      * Class for events that add a value to the list.
  174.      * <p>
  175.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  176.      * <br>
  177.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  178.      * <p>
  179.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 jan. 2018 <br>
  180.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  181.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  182.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  183.      * @param <E> element type
  184.      * @param <L> list type
  185.      */
  186.     public static class AddEvent<E, L extends List<E>> extends EventList<E, L>
  187.     {

  188.         /**
  189.          * Constructor.
  190.          * @param time double; time of event
  191.          * @param value E; value of event
  192.          * @param index int; index
  193.          */
  194.         public AddEvent(final double time, final E value, final int index)
  195.         {
  196.             super(time, value, index);
  197.         }

  198.         /** {@inheritDoc} */
  199.         @Override
  200.         public void restore(final L list)
  201.         {
  202.             list.remove(getIndex());
  203.         }

  204.         /** {@inheritDoc} */
  205.         @Override
  206.         public String toString()
  207.         {
  208.             return "AddEvent [time=" + getTime() + ", value=" + getValue() + ", index=" + getIndex() + "]";
  209.         }

  210.     }

  211.     /**
  212.      * Class for events that remove a value from the list.
  213.      * <p>
  214.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  215.      * <br>
  216.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  217.      * <p>
  218.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 jan. 2018 <br>
  219.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  220.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  221.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  222.      * @param <E> element type
  223.      * @param <L> list type
  224.      */
  225.     public static class RemoveEvent<E, L extends List<E>> extends EventList<E, L>
  226.     {

  227.         /**
  228.          * Constructor.
  229.          * @param time double; time of event
  230.          * @param value E; value of event
  231.          * @param index int; index the value is at
  232.          */
  233.         public RemoveEvent(final double time, final E value, final int index)
  234.         {
  235.             super(time, value, index);
  236.         }

  237.         /** {@inheritDoc} */
  238.         @Override
  239.         public void restore(final L list)
  240.         {
  241.             list.add(getIndex(), getValue());
  242.         }

  243.         /** {@inheritDoc} */
  244.         @Override
  245.         public String toString()
  246.         {
  247.             return "RemoveEvent [time=" + getTime() + ", value=" + getValue() + ", index=" + getIndex() + "]";
  248.         }

  249.     }

  250. }