Prevent Acquired from being used after unlock

This commit is contained in:
TheMode 2021-04-24 21:39:39 +02:00
parent 9c99cb9b2e
commit 41a0f22468

View File

@ -1,5 +1,6 @@
package net.minestom.server.acquirable;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -12,6 +13,8 @@ public class Acquired<T> {
private final boolean locked;
private final ReentrantLock lock;
private boolean unlocked;
protected Acquired(@NotNull T value,
boolean locked, @Nullable ReentrantLock lock) {
this.value = value;
@ -20,13 +23,20 @@ public class Acquired<T> {
}
public @NotNull T get() {
checkLock();
return value;
}
public void unlock() {
checkLock();
this.unlocked = true;
if (!locked)
return;
Acquisition.acquireLeave(lock);
}
private void checkLock() {
Check.stateCondition(unlocked, "The acquired element has already been unlocked!");
}
}