private ScanState switchState(ScanState desired,EnumSet<ScanState> allowed){
  final ScanState old;
  final long timestamp;
  final long sequence;
synchronized (this) {
    old=state;
    if (!allowed.contains(state))     throw new IllegalStateException(state.toString());
    state=desired;
    timestamp=System.currentTimeMillis();
    sequence=getNextSeqNumber();
  }
  LOG.fine("switched state: " + old + " -> "+ desired);
  if (old != desired)   queueStateChangedNotification(sequence,timestamp,old,desired);
  return old;
}

private ScanState switchState ( ScanState desired , EnumSet < ScanState > allowed ) { final ScanState old ; final long timestamp ; final long sequence ; synchronized ( this ) { old = state ; if ( ! allowed . contains ( state ) ) throw new IllegalStateException ( state . toString ( ) ) ; state = desired ; timestamp = System . currentTimeMillis ( ) ; sequence = getNextSeqNumber ( ) ; } LOG . fine ( "switched state: " + old + " -> " + desired ) ; if ( old != desired ) queueStateChangedNotification ( sequence , timestamp , old , desired ) ; return old ; }
checks that the current state is one of the allowed states , and if so , switch its value to the new desired state . this operation also enqueue the appropriate state changed notification .
