Simplify Acquirable#lock

This commit is contained in:
TheMode 2021-04-25 23:07:31 +02:00
parent 6c3c6e1438
commit b8b62c4ff7
2 changed files with 5 additions and 9 deletions

View File

@ -83,13 +83,7 @@ public interface Acquirable<T> {
*/ */
default @NotNull Acquired<T> lock() { default @NotNull Acquired<T> lock() {
var optional = local(); var optional = local();
if (optional.isPresent()) { return optional.map(Acquired::local).orElseGet(() -> Acquired.locked(this));
return Acquired.local(optional.get());
} else {
final Thread currentThread = Thread.currentThread();
final TickThread tickThread = getHandler().getTickThread();
return Acquired.locked(unwrap(), currentThread, tickThread);
}
} }
/** /**

View File

@ -19,8 +19,10 @@ public class Acquired<T> {
return new Acquired<>(value, false, null, null); return new Acquired<>(value, false, null, null);
} }
protected static <T> Acquired<T> locked(@NotNull T value, Thread currentThread, TickThread tickThread) { protected static <T> Acquired<T> locked(@NotNull Acquirable<T> acquirable) {
return new Acquired<>(value, true, currentThread, tickThread); final Thread currentThread = Thread.currentThread();
final TickThread tickThread = acquirable.getHandler().getTickThread();
return new Acquired<>(acquirable.unwrap(), true, currentThread, tickThread);
} }
private Acquired(@NotNull T value, private Acquired(@NotNull T value,