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

com.icthh.xm.commons.tenant.internal.ValueHolder Maven / Gradle / Ivy

There is a newer version: 4.0.20
Show newest version
package com.icthh.xm.commons.tenant.internal;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * The {@link ValueHolder} class.
 */
final class ValueHolder {

    private static volatile ValueHolder emptyInstance = new ValueHolder<>();

    private final T value;

    private ValueHolder() {
        this.value = null; //NOPMD
    }

    private ValueHolder(T value) {
        this.value = Objects.requireNonNull(value);
    }

    @SuppressWarnings("unchecked")
    static  ValueHolder empty() {
        return (ValueHolder) emptyInstance;
    }

    static  ValueHolder valueOf(T value) {
        return new ValueHolder<>(value);
    }

    boolean isPresent() {
        return value != null;
    }

    boolean isEmpty() {
        return this == emptyInstance;
    }

    T get() {
        if (value == null) {
            throw new NoSuchElementException("No value present");
        }
        return value;
    }

     T orElseThrow(Supplier exceptionSupplier) throws X {
        if (isPresent()) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy