View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml;
18  
19  import java.io.Serializable;
20  
21  /**
22   * A class representing an event. Specific event types have been
23   * defined in reference to SCXML.
24   *
25   * <b>NOTE:</b> Instances are {@link Serializable} as long as the associated
26   * payload, if any, is {@link Serializable}.
27   *
28   */
29  public class TriggerEvent implements Serializable {
30  
31      /** Serial version UID. */
32      private static final long serialVersionUID = 1L;
33  
34      /**
35       * Constructor.
36       *
37       * @param name The event name
38       * @param type The event type
39       * @param payload The event payload, must be {@link Serializable}
40       */
41      public TriggerEvent(final String name, final int type,
42              final Object payload) {
43          super();
44          this.name = name;
45          this.type = type;
46          this.payload = payload;
47      }
48  
49      /**
50       * Constructor.
51       *
52       * @param name The event name
53       * @param type The event type
54       */
55      public TriggerEvent(final String name, final int type) {
56          this(name, type, null);
57      }
58  
59      /**
60       * <code>CALL_EVENT</code>.
61       */
62      public static final int CALL_EVENT = 1;
63  
64      /**
65       * <code>CHANGE_EVENT</code>.
66       *
67       */
68      public static final int CHANGE_EVENT = 2;
69  
70      /**
71       * <code>SIGNAL_EVENT</code>.
72       *
73       */
74      public static final int SIGNAL_EVENT = 3;
75  
76      /**
77       * <code>TIME_EVENT</code>.
78       *
79       */
80      public static final int TIME_EVENT = 4;
81  
82      /**
83       * <code>ERROR_EVENT</code>.
84       *
85       */
86      public static final int ERROR_EVENT = 5;
87  
88      /**
89       * The event name.
90       *
91       */
92      private String name;
93  
94      /**
95       * The event type.
96       *
97       */
98      private int type;
99  
100     /**
101      * The event payload.
102      *
103      */
104     private Object payload;
105 
106     /**
107      * @return Returns the name.
108      */
109     public String getName() {
110         return name;
111     }
112 
113     /**
114      * @return Returns the payload.
115      */
116     public Object getPayload() {
117         return payload;
118     }
119 
120     /**
121      * @return Returns the type.
122      */
123     public int getType() {
124         return type;
125     }
126 
127     /**
128      * Define an equals operator for TriggerEvent.
129      *
130      * @see java.lang.Object#equals(java.lang.Object)
131      */
132     public boolean equals(final Object obj) {
133         if (obj instanceof TriggerEvent) {
134             TriggerEvent te2 = (TriggerEvent) obj;
135             if (type == te2.type && name.equals(te2.name)
136                 && ((payload == null && te2.payload == null)
137                      || (payload != null && payload.equals(te2.payload)))) {
138                 return true;
139             }
140         }
141         return false;
142     }
143 
144     /**
145      * Returns a string representation of this TriggerEvent object.
146      *
147      * @see java.lang.Object#toString()
148      */
149     public String toString() {
150         StringBuffer buf = new StringBuffer("TriggerEvent{name=");
151         buf.append(name).append(",type=").append(type);
152         if (payload != null) {
153             buf.append(",payload=").append(payload.toString());
154         }
155         buf.append("}");
156         return String.valueOf(buf);
157     }
158 
159     /**
160      * Returns the hash code for this TriggerEvent object.
161      *
162      * @see java.lang.Object#hashCode()
163      */
164     public int hashCode() {
165         return String.valueOf(this).hashCode();
166     }
167 
168 }
169