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

io.katharsis.utils.java.Optional Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
package io.katharsis.utils.java;

import java.util.NoSuchElementException;
import java.util.Objects;

public class Optional {
    private static final Optional EMPTY = new Optional(null);

    private final T value;

    private Optional(T value) {
        this.value = value;
    }

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

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

    @SuppressWarnings("unchecked")
    public static Optional empty() {
        return (Optional) EMPTY;
    }

    public static  Optional of(T value) {
        Objects.requireNonNull(value);
        return new Optional(value);
    }

    public static  Optional ofNullable(T value) {
        return value == null ? Optional.empty() : of(value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy