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 */
020package org.apache.directory.shared.dsmlv2;
021
022
023/**
024 * Define a transition between two states of a grammar. It stores the next
025 * state, and the action to execute while transiting.
026 * 
027 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
028 */
029public class GrammarTransition
030{
031    /** The next state in the grammar */
032    private Enum<Dsmlv2StatesEnum> nextState;
033
034    /** The action associated to the transition */
035    private GrammarAction action;
036
037    /** The current state */
038    private Enum<Dsmlv2StatesEnum> currentState;
039
040    
041    /**
042     * Creates a new GrammarTransition object.
043     * 
044     * @param currentState
045     *      The current transition
046     * @param nextState
047     *      The target state
048     * @param action
049     *      The action to execute. It could be null.
050     */
051    public GrammarTransition( Enum<Dsmlv2StatesEnum> currentState, Enum<Dsmlv2StatesEnum> nextState, GrammarAction action )
052    {
053        this.currentState = currentState;
054        this.nextState = nextState;
055        this.action = action;
056    }
057
058    /**
059     * Gets the target state
060     * 
061     * @return
062     *      the target state.
063     */
064    public Enum<Dsmlv2StatesEnum> getNextState()
065    {
066        return nextState;
067    }
068
069
070    /**
071     * Tells if the transition has an associated action.
072     * 
073     * @return 
074     *      <code>true</code> if an action has been asociated to the
075     *         transition
076     */
077    public boolean hasAction()
078    {
079        return action != null;
080    }
081
082
083    /**
084     * Gets the action associated with the transition
085     * 
086     * @return
087     *      the action associated with the transition
088     */
089    public GrammarAction getAction()
090    {
091        return action;
092    }
093
094
095    /**
096     * Returns a representation of the transition as a string
097     * 
098     * @param grammar
099     *      the grammar which state we want a String from
100     * @param statesEnum
101     *      the states enum that contains the states' names
102     * @return 
103     *      a representation of the transition as a string.
104     */
105    public String toString( int grammar, Enum<Dsmlv2StatesEnum> statesEnum )
106    {
107
108        StringBuffer sb = new StringBuffer();
109
110        sb.append( "Transition from <" ).append( currentState ).append( "> to <" ).append(
111            nextState ).append( ">, action : " ).append(
112            ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
113
114        return sb.toString();
115    }
116}