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 <a href="http://mina.apache.org">Apache MINA Project</a>
36 */
37 public abstract class AbstractTransition implements Transition {
38 private final Object eventId;
39
40 private final State nextState;
41
42 /**
43 * Creates a new instance which will loopback to the same {@link State}
44 * for the specified {@link Event} id.
45 *
46 * @param eventId the {@link Event} id.
47 */
48 public AbstractTransition(Object eventId) {
49 this(eventId, null);
50 }
51
52 /**
53 * Creates a new instance with the specified {@link State} as next state
54 * and for the specified {@link Event} id.
55 *
56 * @param eventId the {@link Event} id.
57 * @param nextState the next {@link State}.
58 */
59 public AbstractTransition(Object eventId, State nextState) {
60 this.eventId = eventId;
61 this.nextState = nextState;
62 }
63
64 public State getNextState() {
65 return nextState;
66 }
67
68 public boolean execute(Event event) {
69 if (!eventId.equals(Event.WILDCARD_EVENT_ID) && !eventId.equals(event.getId())) {
70 return false;
71 }
72
73 return doExecute(event);
74 }
75
76 /**
77 * Executes this {@link Transition}. This method doesn't have to check
78 * if the {@link Event}'s id matches because {@link #execute(Event)} has
79 * already made sure that that is the case.
80 *
81 * @param event the current {@link Event}.
82 * @return <code>true</code> if the {@link Transition} has been executed
83 * successfully and the {@link StateMachine} should move to the
84 * next {@link State}. <code>false</code> otherwise.
85 */
86 protected abstract boolean doExecute(Event event);
87
88 public boolean equals(Object o) {
89 if (!(o instanceof AbstractTransition)) {
90 return false;
91 }
92 if (o == this) {
93 return true;
94 }
95 AbstractTransition that = (AbstractTransition) o;
96 return new EqualsBuilder()
97 .append(eventId, that.eventId)
98 .append(nextState, that.nextState)
99 .isEquals();
100 }
101
102 public int hashCode() {
103 return new HashCodeBuilder(11, 31).append(eventId).append(nextState).toHashCode();
104 }
105
106 public String toString() {
107 return new ToStringBuilder(this)
108 .append("eventId", eventId)
109 .append("nextState", nextState)
110 .toString();
111 }
112 }