38 lines
973 B
Java
38 lines
973 B
Java
package io.gitlab.jfronny.libjf.mainhttp.impl.util;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
public class ClaimPool<T> {
|
|
private final List<Claim> content = new LinkedList<>();
|
|
|
|
public Claim claim(T value) {
|
|
return new Claim(value);
|
|
}
|
|
|
|
public T getTopmost() {
|
|
return content.isEmpty() ? null : content.get(content.size() - 1).value;
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
return content.isEmpty();
|
|
}
|
|
|
|
public class Claim {
|
|
private final T value;
|
|
private final AtomicBoolean active = new AtomicBoolean(true);
|
|
|
|
private Claim(T value) {
|
|
this.value = value;
|
|
content.add(this);
|
|
}
|
|
|
|
public void release() {
|
|
if (!active.getAndSet(false))
|
|
throw new UnsupportedOperationException("Cannot release claim that is already released");
|
|
content.remove(this);
|
|
}
|
|
}
|
|
}
|