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
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class Event implements Serializable
25 {
26
27 private static final long serialVersionUID = 20140826L;
28
29
30 private final EventType type;
31
32
33 private final Serializable content;
34
35
36
37
38
39
40 public Event(final EventType type, final Serializable content)
41 {
42 this(type, content, true);
43 }
44
45
46
47
48
49
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
75
76
77 public final Serializable getContent()
78 {
79 return this.content;
80 }
81
82
83
84
85
86 public EventType getType()
87 {
88 return this.type;
89 }
90
91 @Override
92 public int hashCode()
93 {
94 return Objects.hash(this.content, this.type);
95 }
96
97 @Override
98 @SuppressWarnings("checkstyle:needbraces")
99 public boolean equals(final Object obj)
100 {
101 if (this == obj)
102 return true;
103 if (obj == null)
104 return false;
105 if (getClass() != obj.getClass())
106 return false;
107 Event other = (Event) obj;
108 return Objects.equals(this.content, other.content) && Objects.equals(this.type, other.type);
109 }
110
111 @Override
112 public String toString()
113 {
114 return "[" + this.getClass().getName() + ";" + this.getType() + ";" + this.getContent() + "]";
115 }
116 }