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.asn1.ber.grammar;
21  
22  
23  import org.apache.directory.api.asn1.ber.Asn1Container;
24  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
25  import org.apache.directory.api.asn1.util.Asn1StringUtils;
26  
27  
28  /**
29   * Define a transition between two states of a grammar. It stores the next
30   * state, and the action to execute while executing the transition.
31   * 
32   * @param <C> The container type
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class GrammarTransition<C extends Asn1Container>
37  {
38      /** The action associated to the transition */
39      private Action<C> action;
40  
41      /** The previous state */
42      private Enum<?> previousState;
43  
44      /** The current state */
45      private Enum<?> currentState;
46  
47      /** The current tag */
48      private int currentTag;
49  
50  
51      /**
52       * Creates a new GrammarTransition object.
53       *
54       * @param previousState the previous state
55       * @param currentState The current state
56       * @param currentTag the current TLV's tag
57       * @param action The action to execute. It could be null.
58       */
59      public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag, Action<C> action )
60      {
61          this.previousState = previousState;
62          this.currentState = currentState;
63          this.action = action;
64          this.currentTag = currentTag;
65      }
66  
67  
68      /**
69       * Creates a new GrammarTransition object.
70       *
71       * @param previousState the previous state
72       * @param currentState The current state
73       * @param currentTag the current TLV's tag
74       */
75      public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag )
76      {
77          this.previousState = previousState;
78          this.currentState = currentState;
79          this.currentTag = currentTag;
80      }
81  
82  
83      /**
84       * Creates a new GrammarTransition object.
85       *
86       * @param previousState the previous state
87       * @param currentState The current state
88       * @param currentTag the current TLV's tag
89       * @param action The action to execute. It could be null.
90       */
91      public GrammarTransition( Enum<?> previousState, Enum<?> currentState, UniversalTag currentTag, Action<C> action )
92      {
93          this.previousState = previousState;
94          this.currentState = currentState;
95          this.action = action;
96          this.currentTag = currentTag.getValue();
97      }
98  
99  
100     /**
101      * Creates a new GrammarTransition object.
102      *
103      * @param previousState the previous state
104      * @param currentState The current state
105      * @param currentTag the current TLV's tag
106      */
107     public GrammarTransition( Enum<?> previousState, Enum<?> currentState, UniversalTag currentTag )
108     {
109         this.previousState = previousState;
110         this.currentState = currentState;
111         this.currentTag = currentTag.getValue();
112     }
113 
114 
115     /**
116      * Tells if the transition has an associated action.
117      *
118      * @return <code>true</code> if an action has been associated to the transition
119      */
120     public boolean hasAction()
121     {
122         return action != null;
123     }
124 
125 
126     /**
127      * @return Returns the action associated with the transition
128      */
129     public Action<C> getAction()
130     {
131         return action;
132     }
133 
134 
135     /**
136      * @return The current state
137      */
138     public Enum<?> getCurrentState()
139     {
140         return currentState;
141     }
142 
143 
144     /**
145      * @return The previous state
146      */
147     public Enum<?> getPreviousState()
148     {
149         return previousState;
150     }
151 
152 
153     /**
154      * @return A representation of the transition as a string.
155      */
156     @Override
157     public String toString()
158     {
159         StringBuilder sb = new StringBuilder();
160 
161         sb.append( "Transition from state <" ).append( previousState ).append( "> " );
162         sb.append( "to state <" ).append( currentState ).append( ">, " );
163         sb.append( "tag <" ).append( Asn1StringUtils.dumpByte( ( byte ) currentTag ) ).append( ">, " );
164         sb.append( "action : " );
165 
166         if ( action == null )
167         {
168             sb.append( "no action" );
169         }
170         else
171         {
172             sb.append( action );
173         }
174 
175         return sb.toString();
176     }
177 }