com.g2forge.alexandria.java.resource.SingleThreadResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ax-java Show documentation
Show all versions of ax-java Show documentation
Standard Java library and the basis of the ${alexandria.name} project.
package com.g2forge.alexandria.java.resource;
import com.g2forge.alexandria.java.close.ICloseable;
public class SingleThreadResource implements ICloseableResource {
protected final ThreadLocal local = new ThreadLocal<>();
public void close(T value) {
if (local.get() != value) throw new IllegalStateException();
local.remove();
}
@Override
public T get() {
return local.get();
}
@Override
public ICloseable open(T value) {
if (local.get() != null) throw new IllegalStateException();
local.set(value);
return () -> close(value);
}
}