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

panda.std.Option Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021 dzikoysk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package panda.std;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import panda.std.function.ThrowingRunnable;
import panda.std.function.ThrowingSupplier;
import panda.std.reactive.Completable;
import panda.std.stream.PandaStream;
import panda.std.collection.SingletonIterator;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class Option implements Iterable, Serializable {

    private static final Option NONE = new Option<>(null);

    protected @Nullable T value;

    protected Option(@Nullable T value) {
        this.value = value;
    }

    @SuppressWarnings("unchecked")
    public static  Option none() {
        return (Option) NONE;
    }

    public static Option blank() {
        return Option.of(Blank.BLANK);
    }

    public static  Option<@NotNull T> of(@Nullable T value) {
        return value != null ? new Option<>(value) : none();
    }

    @SuppressWarnings({ "OptionalUsedAsFieldOrParameterType" })
    public static  Option ofOptional(Optional optional) {
        return of(optional.orElse(null));
    }

    public static  Option> withCompleted(T value) {
        return Option.of(Completable.completed(value));
    }

    public static  Option when(boolean condition, @Nullable T value) {
        return when(condition, () -> value);
    }

    public static  Option when(boolean condition, Supplier<@Nullable T> valueSupplier) {
        return condition ? of(valueSupplier.get()) : Option.none();
    }

    public static  Option flatWhen(boolean condition, Option value) {
        return condition ? value : none();
    }

    public static  Option flatWhen(boolean condition, Supplier> supplier) {
        return condition ? supplier.get() : none();
    }

    @Deprecated
    public static  Option attempt(Class throwableType, ThrowingSupplier supplier) throws AttemptFailedException {
        return supplyThrowing(throwableType, supplier);
    }

    public static Option runThrowing(ThrowingRunnable runnable) throws AttemptFailedException {
        return supplyThrowing(() -> {
            runnable.run();
            return Blank.BLANK;
        });
    }

    public static  Option supplyThrowing(ThrowingSupplier supplier) throws AttemptFailedException {
        return supplyThrowing(Exception.class, supplier);
    }

    public static  Option supplyThrowing(Class throwableType, ThrowingSupplier supplier) throws AttemptFailedException {
        try {
            return of(supplier.get());
        } catch (Throwable throwable) {
            if (throwableType.isAssignableFrom(throwable.getClass())) {
                return Option.none();
            }

            throw new AttemptFailedException(throwable);
        }
    }

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

    @Override
    public boolean equals(@Nullable Object to) {
        if (!(to instanceof Option)) {
            return false;
        }

        return contentEquals(((Option) to).value);
    }

    public boolean contentEquals(@Nullable Object to) {
        return Objects.equals(value, to);
    }

    @Override
    public String toString() {
        return isEmpty() ? "Option{EMPTY}" : "Option{'" + value + "'}";
    }

    @Override
    public Iterator iterator() {
        return isDefined() ? new SingletonIterator<>(value) : Collections.emptyIterator();
    }

    public Option filter(Predicate predicate) {
        return (isDefined() && predicate.test(value)) ? this : Option.none();
    }

    public Option filterNot(Predicate predicate) {
        return filter(value -> !predicate.test(value));
    }

    public  Option map(Function function) {
        return isDefined() ? Option.of(function.apply(value)) : Option.none();
    }

    public  Option flatMap(Function> function) {
        return isDefined() ? function.apply(value) : Option.none();
    }

    public  Option> associateWith(Function> supplier) {
        return flatMap(a -> supplier.apply(a).map(b -> new Pair<>(a, b)));
    }

    @SafeVarargs
    public final  Option match(Case... cases) {
        return match(Arrays.asList(cases));
    }

    public  Option match(List> cases) {
        for (Case currentCase : cases) {
            if (currentCase.getCondition().test(value)) {
                return Option.of(currentCase.getValue().apply(value));
            }
        }

        return Option.none();
    }

    public boolean is(Predicate predicate) {
        return isDefined() && predicate.test(value);
    }

    public boolean isNot(Predicate predicate) {
        return isDefined() && !predicate.test(value);
    }

    public  Option is(Class type) {
        return this
                .filter(type::isInstance)
                .map(type::cast);
    }

    public Option isNot(Class type) {
        return this.filterNot(type::isInstance);
    }

    public Option peek(Consumer consumer) {
        if (isDefined()) {
            consumer.accept(value);
        }

        return this;
    }

    public Option peekIf(Predicate predicate, Consumer consumer) {
        return peek(value -> {
            if (predicate.test(value)) consumer.accept(value);
        });
    }

    public Option peekIfNot(Predicate predicate, Consumer consumer) {
        return peek(value -> {
            if (!predicate.test(value)) consumer.accept(value);
        });
    }

    public Option onEmpty(Runnable runnable) {
        if (isEmpty()) {
            runnable.run();
        }

        return this;
    }

    public Option orElse(T value) {
        return isDefined() ? this : of(value);
    }

    public Option orElse(Option value) {
        return isDefined() ? this : value;
    }

    public Option orElse(Supplier> supplier) {
        return isDefined() ? this : supplier.get();
    }

    public Option orElseSupply(Supplier supplier) {
        return isDefined() ? this : Option.of(supplier.get());
    }

    public  T orThrow(Supplier exceptionSupplier) throws E {
        if (isEmpty()) {
            throw exceptionSupplier.get();
        }

        return value;
    }

    public T orElseGet(T elseValue) {
        return isDefined() ? value : elseValue;
    }

    public T orElseGet(Supplier supplier) {
        return isDefined() ? value : supplier.get();
    }

    @Deprecated
    public @Nullable T getOrNull() {
        return value;
    }

    public @Nullable T orNull() {
        return value;
    }

    public T get() throws NoSuchElementException {
        if (isEmpty()) {
            throw new NoSuchElementException("Value is not defined");
        }

        return value;
    }

    public boolean isPresent() {
        return isDefined();
    }

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

    public boolean isEmpty() {
        return value == null;
    }

    public  PandaStream toStream(Function> function) {
        return isDefined() ? PandaStream.of(function.apply(value)) : PandaStream.empty();
    }

    public PandaStream toStream() {
        return PandaStream.of(toJavaStream());
    }

    public Stream toJavaStream() {
        return isDefined() ? Stream.of(value) : Stream.empty();
    }

    public  Result toResult(E orElse) {
        return toResult(() -> orElse);
    }

    public  Result toResult(Supplier orElse) {
        return isDefined() ? Result.ok(get()) : Result.error(orElse.get());
    }

    public Optional toOptional() {
        return Optional.ofNullable(value);
    }

}