001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.statemachine.transition;
021:
022: import org.apache.commons.lang.builder.EqualsBuilder;
023: import org.apache.commons.lang.builder.HashCodeBuilder;
024: import org.apache.commons.lang.builder.ToStringBuilder;
025: import org.apache.mina.statemachine.State;
026: import org.apache.mina.statemachine.StateMachine;
027: import org.apache.mina.statemachine.event.Event;
028:
029: /**
030: * Abstract {@link Transition} implementation. Takes care of matching the
031: * current {@link Event}'s id against the id of the {@link Event} this
032: * {@link Transition} handles. To handle any {@link Event} the id should be set
033: * to {@link Event#WILDCARD_EVENT_ID}.
034: *
035: * @author The Apache MINA Project (dev@mina.apache.org)
036: * @version $Rev: 586695 $, $Date: 2007-10-20 04:01:17 -0600 (Sat, 20 Oct 2007) $
037: */
038: public abstract class AbstractTransition implements Transition {
039: private final Object eventId;
040:
041: private final State nextState;
042:
043: /**
044: * Creates a new instance which will loopback to the same {@link State}
045: * for the specified {@link Event} id.
046: *
047: * @param eventId the {@link Event} id.
048: */
049: public AbstractTransition(Object eventId) {
050: this (eventId, null);
051: }
052:
053: /**
054: * Creates a new instance with the specified {@link State} as next state
055: * and for the specified {@link Event} id.
056: *
057: * @param eventId the {@link Event} id.
058: * @param nextState the next {@link State}.
059: */
060: public AbstractTransition(Object eventId, State nextState) {
061: this .eventId = eventId;
062: this .nextState = nextState;
063: }
064:
065: public State getNextState() {
066: return nextState;
067: }
068:
069: public boolean execute(Event event) {
070: if (!eventId.equals(Event.WILDCARD_EVENT_ID)
071: && !eventId.equals(event.getId())) {
072: return false;
073: }
074:
075: return doExecute(event);
076: }
077:
078: /**
079: * Executes this {@link Transition}. This method doesn't have to check
080: * if the {@link Event}'s id matches because {@link #execute(Event)} has
081: * already made sure that that is the case.
082: *
083: * @param event the current {@link Event}.
084: * @return <code>true</code> if the {@link Transition} has been executed
085: * successfully and the {@link StateMachine} should move to the
086: * next {@link State}. <code>false</code> otherwise.
087: */
088: protected abstract boolean doExecute(Event event);
089:
090: public boolean equals(Object o) {
091: if (!(o instanceof AbstractTransition)) {
092: return false;
093: }
094: if (o == this ) {
095: return true;
096: }
097: AbstractTransition that = (AbstractTransition) o;
098: return new EqualsBuilder().append(eventId, that.eventId)
099: .append(nextState, that.nextState).isEquals();
100: }
101:
102: public int hashCode() {
103: return new HashCodeBuilder(11, 31).append(eventId).append(
104: nextState).toHashCode();
105: }
106:
107: public String toString() {
108: return new ToStringBuilder(this ).append("eventId", eventId)
109: .append("nextState", nextState).toString();
110: }
111: }
|