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