001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2;
018
019import java.io.Serializable;
020
021/**
022 * A class representing an event. Specific event types have been
023 * defined in reference to SCXML.
024 *
025 * <b>NOTE:</b> Instances are {@link Serializable} as long as the associated
026 * payload, if any, is {@link Serializable}.
027 *
028 */
029public class TriggerEvent implements Serializable {
030
031    /** Serial version UID. */
032    private static final long serialVersionUID = 1L;
033
034    /**
035     * Constructor.
036     *
037     * @param name The event name
038     * @param type The event type
039     * @param payload The event payload, must be {@link Serializable}
040     */
041    public TriggerEvent(final String name, final int type,
042            final Object payload) {
043        super();
044        this.name = name != null ? name.trim() : "";
045        this.type = type;
046        this.payload = payload;
047    }
048
049    /**
050     * Constructor.
051     *
052     * @param name The event name
053     * @param type The event type
054     */
055    public TriggerEvent(final String name, final int type) {
056        this(name, type, null);
057    }
058
059    /**
060     * <code>CALL_EVENT</code>.
061     */
062    public static final int CALL_EVENT = 1;
063
064    /**
065     * <code>CHANGE_EVENT</code>.
066     *
067     */
068    public static final int CHANGE_EVENT = 2;
069
070    /**
071     * <code>SIGNAL_EVENT</code>.
072     *
073     */
074    public static final int SIGNAL_EVENT = 3;
075
076    /**
077     * <code>TIME_EVENT</code>.
078     *
079     */
080    public static final int TIME_EVENT = 4;
081
082    /**
083     * <code>ERROR_EVENT</code>.
084     *
085     */
086    public static final int ERROR_EVENT = 5;
087
088    /**
089     * <code>CANCEL_EVENT</code>.
090     *
091     */
092    public static final int CANCEL_EVENT = 6;
093
094    /**
095     * The predefined SCXML 'error.execution' Event name
096     * <p>
097     * Indicates that an error internal to the execution of the document has occurred, such as one arising from
098     * expression evaluation.
099     * </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