View Javadoc
1   package org.opentrafficsim.core.perception;
2   
3   import java.util.Collections;
4   import java.util.Set;
5   import java.util.WeakHashMap;
6   
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Time;
9   
10  /**
11   * History manager with automatic garbage collection by the java garbage collector using weak references to the
12   * {@code Historical}s.
13   * <p>
14   * Copyright (c) 2013-2019 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 18 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   */
22  public abstract class HistoryManager
23  {
24  
25      /** Set of all {@code Historical}s. */
26      // There's no WeakSet, but this is effectively the same. Iterating over this is safe, only alive objects are returned.
27      private final Set<HistoricalElement> historicals = Collections.newSetFromMap(new WeakHashMap<HistoricalElement, Boolean>());
28  
29      /**
30       * Registers a historical.
31       * @param historical HistoricalElement; historical to register.
32       */
33      public void registerHistorical(final HistoricalElement historical)
34      {
35          if (historical != null)
36          {
37              this.historicals.add(historical);
38          }
39      }
40  
41      /**
42       * Returns the historicals.
43       * @return the historicals
44       */
45      protected final Set<HistoricalElement> getHistoricals()
46      {
47          return this.historicals;
48      }
49  
50      /**
51       * Returns the current simulation time. This is used by historicals to time-stamp state changes.
52       * @return Time; current simulation time.
53       */
54      abstract Time now();
55  
56      /**
57       * Historical view for the history manager.
58       * <p>
59       * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
60       * <br>
61       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
62       * <p>
63       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 feb. 2018 <br>
64       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
65       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
66       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
67       */
68      public interface HistoricalElement
69      {
70          /**
71           * Removes events that are no longer needed to guarantee the history time. This is invoked by the history manager.
72           * @param history Duration; history time to keep
73           */
74          void cleanUpHistory(Duration history);
75      }
76  
77      /**
78       * Method that clears the entire memory at simulation end.
79       */
80      protected final void endOfSimulation()
81      {
82          for (HistoricalElement historical : this.historicals)
83          {
84              historical.cleanUpHistory(Duration.ZERO);
85          }
86          this.historicals.clear();
87      }
88  
89  }