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

org.rx.util.Lazy Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.util;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.rx.util.function.Func;

@RequiredArgsConstructor
public final class Lazy {
    private Func func;
    private volatile T value;

    public boolean isValueCreated() {
        return value != null;
    }

    @SneakyThrows
    public T getValue() {
        if (value == null) {
            synchronized (this) {
                if (value == null) {
                    value = func.invoke();
                    func = null;
                }
            }
        }
        return value;
    }

    public Lazy(@NonNull Func func) {
        this.func = func;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy