Last active
August 29, 2015 14:07
-
-
Save V0L0DYMYR/8141be8cafd7082971b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StateMachine { | |
| public boolean isNumber(String s) { | |
| boolean result = false; | |
| if (s!= null && s.length() > 0) { | |
| State state = State.INIT; | |
| int i = 0; | |
| while (!state.isEnd() && i < s.length()) { | |
| char c = s.charAt(i++); | |
| state = state.next(c); | |
| } | |
| state = state.next(' '); | |
| if (state == State.VALID_END) result = true; | |
| } | |
| return result; | |
| } | |
| public enum State { | |
| INIT { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return INIT; | |
| if (c == '-' || c == '+') return SIGN; | |
| if (c == '.') return START_DOT; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| SIGN { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (c == '.') return START_DOT; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| START_DOT { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return DECIMAL_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| BASE_NUM { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return VALID_END; | |
| if (c == '.') return DOT; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| DOT { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return VALID_END; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return DECIMAL_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| DECIMAL_NUM { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return VALID_END; | |
| if (c == 'e') return E; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return DECIMAL_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| E { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return EXP_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| EXP_NUM { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return VALID_END; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return EXP_NUM; | |
| return INVALID_END; | |
| } | |
| }, | |
| VALID_END { | |
| @Override | |
| public State next(char c) { | |
| return VALID_END; | |
| } | |
| }, | |
| INVALID_END { | |
| @Override | |
| public State next(char c) { | |
| return INVALID_END; | |
| } | |
| }; | |
| public abstract State next(char c); | |
| public boolean isEnd() { | |
| return this == VALID_END || this == INVALID_END; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment