com.davidbracewell.Lazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
A set of utilities and tools to speed up and ease programming in Java.
package com.davidbracewell;
import com.davidbracewell.function.SerializableSupplier;
import lombok.NonNull;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicReference;
/**
* Lazily create a value in a thread safe manner.
*
* @param the type parameter
* @author David B. Bracewell
*/
public final class Lazy implements Serializable {
private static final long serialVersionUID = 1L;
private volatile AtomicReference atomicReference = new AtomicReference<>(null);
private volatile SerializableSupplier extends T> supplier;
/**
* Instantiates a new Lazy.
*
* @param supplier the supplier used to generate the value
*/
public Lazy(@NonNull SerializableSupplier extends T> supplier) {
this.supplier = supplier;
}
/**
* Gets the value lazily
*
* @return the value
*/
public T get() {
atomicReference.compareAndSet(null, supplier.get());
return atomicReference.get();
}
}// END OF Lazy
© 2015 - 2025 Weber Informatics LLC | Privacy Policy