@Test public void shouldReturnUnderlyingTokenAfterAllReferencesAreUnlocked() { List<LockTokenShare> tokens = getSharedLockTokens(LOCK_TOKEN, 2); LockTokenShare firstToken = tokens.get(0); LockTokenShare secondToken = tokens.get(1); assertThat(firstToken.unlock()) .as("no token as second token still has reference") .isEmpty(); assertThat(firstToken.unlock()) .as("no token as second token still has reference") .isEmpty(); assertThat(secondToken.unlock()) .as("get lock token since both references are unlocked") .contains(LOCK_TOKEN); assertThat(firstToken.unlock()) .as("returns empty as unlock should return only once") .isEmpty(); }
synchronized Optional<LockToken> unlock() { if (!unlocked) { unlocked = true; return referenceCounter.unmark() ? Optional.of(sharedLockToken) : Optional.empty(); } return Optional.empty(); }
LockTokenShare implements LockToken { synchronized Optional<LockToken> unlock() { if (!unlocked) { unlocked = true; return referenceCounter.unmark() ? Optional.of(sharedLockToken) : Optional.empty(); } return Optional.empty(); } }
LockTokenShare implements LockToken { synchronized Optional<LockToken> unlock() { if (!unlocked) { unlocked = true; return referenceCounter.unmark() ? Optional.of(sharedLockToken) : Optional.empty(); } return Optional.empty(); } private  LockTokenShare(ReferenceCounter referenceCounter, LockToken token); }
LockTokenShare implements LockToken { synchronized Optional<LockToken> unlock() { if (!unlocked) { unlocked = true; return referenceCounter.unmark() ? Optional.of(sharedLockToken) : Optional.empty(); } return Optional.empty(); } private  LockTokenShare(ReferenceCounter referenceCounter, LockToken token); @Override UUID getRequestId(); @Override SafeArg<?> toSafeArg(String name); }
LockTokenShare implements LockToken { synchronized Optional<LockToken> unlock() { if (!unlocked) { unlocked = true; return referenceCounter.unmark() ? Optional.of(sharedLockToken) : Optional.empty(); } return Optional.empty(); } private  LockTokenShare(ReferenceCounter referenceCounter, LockToken token); @Override UUID getRequestId(); @Override SafeArg<?> toSafeArg(String name);  }