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.scxml2;
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 != null ? name.trim() : "";
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       * <code>CANCEL_EVENT</code>.
90       *
91       */
92      public static final int CANCEL_EVENT = 6;
93  
94      /**
95       * The predefined SCXML 'error.execution' Event name
96       * <p>
97       * Indicates that an error internal to the execution of the document has occurred, such as one arising from
98       * expression evaluation.
99       * </p>
100      * @see: <a href="http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents">
101      *     http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents</a>
102      */
103     public static final String ERROR_EXECUTION = "error.execution";
104 
105     /**
106      * The predefined SCXML 'error.communication' Event name
107      * <p>
108      * Indicates that an error has occurred while trying to communicate with an external entity.
109      * </p>
110      * @see: <a href="http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents">
111      *     http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents</a>
112      */
113     public static final String ERROR_COMMUNICATION = "error.communication";
114 
115     /**
116      * The predefined SCXML 'error.platform' Event name
117      * <p>
118      * Indicates that a platform- or application-specific error has occurred.
119      * </p>
120      * @see: <a href="http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents">
121      *     http://www.w3.org/TR/2014/CR-scxml-20140313/#errorsAndEvents</a>
122      */
123     public static final String ERROR_PLATFORM = "error.platform";
124 
125     /**
126      * The event name.
127      *
128      */
129     private String name;
130 
131     /**
132      * The event type.
133      *
134      */
135     private int type;
136 
137     /**
138      * The event payload.
139      *
140      */
141     private Object payload;
142 
143     /**
144      * @return Returns the name.
145      */
146     public String getName() {
147         return name;
148     }
149 
150     /**
151      * @return Returns the payload.
152      */
153     public Object getPayload() {
154         return payload;
155     }
156 
157     /**
158      * @return Returns the type.
159      */
160     public int getType() {
161         return type;
162     }
163 
164     /**
165      * Define an equals operator for TriggerEvent.
166      *
167      * @see java.lang.Object#equals(java.lang.Object)
168      */
169     @Override
170     public boolean equals(final Object obj) {
171         if (obj instanceof TriggerEvent) {
172             TriggerEvent te2 = (TriggerEvent) obj;
173             if (type == te2.type && name.equals(te2.name)
174                 && ((payload == null && te2.payload == null)
175                      || (payload != null && payload.equals(te2.payload)))) {
176                 return true;
177             }
178         }
179         return false;
180     }
181 
182     /**
183      * Returns a string representation of this TriggerEvent object.
184      *
185      * @see java.lang.Object#toString()
186      */
187     @Override
188     public String toString() {
189         StringBuffer buf = new StringBuffer("TriggerEvent{name=");
190         buf.append(name).append(",type=").append(type);
191         if (payload != null) {
192             buf.append(",payload=").append(payload.toString());
193         }
194         buf.append("}");
195         return String.valueOf(buf);
196     }
197 
198     /**
199      * Returns the hash code for this TriggerEvent object.
200      *
201      * @see java.lang.Object#hashCode()
202      */
203     @Override
204     public int hashCode() {
205         return String.valueOf(this).hashCode();
206     }
207 
208 }
209