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