1 package org.djutils.event; 2 3 import java.io.Serializable; 4 5 /** 6 * The EventType is a masker used for the subscription to asynchronous events. Eventtypes are used by EventProducers to show 7 * which events they potentially fire. EventTypes should be defined as static final fields. In order to prevent name clashes for 8 * the EventType, the full name of the class from which the EventType was defined (usually in the <clinit>) is added to 9 * the equals() and hashCode() methods of the EventType. In that way, EventTypes that are the same will be unique, but 10 * EventTypes with just the same name but defined in different classes will be different. <br> 11 * <br> 12 * Note: the reason why this is important is because <b>remote events</b> that use EventTypes can have <i>multiple versions</i> 13 * of the same public static final EventType: one the is defined in the client, and one that is defined via the network. These 14 * will have <i>different addresses in memory</i> but they share the same class and name info, so equals() will yield true. 15 * <p> 16 * Copyright (c) 2002-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See 17 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is 18 * distributed under a three-clause BSD-style license, which can be found at 19 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was 20 * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank"> 21 * https://simulation.tudelft.nl/dsol/manual</a>. 22 * </p> 23 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a> 24 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a> 25 */ 26 public final class EventType implements Serializable 27 { 28 /** The default serial version UID for serializable classes. */ 29 private static final long serialVersionUID = 20140830L; 30 31 /** The name of the eventType. */ 32 private final String name; 33 34 /** 35 * The class name from which the event type construction was called; together with the event name this should lead to a 36 * unique hash, even when the same name is used in different classes. 37 */ 38 private final String definingClassName; 39 40 /** 41 * Construct a new EventType. 42 * @param name String; the name of this eventType. Two values are not appreciated: null and the empty string. 43 */ 44 public EventType(final String name) 45 { 46 if (name == null || name.equals("")) 47 { 48 throw new IllegalArgumentException("EventType name == null || EventType name == \"\""); 49 } 50 this.name = name; 51 StackTraceElement[] steArray = new Throwable().getStackTrace(); 52 this.definingClassName = steArray[1].getClassName(); 53 } 54 55 /** 56 * Return the event type name. 57 * @return String; the event type name 58 */ 59 public String getName() 60 { 61 return this.name; 62 } 63 64 /** {@inheritDoc} */ 65 @Override 66 public String toString() 67 { 68 return this.name; 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 public int hashCode() 74 { 75 final int prime = 31; 76 int result = 1; 77 result = prime * result + this.definingClassName.hashCode(); 78 result = prime * result + this.name.hashCode(); 79 return result; 80 } 81 82 /** {@inheritDoc} */ 83 @Override 84 @SuppressWarnings("checkstyle:needbraces") 85 public boolean equals(final Object obj) 86 { 87 if (this == obj) 88 return true; 89 if (obj == null) 90 return false; 91 if (getClass() != obj.getClass()) 92 return false; 93 EventType/../../org/djutils/event/EventType.html#EventType">EventType other = (EventType) obj; 94 if (!this.name.equals(other.name)) 95 return false; 96 if (!this.definingClassName.equals(other.definingClassName)) 97 return false; 98 return true; 99 } 100 101 }