View Javadoc
1   package org.djutils.event;
2   
3   import java.io.Serializable;
4   
5   /**
6    * The Event class forms the reference implementation for the EventInterface. Because events are often sent over the network,
7    * the interface demands that source of the event and its content are serializable. It is the responsibility of the programmer,
8    * though, that the <b>fields</b> of the sourceId and content are serializable as well.
9    * <p>
10   * Copyright (c) 2002-2020 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   */
20  public class Event implements EventInterface
21  {
22      /** The default serial version UID for serializable classes. */
23      private static final long serialVersionUID = 20140826L;
24  
25      /** The type of the event. */
26      private final EventType type;
27  
28      /** The content of the event. */
29      private final Serializable content;
30  
31      /** The source id of an event. */
32      private final Serializable sourceId;
33  
34      /**
35       * Construct a new Event.
36       * @param type EventType; the name of the Event.
37       * @param sourceId Serializable; the source id of the sender
38       * @param content Serializable; the content of the event
39       */
40      public Event(final EventType type, final Serializable sourceId, final Serializable content)
41      {
42          this.type = type;
43          this.sourceId = sourceId;
44          this.content = content;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public final Serializable getSourceId()
50      {
51          return this.sourceId;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public final Serializable getContent()
57      {
58          return this.content;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final EventType getType()
64      {
65          return this.type;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public String toString()
71      {
72          return "[" + this.getClass().getName() + ";" + this.getType() + ";" + this.getSourceId() + ";" + this.getContent()
73                  + "]";
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public int hashCode()
79      {
80          final int prime = 31;
81          int result = 1;
82          result = prime * result + ((this.content == null) ? 0 : this.content.hashCode());
83          result = prime * result + ((this.sourceId == null) ? 0 : this.sourceId.hashCode());
84          result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
85          return result;
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      @SuppressWarnings("checkstyle:needbraces")
91      public boolean equals(final Object obj)
92      {
93          if (this == obj)
94              return true;
95          if (obj == null)
96              return false;
97          if (getClass() != obj.getClass())
98              return false;
99          Event="../../../org/djutils/event/Event.html#Event">Event other = (Event) obj;
100         if (this.content == null)
101         {
102             if (other.content != null)
103                 return false;
104         }
105         else if (!this.content.equals(other.content))
106             return false;
107         if (this.sourceId == null)
108         {
109             if (other.sourceId != null)
110                 return false;
111         }
112         else if (!this.sourceId.equals(other.sourceId))
113             return false;
114         if (this.type == null)
115         {
116             if (other.type != null)
117                 return false;
118         }
119         else if (!this.type.equals(other.type))
120             return false;
121         return true;
122     }
123 
124 }