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.commons.lang.builder.EqualsBuilder;
23  import org.apache.commons.lang.builder.HashCodeBuilder;
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  import org.apache.mina.statemachine.State;
26  import org.apache.mina.statemachine.StateMachine;
27  import org.apache.mina.statemachine.event.Event;
28  
29  /**
30   * Abstract {@link Transition} implementation. Takes care of matching the
31   * current {@link Event}'s id against the id of the {@link Event} this 
32   * {@link Transition} handles. To handle any {@link Event} the id should be set
33   * to {@link Event#WILDCARD_EVENT_ID}.
34   *
35   * @author The Apache MINA Project (dev@mina.apache.org)
36   * @version $Rev: 586695 $, $Date: 2007-10-20 12:01:17 +0200 (sam, 20 oct 2007) $
37   */
38  public abstract class AbstractTransition implements Transition {
39      private final Object eventId;
40  
41      private final State nextState;
42  
43      /**
44       * Creates a new instance which will loopback to the same {@link State} 
45       * for the specified {@link Event} id.
46       * 
47       * @param eventId the {@link Event} id.
48       */
49      public AbstractTransition(Object eventId) {
50          this(eventId, null);
51      }
52  
53      /**
54       * Creates a new instance with the specified {@link State} as next state 
55       * and for the specified {@link Event} id.
56       * 
57       * @param eventId the {@link Event} id.
58       * @param nextState the next {@link State}.
59       */
60      public AbstractTransition(Object eventId, State nextState) {
61          this.eventId = eventId;
62          this.nextState = nextState;
63      }
64  
65      public State getNextState() {
66          return nextState;
67      }
68  
69      public boolean execute(Event event) {
70          if (!eventId.equals(Event.WILDCARD_EVENT_ID) && !eventId.equals(event.getId())) {
71              return false;
72          }
73  
74          return doExecute(event);
75      }
76  
77      /**
78       * Executes this {@link Transition}. This method doesn't have to check
79       * if the {@link Event}'s id matches because {@link #execute(Event)} has
80       * already made sure that that is the case.
81       * 
82       * @param event the current {@link Event}.
83       * @return <code>true</code> if the {@link Transition} has been executed 
84       *         successfully and the {@link StateMachine} should move to the 
85       *         next {@link State}. <code>false</code> otherwise.
86       */
87      protected abstract boolean doExecute(Event event);
88  
89      public boolean equals(Object o) {
90          if (!(o instanceof AbstractTransition)) {
91              return false;
92          }
93          if (o == this) {
94              return true;
95          }
96          AbstractTransition that = (AbstractTransition) o;
97          return new EqualsBuilder()
98              .append(eventId, that.eventId)
99              .append(nextState, that.nextState)
100             .isEquals();
101     }
102 
103     public int hashCode() {
104         return new HashCodeBuilder(11, 31).append(eventId).append(nextState).toHashCode();
105     }
106 
107     public String toString() {
108         return new ToStringBuilder(this)
109             .append("eventId", eventId)
110             .append("nextState", nextState)
111             .toString();
112     }
113 }