com.xqbase.util.Lazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xqbase-util-jdk17 Show documentation
Show all versions of xqbase-util-jdk17 Show documentation
Reusable Java components for www.xqbase.com
package com.xqbase.util;
import com.xqbase.util.function.ConsumerEx;
import com.xqbase.util.function.SupplierEx;
public class Lazy implements AutoCloseable {
private SupplierEx extends T, ? extends E> initializer;
private ConsumerEx super T, ?> finalizer;
private volatile T instance = null;
public Lazy(SupplierEx extends T, ? extends E> initializer,
ConsumerEx super T, ?> finalizer) {
this.initializer = initializer;
this.finalizer = finalizer;
}
public T get() throws E {
if (instance == null) {
synchronized (this) {
if (instance == null) {
instance = initializer.get();
}
}
}
return instance;
}
@Override
public void close() {
synchronized (this) {
if (instance != null) {
T instance_ = instance;
instance = null;
try {
finalizer.accept(instance_);
} catch (Exception e) {
// Ignored
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy