/** * Atomically sets the state to <code>update</code> if and only if the * state is currently <code>expected</code>. * @param expected the expected state. * @param update the new state. * @return true if the update succeeds, or false if the current state * does not equal <code>expected</code>. */ private synchronized boolean compareAndSetState(State expected, State update) { if (state == expected) { setState(update); return true; } else { return false; } }
/** * Atomically sets the state to <code>update</code> if and only if the * state is currently one of <code>expectedStates</code>. * @param expectedStates the expected states. * @param update the new state. * @return true if the update succeeds, or false if the current state * does not equal any of the <code>expectedStates</code>. */ private synchronized boolean compareAndSetState(State[] expectedStates, State update) { for (State expected : expectedStates) { if (state == expected) { setState(update); return true; } } return false; }