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

eu.lunisolar.magma.basics.probing.Probe Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is part of "lunisolar-magma".
 *
 * (C) Copyright 2014-2019 Lunisolar (http://lunisolar.eu/).
 *
 * 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 eu.lunisolar.magma.basics.probing;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.*;

import static java.util.Objects.*;

/**
 * The point is to not hide the object that is probed, but to provide some _probing_ methods that enclose some simple conditions into a nice named method.
 */
@SuppressWarnings("unused")
public interface Probe {

    @Nullable T target();

    default @Nonnull T nonNullTarget() {
        return requireNonNull(target());
    }

    default boolean isNotNull() {
        return check(Objects::nonNull);
    }

    default boolean isNull() {
        return check(Objects::isNull);
    }

    // 

    default boolean check(Predicate predicate) {
        return predicate.test(target());
    }

    /**
     * BiPredicate allows to use non-capturing lambda when second argument is required.
     */
    default  boolean check(A argument, BiPredicate predicate) {
        return predicate.test(target(), argument);
    }

    default boolean checkOnlyWhen(Predicate predicateForWhen, Predicate predicateForCheck) {
        return check(predicateForWhen) && check(predicateForCheck);
    }

    default  boolean checkOnlyWhen(Predicate predicateForWhen, A argument, BiPredicate predicateForCheck) {

        return check(predicateForWhen) && check(argument, predicateForCheck);
    }

    default boolean checkWhenTargetNotNull(Predicate predicate) {
        return checkOnlyWhen(Objects::nonNull, predicate);
    }

    default  boolean checkWhenTargetNotNull(A argument, BiPredicate predicate) {
        return checkOnlyWhen(Objects::nonNull, argument, predicate);
    }

    default boolean checkWhenTargetNotNullAnd(Predicate predicateForWhen, Predicate predicateForCheck) {
        return checkWhenTargetNotNull(predicateForWhen) && check(predicateForCheck);
    }

    default  boolean checkWhenTargetNotNullAnd(Predicate predicateForWhen, A argument, BiPredicate predicateForCheck) {
        return checkWhenTargetNotNull(predicateForWhen) && check(argument, predicateForCheck);
    }

    class Base implements Probe {

        private final T target;

        public Base(T target) {
            this.target = target;
        }

        @Nullable @Override public T target() {
            return target;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy