View Javadoc

1   /*
2    * @(#) EventType.java Sep 1, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.event;
11  
12  import java.io.Serializable;
13  
14  /***
15   * The EventType is a masker used for the subscription to asynchronous events.
16   * Eventtypes are used by EventProducers to show which events they potentially
17   * fire. EventTypes should be defined as static final fields.
18   * <p>
19   * The EventType class is made final for a number of reasons. The most important
20   * is to assure that the <code>equals</code> and <code>hashcode</code>
21   * methods are not overloaded.
22   * <p>
23   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
24   * University of Technology </a>, the Netherlands. <br>
25   * See for project information <a
26   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
27   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
28   * License (GPL) </a>, no warranty <br>
29   * 
30   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
31   *         Jacobs </a>
32   * @version 1.08 2004-03-18
33   * @since 1.0
34   */
35  public final class EventType implements Serializable
36  {
37  	/*** value is the flag number */
38  	private int value;
39  
40  	/*** name refers to the name of the eventType */
41  	private String name;
42  
43  	/***
44  	 * constructs a new EventType.
45  	 * 
46  	 * @param name the name of this eventType. Two values are not appreciated :
47  	 *        <code>null</code> and <code>""</code>.
48  	 */
49  	public EventType(final String name)
50  	{
51  		super();
52  		if (name == null || name.equals(""))
53  		{
54  			throw new IllegalArgumentException("name == null || name == \"\"");
55  		}
56  		this.value = name.hashCode();
57  		this.name = name;
58  	}
59  
60  	/***
61  	 * @see java.lang.Object#equals(java.lang.Object)
62  	 */
63  	public boolean equals(final Object arg0)
64  	{
65  		if (!(arg0 instanceof EventType))
66  		{
67  			return false;
68  		}
69  		return this.value == ((EventType) arg0).value;
70  	}
71  
72  	/***
73  	 * Returns a hash code for the name of the this eventType. The hash code for
74  	 * an <code>EventType</code> object is computed as <blockquote>
75  	 * 
76  	 * <pre>
77  	 * 
78  	 *   s[0]*31&circ;(n-1) + s[1]*31&circ;(n-2) + ... + s[n-1]
79  	 *  
80  	 * </pre>
81  	 * 
82  	 * </blockquote> using <code>int</code> arithmetic, where
83  	 * <code>s[i]</code> is the <i>i </i>th character of the name of the
84  	 * eventType, <code>n</code> is the length of the name, and <code>^</code>
85  	 * indicates exponentiation. This algoritm assures JVM, host, time
86  	 * independency.
87  	 * 
88  	 * @return a hash code value for this object.
89  	 */
90  	public int hashCode()
91  	{
92  		return this.value;
93  	}
94  
95  	/***
96  	 * @see java.lang.Object#toString()
97  	 */
98  	public String toString()
99  	{
100 		return this.name;
101 	}
102 }