state machine pattern for java