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.directory.shared.asn1.ber.grammar;
021    
022    
023    import org.apache.directory.shared.asn1.util.Asn1StringUtils;
024    
025    
026    /**
027     * Define a transition between two states of a grammar. It stores the next
028     * state, and the action to execute while transiting.
029     * 
030     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031     * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $
032     */
033    public class GrammarTransition
034    {
035        /** The action associated to the transition */
036        private IAction action;
037    
038        /** The previous state */
039        private int previousState;
040        
041        /** The current state */
042        private int currentState;
043    
044        /** The current tag */
045        private int currentTag;
046    
047        // ~ Constructors
048        // -------------------------------------------------------------------------------
049    
050        /**
051         * Creates a new GrammarTransition object.
052         * 
053         * @param previousState the previous state
054         * @param currentState The current state
055         * @param currentTag the current TLV's tag
056         * @param action The action to execute. It could be null.
057         */
058        public GrammarTransition( int previousState, int currentState, int currentTag, IAction action )
059        {
060            this.previousState = previousState;
061            this.currentState = currentState;
062            this.action = action;
063            this.currentTag = currentTag;
064        }
065    
066        /**
067         * Tells if the transition has an associated action.
068         * 
069         * @return <code>true</code> if an action has been asociated to the
070         *         transition
071         */
072        public boolean hasAction()
073        {
074            return action != null;
075        }
076    
077        /**
078         * @return Returns the action associated with the transition
079         */
080        public IAction getAction()
081        {
082            return action;
083        }
084    
085        /**
086         * @param statesEnum Starting state.
087         * @return A representation of the transition as a string.
088         */
089        public String toString( IStates statesEnum )
090        {
091    
092            StringBuffer sb = new StringBuffer();
093    
094            sb.append( "Transition from state <" ).append( statesEnum.getState( previousState ) ).append( "> " );
095            sb.append( "to state <" ).append( statesEnum.getState( currentState ) ).append( ">, " );
096            sb.append( "tag <" ).append(  Asn1StringUtils.dumpByte( (byte)currentTag ) ).append( ">, " );
097            sb.append( "action : " ).append( ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
098    
099            return sb.toString();
100        }
101    
102        /**
103         * @return The current state
104         */
105        public int getCurrentState()
106        {
107            return currentState;
108        }
109    
110        /**
111         * @return The previous state
112         */
113        public int getPreviousState()
114        {
115            return previousState;
116        }
117    }