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

com.davidbracewell.Lazy Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
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 supplier; /** * Instantiates a new Lazy. * * @param supplier the supplier used to generate the value */ public Lazy(@NonNull SerializableSupplier 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