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

com.github.puddingspudding.IntegerPredicates Maven / Gradle / Ivy

Go to download

The intention of this project is to improve readability of equality and relational operators using predefined [Predicates](https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html).

The newest version!
/*
 * 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 com.github.puddingspudding;

import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Generic Integer predicates.
 */
public final class IntegerPredicates {

    public static Predicate isEqualTo(final int i1) {
        return i2 -> i1 == i2;
    }

    public static Predicate isNotEqualTo(final int i1) {
        return isEqualTo(i1).negate();
    }

    public static Predicate isBiggerThan(final int i1) {
        return i2 -> i1 < i2;
    }

    public static Predicate isSmallerThan(final int i1) {
        return i2 -> i1 > i2;
    }

    public static Predicate isEqualOrBiggerThan(final int i1) {
        return isSmallerThan(i1).negate();
    }

    public static Predicate isEqualOrSmallerThan(final int i1) {
        return isBiggerThan(i1).negate();
    }

    public static Function> isInRange(final int min) {
        return max -> isEqualOrBiggerThan(min).and(isEqualOrSmallerThan(max));
    }

    public static Predicate isInRange(final int min, final int max) {
        return isEqualOrSmallerThan(max).and(isEqualOrBiggerThan(min));
    }

    public static Predicate isNotInRange(final int min, final int max) {
        return isInRange(min, max).negate();
    }

    public static Function> isNotInRange(final int min) {
        return max -> isInRange(min).apply(max).negate();
    }

    public static final Predicate isNegative() {
        return isSmallerThan(0);
    }

    public static final Predicate isPositive() {
        return isBiggerThan(0);
    }

    public static final Predicate isEven() {
        return i -> i % 2 == 0;
    }

    public static final Predicate isOdd() {
        return isEven().negate();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy