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