HistoricalList.java

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

  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Objects;
  6. import java.util.function.UnaryOperator;

  7. import org.djunits.value.vdouble.scalar.Time;

  8. /**
  9.  * Interface for historical lists.
  10.  * <p>
  11.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * </p>
  14.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  16.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  17.  * @param <E> element type
  18.  */
  19. public interface HistoricalList<E> extends HistoricalCollection<E>, List<E>
  20. {

  21.     /**
  22.      * Returns the current list.
  23.      * @return current list
  24.      */
  25.     @Override
  26.     List<E> get();

  27.     /**
  28.      * Returns a past list.
  29.      * @param time time to obtain the list at
  30.      * @return past list
  31.      */
  32.     @Override
  33.     List<E> get(Time time);

  34.     @Override
  35.     default void replaceAll(final UnaryOperator<E> operator)
  36.     {
  37.         // super uses ListIterator.set(E), which is not supported
  38.         Objects.requireNonNull(operator);
  39.         for (int i = 0; i < size(); i++)
  40.         {
  41.             set(i, operator.apply(get(i)));
  42.         }
  43.     }

  44.     @SuppressWarnings({"rawtypes", "unchecked"})
  45.     @Override
  46.     default void sort(final Comparator<? super E> c)
  47.     {
  48.         // super uses ListIterator.set(E), which is not supported
  49.         Object[] a = this.toArray();
  50.         Arrays.sort(a, (Comparator) c);
  51.         for (int i = 0; i < a.length; i++)
  52.         {
  53.             set(i, (E) a[i]);
  54.         }
  55.     }

  56. }