View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.statemachine.event;
21  
22  import org.apache.mina.statemachine.context.StateContext;
23  
24  /**
25   * Represents an event which typically corresponds to a method call on a proxy.
26   * An event has an id and zero or more arguments typically corresponding to
27   * the method arguments. 
28   *
29   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
30   */
31  public class Event {
32      public static final String WILDCARD_EVENT_ID = "*";
33  
34      private final Object id;
35  
36      private final StateContext context;
37  
38      private final Object[] arguments;
39  
40      /**
41       * Creates a new {@link Event} with the specified id and no arguments.
42       * 
43       * @param id the event id.
44       * @param context the {@link StateContext} the event was triggered for.
45       */
46      public Event(Object id, StateContext context) {
47          this(id, context, new Object[0]);
48      }
49  
50      /**
51       * Creates a new {@link Event} with the specified id and arguments.
52       * 
53       * @param id the event id.
54       * @param context the {@link StateContext} the event was triggered for.
55       * @param arguments the event arguments.
56       */
57      public Event(Object id, StateContext context, Object[] arguments) {
58          if (id == null) {
59              throw new IllegalArgumentException("id");
60          }
61          if (context == null) {
62              throw new IllegalArgumentException("context");
63          }
64          if (arguments == null) {
65              throw new IllegalArgumentException("arguments");
66          }
67          this.id = id;
68          this.context = context;
69          this.arguments = arguments;
70      }
71  
72      /**
73       * @return the {@link StateContext} this {@link Event} was triggered for.
74       */
75      public StateContext getContext() {
76          return context;
77      }
78  
79      /**
80       * @return the id of this {@link Event}.
81       */
82      public Object getId() {
83          return id;
84      }
85  
86      /**
87       * @return the arguments of this {@link Event}. @return an empty array if this {@link Event} has 
88       *         no arguments.
89       */
90      public Object[] getArguments() {
91          return arguments;
92      }
93  
94      public String toString() {
95          StringBuilder sb = new StringBuilder();
96          
97          sb.append("Event[");
98          sb.append("id=").append(id);
99          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 }