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