All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.g2forge.alexandria.java.resource.SingleThreadResource Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy