View Javadoc
1   package org.djutils.event;
2   
3   import org.djutils.exceptions.Throw;
4   
5   /**
6    * The TimedEvent is the reference implementation for a timed event.
7    * <p>
8    * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
10   * distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
12   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
13   * https://simulation.tudelft.nl/dsol/manual</a>.
14   * </p>
15   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @param <T> the Comparable type that represents time
18   */
19  public class TimedEvent<T extends Comparable<T>> extends Event implements Comparable<TimedEvent<T>>
20  {
21      /** Time stamp of this TimedEvent. */
22      private final T timeStamp;
23  
24      /**
25       * Construct a new timed event, where compliance with the metadata is verified.
26       * @param type the eventType of the event.
27       * @param content the content of the event.
28       * @param timeStamp the timeStamp.
29       */
30      public TimedEvent(final EventType type, final Object content, final T timeStamp)
31      {
32          this(type, content, timeStamp, true);
33      }
34  
35      /**
36       * Construct a new timed event, with a choice to verify compliance with metadata.
37       * @param type the eventType of the event.
38       * @param content the content of the event.
39       * @param timeStamp the timeStamp.
40       * @param verifyMetaData whether to verify the compliance with metadata or not
41       */
42      public TimedEvent(final EventType type, final Object content, final T timeStamp, final boolean verifyMetaData)
43      {
44          super(type, content, verifyMetaData);
45          Throw.whenNull(timeStamp, "timeStamp cannot be null");
46          this.timeStamp = timeStamp;
47      }
48  
49      /**
50       * Returns the timeStamp of this event.
51       * @return the time stamp
52       */
53      public T getTimeStamp()
54      {
55          return this.timeStamp;
56      }
57  
58      @Override
59      public int hashCode()
60      {
61          final int prime = 31;
62          int result = super.hashCode();
63          result = prime * result + ((this.timeStamp == null) ? 0 : this.timeStamp.hashCode());
64          return result;
65      }
66  
67      @Override
68      @SuppressWarnings("checkstyle:needbraces")
69      public boolean equals(final Object obj)
70      {
71          if (this == obj)
72              return true;
73          if (!super.equals(obj))
74              return false;
75          TimedEvent<?> other = (TimedEvent<?>) obj;
76          if (this.timeStamp == null)
77          {
78              if (other.timeStamp != null)
79                  return false;
80          }
81          else if (!this.timeStamp.equals(other.timeStamp))
82              return false;
83          return true;
84      }
85  
86      @Override
87      public int compareTo(final TimedEvent<T> o)
88      {
89          return this.timeStamp.compareTo(o.getTimeStamp());
90      }
91  
92      @Override
93      public String toString()
94      {
95          return "[" + this.getClass().getName() + ";" + this.getType() + ";" + this.getContent() + ";" + this.getTimeStamp()
96                  + "]";
97      }
98  
99  }