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.asn1.ber.grammar;
021
022
023import org.apache.directory.api.asn1.ber.Asn1Container;
024import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
025import org.apache.directory.api.asn1.util.Asn1StringUtils;
026
027
028/**
029 * Define a transition between two states of a grammar. It stores the next
030 * state, and the action to execute while executing the transition.
031 * 
032 * @param <C> The container type
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class GrammarTransition<C extends Asn1Container>
037{
038    /** The action associated to the transition */
039    private Action<C> action;
040
041    /** The previous state */
042    private Enum<?> previousState;
043
044    /** The current state */
045    private Enum<?> currentState;
046
047    /** The current tag */
048    private int currentTag;
049
050
051    /**
052     * Creates a new GrammarTransition object.
053     *
054     * @param previousState the previous state
055     * @param currentState The current state
056     * @param currentTag the current TLV's tag
057     * @param action The action to execute. It could be null.
058     */
059    public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag, Action<C> action )
060    {
061        this.previousState = previousState;
062        this.currentState = currentState;
063        this.action = action;
064        this.currentTag = currentTag;
065    }
066
067
068    /**
069     * Creates a new GrammarTransition object.
070     *
071     * @param previousState the previous state
072     * @param currentState The current state
073     * @param currentTag the current TLV's tag
074     */
075    public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag )
076    {
077        this.previousState = previousState;
078        this.currentState = currentState;
079        this.currentTag = currentTag;
080    }
081
082
083    /**
084     * Creates a new GrammarTransition object.
085     *
086     * @param previousState the previous state
087     * @param currentState The current state
088     * @param currentTag the current TLV's tag
089     * @param action The action to execute. It could be null.
090     */
091    public GrammarTransition( Enum<?> previousState, Enum<?> currentState, UniversalTag currentTag, Action<C> action )
092    {
093        this.previousState = previousState;
094        this.currentState = currentState;
095        this.action = action;
096        this.currentTag = currentTag.getValue();
097    }
098
099
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}