A generic implementation of a finite state machine.