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