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     */
017    package org.apache.commons.scxml;
018    
019    import 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     */
029    public 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;
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         * The event name.
090         *
091         */
092        private String name;
093    
094        /**
095         * The event type.
096         *
097         */
098        private int type;
099    
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