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.shared.asn1.ber.grammar; 021 022 023import org.apache.directory.shared.asn1.ber.Asn1Container; 024import org.apache.directory.shared.asn1.ber.tlv.UniversalTag; 025import org.apache.directory.shared.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 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 */ 034public class GrammarTransition<E extends Asn1Container> 035{ 036 /** The action associated to the transition */ 037 private Action<E> action; 038 039 /** The previous state */ 040 private Enum<?> previousState; 041 042 /** The current state */ 043 private Enum<?> currentState; 044 045 /** The current tag */ 046 private int currentTag; 047 048 049 /** 050 * Creates a new GrammarTransition object. 051 * 052 * @param previousState the previous state 053 * @param currentState The current state 054 * @param currentTag the current TLV's tag 055 * @param action The action to execute. It could be null. 056 */ 057 public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag, Action<E> action ) 058 { 059 this.previousState = previousState; 060 this.currentState = currentState; 061 this.action = action; 062 this.currentTag = currentTag; 063 } 064 065 066 /** 067 * Creates a new GrammarTransition object. 068 * 069 * @param previousState the previous state 070 * @param currentState The current state 071 * @param currentTag the current TLV's tag 072 */ 073 public GrammarTransition( Enum<?> previousState, Enum<?> currentState, int currentTag ) 074 { 075 this.previousState = previousState; 076 this.currentState = currentState; 077 this.currentTag = currentTag; 078 } 079 080 081 /** 082 * Creates a new GrammarTransition object. 083 * 084 * @param previousState the previous state 085 * @param currentState The current state 086 * @param currentTag the current TLV's tag 087 * @param action The action to execute. It could be null. 088 */ 089 public GrammarTransition( Enum<?> previousState, Enum<?> currentState, UniversalTag currentTag, Action<E> action ) 090 { 091 this.previousState = previousState; 092 this.currentState = currentState; 093 this.action = action; 094 this.currentTag = currentTag.getValue(); 095 } 096 097 098 /** 099 * Creates a new GrammarTransition object. 100 * 101 * @param previousState the previous state 102 * @param currentState The current state 103 * @param currentTag the current TLV's tag 104 */ 105 public GrammarTransition( Enum<?> previousState, Enum<?> currentState, UniversalTag currentTag ) 106 { 107 this.previousState = previousState; 108 this.currentState = currentState; 109 this.currentTag = currentTag.getValue(); 110 } 111 112 113 /** 114 * Tells if the transition has an associated action. 115 * 116 * @return <code>true</code> if an action has been associated to the transition 117 */ 118 public boolean hasAction() 119 { 120 return action != null; 121 } 122 123 124 /** 125 * @return Returns the action associated with the transition 126 */ 127 public Action<E> getAction() 128 { 129 return action; 130 } 131 132 133 /** 134 * @return The current state 135 */ 136 public Enum<?> getCurrentState() 137 { 138 return currentState; 139 } 140 141 142 /** 143 * @return The previous state 144 */ 145 public Enum<?> getPreviousState() 146 { 147 return previousState; 148 } 149 150 151 /** 152 * @param statesEnum Starting state. 153 * @return A representation of the transition as a string. 154 */ 155 @Override 156 public String toString( ) 157 { 158 StringBuilder sb = new StringBuilder(); 159 160 sb.append( "Transition from state <" ).append( previousState ).append( "> " ); 161 sb.append( "to state <" ).append( currentState ).append( ">, " ); 162 sb.append( "tag <" ).append( Asn1StringUtils.dumpByte( ( byte ) currentTag ) ).append( ">, " ); 163 sb.append( "action : " ); 164 165 if ( action == null ) 166 { 167 sb.append( "no action" ); 168 } 169 else 170 { 171 sb.append( action ); 172 } 173 174 return sb.toString(); 175 } 176}