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      /** The wildcard event */
33      public static final String WILDCARD_EVENT_ID = "*";
34  
35      private final Object id;
36  
37      private final StateContext context;
38  
39      private final Object[] arguments;
40  
41      /**
42       * Creates a new {@link Event} with the specified id and no arguments.
43       * 
44       * @param id the event id.
45       * @param context the {@link StateContext} the event was triggered for.
46       */
47      public Event(Object id, StateContext context) {
48          this(id, context, new Object[0]);
49      }
50  
51      /**
52       * Creates a new {@link Event} with the specified id and arguments.
53       * 
54       * @param id the event id.
55       * @param context the {@link StateContext} the event was triggered for.
56       * @param arguments the event arguments.
57       */
58      public Event(Object id, StateContext context, Object[] arguments) {
59          if (id == null) {
60              throw new IllegalArgumentException("id");
61          }
62          
63          if (context == null) {
64              throw new IllegalArgumentException("context");
65          }
66          
67          if (arguments == null) {
68              throw new IllegalArgumentException("arguments");
69          }
70          
71          this.id = id;
72          this.context = context;
73          this.arguments = arguments;
74      }
75  
76      /**
77       * @return the {@link StateContext} this {@link Event} was triggered for.
78       */
79      public StateContext getContext() {
80          return context;
81      }
82  
83      /**
84       * @return the id of this {@link Event}.
85       */
86      public Object getId() {
87          return id;
88      }
89  
90      /**
91       * @return the arguments of this {@link Event}. @return an empty array if this {@link Event} has 
92       *         no arguments.
93       */
94      public Object[] getArguments() {
95          return arguments;
96      }
97  
98      @Override
99      public String toString() {
100         StringBuilder sb = new StringBuilder();
101         
102         sb.append("Event[");
103         sb.append("id=").append(id);
104         sb.append(",context=").append(context);
105         sb.append(",arguments=");
106         
107         if (arguments != null) {
108             sb.append('{');
109             boolean isFirst = true;
110             
111             for (Object argument:arguments) {
112                 if (isFirst) {
113                     isFirst = false;
114                 } else {
115                     sb.append(',');
116                 }
117                 
118                 sb.append(argument);
119             }
120             
121             sb.append('}');
122         } else {
123             sb.append("null");
124         }
125 
126         sb.append("]");
127         
128         return sb.toString();
129     }
130 }