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

almost.functional.Predicates Maven / Gradle / Ivy

Go to download

Almost Functional is very low impact functional classes inspired by guava, jdk 1.8 and various others.

There is a newer version: 1.8
Show newest version
/*
 * Copyright (c) 2013-2014, [email protected]
 *
 * Permission to use, copy, modify, and/or distribute this software for any purpose with or
 * without fee is hereby granted, provided that the above copyright notice and this permission
 * notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
 * OR PERFORMANCE OF THIS SOFTWARE.
 */

package almost.functional;


import almost.functional.utils.Iterables;

/**
 * Utility operations on predicates.
 */
public class Predicates {
	private Predicates(){}

	/**
	 * Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
	 * @param targetRef the object reference with which to compare for equality, which may be null
	 * @param   the type of arguments to the predicate
	 * @return a predicate that tests if two arguments are equal according to Objects.equals(Object, Object)
	 */
	public static  Predicate isEqual(final T targetRef) {
		return new Predicate() {
			@Override
			public boolean test(T t) {
				return t.equals(targetRef);
			}
		};
	}

	/**
	 * Returns a predicate that that tests if an iterable contains an argument.
	 * @param iterable the iterable, may not be null
	 * @param  the type of the argument to the predicate
	 * @return a predicate that that tests if an iterable contains an argument
	 */
	public static  Predicate contains(final Iterable iterable) {
		return new Predicate() {
			@Override
			public boolean test(T t) {
				return Iterables.contains(iterable, t);
			}
		};
	}

    /**
     * Negate an existing predicate's test result.
     * @param predicate An existing predicate.
     * @param  type of the predicate.
     * @return new predicate.
     * @since 1.5
     */
    public static  Predicate negate(final Predicate predicate) {
        return new Predicate() {
            @Override
            public boolean test(T t) {
                return !predicate.test(t);
            }
        };
    }

    /**
     * Create a predicate which is a logical and of two existing predicate.
     * @param first first predicate.
     * @param second second predicate.
     * @param  type of the predicates
     * @return resultant predicate
     * @since 1.5
     */
    public static  Predicate and(final Predicate first, final Predicate second) {
        return new Predicate() {
            @Override
            public boolean test(T t) {
                return first.test(t) && second.test(t);
            }
        };
    }

    /**
     * Create a predicate which is a logical or of two existing predicate.
     * @param first first predicate.
     * @param second second predicate.
     * @param  type of the predicates
     * @return resultant predicate
     * @since 1.5
     */
    public static  Predicate or(final Predicate first, final Predicate second) {
        return new Predicate() {
            @Override
            public boolean test(T t) {
                return first.test(t) || second.test(t);
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy