public boolean set ( State state ) { while ( true ) { InternalState internalState = currentInternalState . get ( ) ; checkState ( ! internalState . isRead , "State was already read, cannot set state." ) ; if ( state == internalState . state ) { return false ; } else { if ( ! currentInternalState . compareAndSet ( internalState , state == State . ENABLED ? InternalState . ENABLED_NOT_READ : InternalState . DISABLED_NOT_READ ) ) { // The state was changed between the moment the internalState was read and this point. // Some conditions may be not correct, reset at the beginning and recheck all conditions. continue ; } return true ; } } }
Sets current state to the given state . Returns true if the current state is changed false otherwise .