/** * Returns a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true}. The components are * evaluated in order, and evaluation will end if a predicate result * fails to match the first predicate's result. * * @param <T> the type of values evaluated by the predicates. * @param first initial component predicate to be evaluated. * @param second additional component predicate to be evaluated. * @return a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true} */ public static <T, P extends Predicate<? super T>> Predicate<T> xor(final Predicate<T> first, final P second) { if(null != first && first == second) { return alwaysFalse(); } Objects.requireNonNull(first); Objects.requireNonNull(second); return new Predicate<T>() { @Override public boolean test(final T t) { //noinspection ConstantConditions return first.test(t) ^ second.test(t); } }; }
/** * Returns a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true}. The components are * evaluated in order, and evaluation will end if a predicate result * fails to match the first predicate's result. * * @param <T> the type of values evaluated by the predicates. * @param first initial component predicate to be evaluated. * @param second additional component predicate to be evaluated. * @return a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true} */ public static <T, P extends Predicate<? super T>> Predicate<T> xor(final Predicate<T> first, final P second) { if(null != first && first == second) { return alwaysFalse(); } Objects.requireNonNull(first); Objects.requireNonNull(second); return new Predicate<T>() { @Override public boolean test(final T t) { //noinspection ConstantConditions return first.test(t) ^ second.test(t); } }; }
/** * Returns a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true}. The components are * evaluated in order, and evaluation will end if a predicate result * fails to match the first predicate's result. * * @param <T> the type of values evaluated by the predicates. * @param first initial component predicate to be evaluated. * @param second additional component predicate to be evaluated. * @return a predicate that evaluates to {@code true} if all or none of the * component predicates evaluate to {@code true} */ public static <T, P extends Predicate<? super T>> Predicate<T> xor(final Predicate<T> first, final P second) { if(null != first && first == second) { return alwaysFalse(); } Objects.requireNonNull(first); Objects.requireNonNull(second); return new Predicate<T>() { @Override public boolean test(final T t) { //noinspection ConstantConditions return first.test(t) ^ second.test(t); } }; }