1 /*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 package org.apache.asn1new.ber.grammar;
18
19 /**
20 * Define a transition between two states of a grammar. It stores the
21 * next state, and the action to execute while transiting.
22 *
23 *
24 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
25 */
26 public class GrammarTransition
27 {
28 //~ Instance fields ----------------------------------------------------------------------------
29
30 /** The next state in the grammar */
31 private int nextState;
32
33 /** The action associated to the transition */
34 private GrammarAction action;
35
36 /** The current state */
37 private int currentState;
38
39 //~ Constructors -------------------------------------------------------------------------------
40
41 /**
42 * Creates a new GrammarTransition object.
43 *
44 * @param currentState The current transition
45 * @param nextState The target state
46 * @param action The action to execute. It could be null.
47 */
48 public GrammarTransition( int currentState, int nextState, GrammarAction action )
49 {
50 this.currentState = currentState;
51 this.nextState = nextState;
52 this.action = action;
53 }
54
55 //~ Methods ------------------------------------------------------------------------------------
56
57 /**
58 * @return Returns the target state.
59 */
60 public int getNextState()
61 {
62 return nextState;
63 }
64
65 /**
66 * Tells if the transition has an associated action.
67 *
68 * @return <code>true</code> if an action has been asociated to the transition
69 */
70 public boolean hasAction()
71 {
72 return action != null;
73 }
74
75 /**
76 * @return Returns the action associated with the transition
77 */
78 public GrammarAction getAction()
79 {
80 return action;
81 }
82
83 /**
84 * @param grammar The grammar which state we want a String from
85 * @return A representation of the transition as a string.
86 */
87 public String toString(int grammar, IStates statesEnum)
88 {
89
90 StringBuffer sb = new StringBuffer();
91
92 sb.append( "Transition from <" ).append( statesEnum.getState( grammar, currentState ) ).append( "> to <" )
93 .append( statesEnum.getState( grammar, nextState ) ).append( ">, action : " )
94 .append( ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
95
96 return sb.toString();
97 }
98 }