1 package org.opentrafficsim.core.perception;
2
3 import org.djunits.value.vdouble.scalar.Time;
4 import org.djutils.exceptions.Throw;
5 import org.opentrafficsim.core.perception.AbstractHistorical.EventValue;
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class HistoricalValue<T> extends AbstractHistorical<T, EventValue<T>> implements Historical<T>
20 {
21
22
23 private T lastValue;
24
25
26 private double lastTime;
27
28
29
30
31
32 public HistoricalValue(final HistoryManager historyManager)
33 {
34 super(historyManager);
35 }
36
37
38
39
40
41
42 public HistoricalValue(final HistoryManager historyManager, final T initialValue)
43 {
44 super(historyManager);
45 set(initialValue);
46 }
47
48
49 @Override
50 public final void set(final T value)
51 {
52 this.lastValue = value;
53 this.lastTime = now().si;
54 EventValue<T> event = getLastEvent();
55 if (event != null && event.getTime() == this.lastTime)
56 {
57 removeEvent(event);
58 }
59 addEvent(new EventValue<>(this.lastTime, value));
60 }
61
62
63 @Override
64 public final T get()
65 {
66 return this.lastValue;
67 }
68
69
70 @Override
71 public final T get(final Time time)
72 {
73 Throw.whenNull(time, "Time may not be null.");
74 if (time.si >= this.lastTime)
75 {
76 return this.lastValue;
77 }
78 EventValue<T> event = getEvent(time);
79 return event == null ? null : event.getValue();
80 }
81
82
83 @Override
84 public String toString()
85 {
86 return "HistoricalValue [current=" + get() + "]";
87 }
88
89 }