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

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

There is a newer version: 3.2.0
Show newest version
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 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 lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

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;

/**
 * A Maybe where a value is present.
 *
 * @param  the type of the content
 * @author Paul Campbell ([email protected])
 */
@SuppressWarnings("methodcount")
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
final class Just implements Maybe {

    private final T value;

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

    @Override
    public  Maybe map(final Function f) {
        return new Just<>(f.apply(value));
    }

    @Override
    public boolean equals(final Object other) {
        return other instanceof Just && Objects.equals(value, ((Just) other).value);
    }

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

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

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

    @Override
    public Maybe filter(final Predicate predicate) {
        if (predicate.test(value)) {
            return this;
        }
        return Maybe.nothing();
    }

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

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

    @Override
    public void orElseThrow(final Supplier e) {
        // do not throw
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy