001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.statemachine.event;
021
022import org.apache.mina.statemachine.context.StateContext;
023
024/**
025 * Represents an event which typically corresponds to a method call on a proxy.
026 * An event has an id and zero or more arguments typically corresponding to
027 * the method arguments. 
028 *
029 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
030 */
031public class Event {
032    public static final String WILDCARD_EVENT_ID = "*";
033
034    private final Object id;
035
036    private final StateContext context;
037
038    private final Object[] arguments;
039
040    /**
041     * Creates a new {@link Event} with the specified id and no arguments.
042     * 
043     * @param id the event id.
044     * @param context the {@link StateContext} the event was triggered for.
045     */
046    public Event(Object id, StateContext context) {
047        this(id, context, new Object[0]);
048    }
049
050    /**
051     * Creates a new {@link Event} with the specified id and arguments.
052     * 
053     * @param id the event id.
054     * @param context the {@link StateContext} the event was triggered for.
055     * @param arguments the event arguments.
056     */
057    public Event(Object id, StateContext context, Object[] arguments) {
058        if (id == null) {
059            throw new IllegalArgumentException("id");
060        }
061        if (context == null) {
062            throw new IllegalArgumentException("context");
063        }
064        if (arguments == null) {
065            throw new IllegalArgumentException("arguments");
066        }
067        this.id = id;
068        this.context = context;
069        this.arguments = arguments;
070    }
071
072    /**
073     * @return the {@link StateContext} this {@link Event} was triggered for.
074     */
075    public StateContext getContext() {
076        return context;
077    }
078
079    /**
080     * @return the id of this {@link Event}.
081     */
082    public Object getId() {
083        return id;
084    }
085
086    /**
087     * @return the arguments of this {@link Event}. @return an empty array if this {@link Event} has 
088     *         no arguments.
089     */
090    public Object[] getArguments() {
091        return arguments;
092    }
093
094    public String toString() {
095        StringBuilder sb = new StringBuilder();
096        
097        sb.append("Event[");
098        sb.append("id=").append(id);
099        sb.append(",context=").append(context);
100        sb.append(",arguments=");
101        
102        if (arguments != null) {
103            sb.append('{');
104            boolean isFirst = true;
105            
106            for (Object argument:arguments) {
107                if (isFirst) {
108                    isFirst = false;
109                } else {
110                    sb.append(',');
111                }
112                
113                sb.append(argument);
114            }
115            
116            sb.append('}');
117        } else {
118            sb.append("null");
119        }
120
121        sb.append("]");
122        
123        return sb.toString();
124    }
125}