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

com.tinkerpop.gremlin.util.SOptional Maven / Gradle / Ivy

package com.tinkerpop.gremlin.util;

import com.tinkerpop.gremlin.util.function.SFunction;
import com.tinkerpop.gremlin.util.function.SPredicate;
import com.tinkerpop.gremlin.util.function.SSupplier;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class SOptional implements Serializable {

    private static final SOptional EMPTY = new SOptional<>();

    private final T value;

    private SOptional() {
        this.value = null;
    }

    public static  SOptional empty() {
        return (SOptional) EMPTY;
    }


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

    public static  SOptional of(T value) {
        return new SOptional<>(value);
    }

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

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

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

    public void ifPresent(Consumer consumer) {
        if (value != null)
            consumer.accept(value);
    }

    public SOptional filter(SPredicate predicate) {
        Objects.requireNonNull(predicate);
        if (!isPresent())
            return this;
        else
            return predicate.test(value) ? this : empty();
    }

    public  SOptional map(SFunction mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return SOptional.ofNullable(mapper.apply(value));
        }
    }

    public  SOptional flatMap(SFunction> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return Objects.requireNonNull(mapper.apply(value));
        }
    }

    public T orElse(T other) {
        return value != null ? value : other;
    }

    public T orElseGet(SSupplier other) {
        return value != null ? value : other.get();
    }

    public  T orElseThrow(SSupplier exceptionSupplier) throws X {
        if (value != null) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof SOptional)) {
            return false;
        }

        SOptional other = (SOptional) obj;
        return Objects.equals(value, other.value);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(value);
    }

    @Override
    public String toString() {
        return value != null
                ? String.format("Optional[%s]", value)
                : "Optional.empty";
    }
}