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.transition;
21  
22  import org.apache.mina.statemachine.State;
23  import org.apache.mina.statemachine.StateMachine;
24  import org.apache.mina.statemachine.event.Event;
25  
26  /**
27   * Abstract {@link Transition} implementation. Takes care of matching the
28   * current {@link Event}'s id against the id of the {@link Event} this 
29   * {@link Transition} handles. To handle any {@link Event} the id should be set
30   * to {@link Event#WILDCARD_EVENT_ID}.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public abstract class AbstractTransition implements Transition {
35      private final Object eventId;
36  
37      private final State nextState;
38  
39      /**
40       * Creates a new instance which will loopback to the same {@link State} 
41       * for the specified {@link Event} id.
42       * 
43       * @param eventId the {@link Event} id.
44       */
45      public AbstractTransition(Object eventId) {
46          this(eventId, null);
47      }
48  
49      /**
50       * Creates a new instance with the specified {@link State} as next state 
51       * and for the specified {@link Event} id.
52       * 
53       * @param eventId the {@link Event} id.
54       * @param nextState the next {@link State}.
55       */
56      public AbstractTransition(Object eventId, State nextState) {
57          this.eventId = eventId;
58          this.nextState = nextState;
59      }
60  
61      public State getNextState() {
62          return nextState;
63      }
64  
65      public boolean execute(Event event) {
66          if (!eventId.equals(Event.WILDCARD_EVENT_ID) && !eventId.equals(event.getId())) {
67              return false;
68          }
69  
70          return doExecute(event);
71      }
72  
73      /**
74       * Executes this {@link Transition}. This method doesn't have to check
75       * if the {@link Event}'s id matches because {@link #execute(Event)} has
76       * already made sure that that is the case.
77       * 
78       * @param event the current {@link Event}.
79       * @return <tt>true</tt> if the {@link Transition} has been executed 
80       *         successfully and the {@link StateMachine} should move to the 
81       *         next {@link State}. <tt>false</tt> otherwise.
82       */
83      protected abstract boolean doExecute(Event event);
84  
85      public boolean equals(Object o) {
86          if (o == this) {
87              return true;
88          }
89  
90          if (!(o instanceof AbstractTransition)) {
91              return false;
92          }
93          
94          AbstractTransition that = (AbstractTransition) o;
95          
96          if (eventId != null) {
97              if (!eventId.equals( that.eventId )) {
98                  return false;
99              }
100         } else {
101             if (that.eventId != null) {
102                 return false;
103             }
104         }
105         
106         
107         if (nextState != null) {
108             return nextState.equals( that.nextState );
109         } else {
110             return that.nextState == null;
111         }
112     }
113 
114     public int hashCode() {
115         int h = 17;
116         
117         if ( eventId != null) {
118             h = h*37 + eventId.hashCode();
119         }
120         
121         if (nextState != null) {
122             h = h*17 + nextState.hashCode();
123         }
124         
125         return h;
126     }
127 
128     public String toString() {
129         StringBuilder sb = new StringBuilder();
130         
131         sb.append("eventId=").append(eventId);
132         sb.append(",nextState=").append(nextState);
133         return sb.toString();
134     }
135 }