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.api.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 The current transition
045     * @param nextState The target state
046     * @param action The action to execute. It could be null.
047     */
048    public GrammarTransition( Enum<Dsmlv2StatesEnum> currentState, Enum<Dsmlv2StatesEnum> nextState,
049        GrammarAction action )
050    {
051        this.currentState = currentState;
052        this.nextState = nextState;
053        this.action = action;
054    }
055
056
057    /**
058     * Gets the target state
059     * 
060     * @return the target state.
061     */
062    public Enum<Dsmlv2StatesEnum> getNextState()
063    {
064        return nextState;
065    }
066
067
068    /**
069     * Tells if the transition has an associated action.
070     * 
071     * @return  <code>true</code> if an action has been associated to the transition
072     */
073    public boolean hasAction()
074    {
075        return action != null;
076    }
077
078
079    /**
080     * Gets the action associated with the transition
081     * 
082     * @return the action associated with the transition
083     */
084    public GrammarAction getAction()
085    {
086        return action;
087    }
088
089
090    /**
091     * Returns a representation of the transition as a string
092     * 
093     * @param grammar the grammar which state we want a String from
094     * @param statesEnum the states enum that contains the states' names
095     * @return  a representation of the transition as a string.
096     */
097    public String toString( int grammar, Enum<Dsmlv2StatesEnum> statesEnum )
098    {
099
100        StringBuffer sb = new StringBuffer();
101
102        sb.append( "Transition from <" ).append( currentState ).append( "> to <" ).append(
103            nextState ).append( ">, action : " ).append(
104            ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
105
106        return sb.toString();
107    }
108}