@Override public Optional<ValueAndBound<T>> getAgreedValue() { return getCoordinationValue() .map(sequenceAndBound -> ValueAndBound.of( getValue(sequenceAndBound.sequence()), sequenceAndBound.bound())); }
@Override public CheckAndSetResult<ValueAndBound<T>> transformAgreedValue(Function<ValueAndBound<T>, T> transform) { Optional<SequenceAndBound> coordinationValue = getCoordinationValue(); ValueAndBound<T> extantValueAndBound = ValueAndBound.of(coordinationValue.flatMap( sequenceAndBound -> getValue(sequenceAndBound.sequence())), coordinationValue.map(SequenceAndBound::bound).orElse(SequenceAndBound.INVALID_BOUND)); T targetValue = transform.apply(extantValueAndBound); SequenceAndBound newSequenceAndBound = determineNewSequenceAndBound(coordinationValue, extantValueAndBound, targetValue); CheckAndSetResult<SequenceAndBound> casResult = checkAndSetCoordinationValue( coordinationValue, newSequenceAndBound); return extractRelevantValues(targetValue, newSequenceAndBound.bound(), casResult); }
@Test public void valuePreservingTransformationsDoNotWriteTheSameValueAgain() { coordinationStore.transformAgreedValue(unused -> VALUE_1); SequenceAndBound firstSequenceAndBound = coordinationStore.getCoordinationValue().get(); coordinationStore.transformAgreedValue(VALUE_PRESERVING_FUNCTION); SequenceAndBound secondSequenceAndBound = coordinationStore.getCoordinationValue().get(); assertThat(firstSequenceAndBound.sequence()).isEqualTo(secondSequenceAndBound.sequence()); assertThat(firstSequenceAndBound.bound()).isLessThan(secondSequenceAndBound.bound()); }
@Override public Optional<ValueAndBound<T>> getAgreedValue() { return getCoordinationValue() .map(sequenceAndBound -> ValueAndBound.of( getValue(sequenceAndBound.sequence()), sequenceAndBound.bound())); }
@Override public CheckAndSetResult<ValueAndBound<T>> transformAgreedValue(Function<ValueAndBound<T>, T> transform) { Optional<SequenceAndBound> coordinationValue = getCoordinationValue(); ValueAndBound<T> extantValueAndBound = ValueAndBound.of(coordinationValue.flatMap( sequenceAndBound -> getValue(sequenceAndBound.sequence())), coordinationValue.map(SequenceAndBound::bound).orElse(SequenceAndBound.INVALID_BOUND)); T targetValue = transform.apply(extantValueAndBound); SequenceAndBound newSequenceAndBound = determineNewSequenceAndBound(coordinationValue, extantValueAndBound, targetValue); CheckAndSetResult<SequenceAndBound> casResult = checkAndSetCoordinationValue( coordinationValue, newSequenceAndBound); return extractRelevantValues(targetValue, newSequenceAndBound.bound(), casResult); }