View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.dsmlv2;
21  
22  
23  /**
24   * Define a transition between two states of a grammar. It stores the next
25   * state, and the action to execute while transiting.
26   * 
27   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
28   */
29  public class GrammarTransition
30  {
31      /** The next state in the grammar */
32      private Enum<Dsmlv2StatesEnum> nextState;
33  
34      /** The action associated to the transition */
35      private GrammarAction action;
36  
37      /** The current state */
38      private Enum<Dsmlv2StatesEnum> currentState;
39  
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( Enum<Dsmlv2StatesEnum> currentState, Enum<Dsmlv2StatesEnum> nextState,
49          GrammarAction action )
50      {
51          this.currentState = currentState;
52          this.nextState = nextState;
53          this.action = action;
54      }
55  
56  
57      /**
58       * Gets the target state
59       * 
60       * @return the target state.
61       */
62      public Enum<Dsmlv2StatesEnum> getNextState()
63      {
64          return nextState;
65      }
66  
67  
68      /**
69       * Tells if the transition has an associated action.
70       * 
71       * @return  <code>true</code> if an action has been associated to the transition
72       */
73      public boolean hasAction()
74      {
75          return action != null;
76      }
77  
78  
79      /**
80       * Gets the action associated with the transition
81       * 
82       * @return the action associated with the transition
83       */
84      public GrammarAction getAction()
85      {
86          return action;
87      }
88  
89  
90      /**
91       * Returns a representation of the transition as a string
92       * 
93       * @param grammar the grammar which state we want a String from
94       * @param statesEnum the states enum that contains the states' names
95       * @return  a representation of the transition as a string.
96       */
97      public String toString( int grammar, Enum<Dsmlv2StatesEnum> statesEnum )
98      {
99  
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 }