View Javadoc
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-2024 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      @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 }