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

net.kemitix.mon.maybe.Nothing Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Paul Campbell
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package net.kemitix.mon.maybe;

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;

/**
 * A Maybe where no value is present.
 *
 * @param  the type of the missing content
 * @author Paul Campbell ([email protected])
 */
@SuppressWarnings("methodcount")
final class Nothing implements Maybe {

    static final Maybe INSTANCE = new Nothing<>();

    @Override
    public boolean isJust() {
        return false;
    }

    @Override
    public boolean isNothing() {
        return true;
    }

    @Override
    public  Maybe flatMap(final Function> f) {
        return Maybe.nothing();
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Maybe map(final Function f) {
        return (Maybe) INSTANCE;
    }

    @Override
    public T orElseGet(final Supplier supplier) {
        return supplier.get();
    }

    @Override
    public T orElse(final T otherValue) {
        return otherValue;
    }

    @Override
    public Maybe filter(final Predicate predicate) {
        return this;
    }

    @Override
    public Optional toOptional() {
        return Optional.empty();
    }

    @Override
    public  T orElseThrow(final Supplier e) throws X {
        throw e.get();
    }

    @Override
    public Maybe peek(final Consumer consumer) {
        return this;
    }

    @Override
    public void ifNothing(final Runnable runnable) {
        runnable.run();
    }

    @Override
    public void match(final Consumer justMatcher, final Runnable nothingMatcher) {
        nothingMatcher.run();
    }

    @Override
    public  R matchValue(
            final Function justMatcher,
            final Supplier nothingMatcher
    ) {
        return nothingMatcher.get();
    }

    @Override
    public Maybe or(final Supplier> alternative) {
        return alternative.get();
    }

    @Override
    public Stream stream() {
        return Stream.empty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy