com.g2forge.alexandria.java.resource.SimpleThreadResource 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 SimpleThreadResource implements ICloseableResource {
protected boolean valid;
protected T value;
public void close(T value) {
if (!valid || (this.value != value)) throw new IllegalStateException();
this.value = null;
this.valid = false;
}
@Override
public T get() {
return this.value;
}
@Override
public ICloseable open(T value) {
if (valid || (value != null)) throw new IllegalStateException();
this.value = value;
this.valid = true;
return () -> close(value);
}
}