class IntegerParser { public IntegerParser () { // Create a default transition that leads to a failure state. FailureState failureState = new FailureState(); DefaultTransition defaultToFailureTransition = new DefaultTransition(failureState); // Create a success state. SuccessState successState = new SuccessState(); // Create a state that represents a partial integer parsing. State partialState = new State(defaultToFailureTransition, "Intermediate"); WhiteSpaceTransition whiteSpaceTransition = new WhiteSpaceTransition(successState); partialState.addTransition(whiteSpaceTransition); DigitTransition digitTransition = new DigitTransition(partialState); partialState.addTransition(digitTransition); // Create the initial state. _initialState = new InitialState(defaultToFailureTransition, "Initial"); DigitTransition negativeOrDigitTransition = new DigitTransition(partialState); negativeOrDigitTransition.addSymbol('-'); _initialState.addTransition(negativeOrDigitTransition); } public boolean parse (String s) { // Iterate through the symbols, each time transitioning to a // new state until success, failure, or the end of the string // is reached. int index = 0; State currentState = _initialState; while ((index < s.length()) && (!currentState.success()) && (!currentState.failure())) { char c = s.charAt(index); currentState = currentState.followTransition(c); index++; } return currentState.success(); } protected void testString (String s) { boolean result = parse(s); System.out.println(s + ": " + result); } public static void main (String[] args) { IntegerParser ip = new IntegerParser(); ip.testString("4 "); ip.testString("143 "); ip.testString("-15\n"); ip.testString("Foo"); ip.testString("668\t-- The Neighbor of The Beast!"); } protected InitialState _initialState; }