Search in sources :

Example 1 with GridNodePredicate

use of org.apache.ignite.internal.util.lang.GridNodePredicate in project ignite by apache.

the class F0 method and.

/**
     * Get a predicate (non peer-deployable) that evaluates to {@code true} if each of its component predicates
     * evaluates to {@code true}. The components are evaluated in order they are supplied.
     * Evaluation will be stopped as soon as first predicate evaluates to {@code false}.
     * Passed in predicates are NOT copied. If no predicates are passed in the returned
     * predicate will always evaluate to {@code false}.
     *
     * @param p1 Passed in predicates.
     * @param p2 Passed in predicates.
     * @param <T> Type of the free variable, i.e. the element the predicate is called on.
     * @return Predicate that evaluates to {@code true} if each of its component predicates
     *      evaluates to {@code true}.
     */
@SuppressWarnings({ "unchecked", "ConfusingArgumentToVarargsMethod" })
public static <T> IgnitePredicate<T> and(@Nullable final IgnitePredicate<? super T>[] p1, @Nullable final IgnitePredicate<? super T>... p2) {
    if (F.isAlwaysFalse(p1) || F.isAlwaysFalse(p2))
        return F.alwaysFalse();
    if (F.isAlwaysTrue(p1) && F.isAlwaysTrue(p2))
        return F.alwaysTrue();
    final boolean e1 = F.isEmpty(p1);
    final boolean e2 = F.isEmpty(p2);
    if (e1 && e2)
        return F.alwaysTrue();
    if (e1) {
        if (p2.length == 1)
            return (IgnitePredicate<T>) p2[0];
    }
    if (!e1 && e2) {
        if (p1.length == 1)
            return (IgnitePredicate<T>) p1[0];
    }
    if ((e1 || isAllNodePredicates(p1)) && (e2 || isAllNodePredicates(p2))) {
        Set<UUID> ids = new GridLeanSet<>();
        if (!e1) {
            for (IgnitePredicate<? super T> p : p1) ids.addAll(((GridNodePredicate) p).nodeIds());
        }
        if (!e2) {
            for (IgnitePredicate<? super T> p : p2) ids.addAll(((GridNodePredicate) p).nodeIds());
        }
        // T must be <T extends ClusterNode>.
        return (IgnitePredicate<T>) new GridNodePredicate(ids);
    } else {
        return new P1<T>() {

            @Override
            public boolean apply(T t) {
                if (!e1) {
                    for (IgnitePredicate<? super T> p : p1) if (p != null && !p.apply(t))
                        return false;
                }
                if (!e2) {
                    for (IgnitePredicate<? super T> p : p2) if (p != null && !p.apply(t))
                        return false;
                }
                return true;
            }
        };
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) GridNodePredicate(org.apache.ignite.internal.util.lang.GridNodePredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) UUID(java.util.UUID)

Example 2 with GridNodePredicate

use of org.apache.ignite.internal.util.lang.GridNodePredicate in project ignite by apache.

the class F0 method and.

/**
     * Get a predicate (not peer-deployable) that evaluates to {@code true} if each of its component predicates
     * evaluates to {@code true}. The components are evaluated in order they are supplied.
     * Evaluation will be stopped as soon as first predicate evaluates to {@code false}.
     * Passed in predicates are NOT copied. If no predicates are passed in the returned
     * predicate will always evaluate to {@code false}.
     *
     * @param ps Passed in predicate. If none provided - always-{@code false} predicate is
     *      returned.
     * @param <T> Type of the free variable, i.e. the element the predicate is called on.
     * @return Predicate that evaluates to {@code true} if each of its component predicates
     *      evaluates to {@code true}.
     */
@SuppressWarnings({ "unchecked", "ConfusingArgumentToVarargsMethod", "ConstantConditions" })
public static <T> IgnitePredicate<T> and(@Nullable final IgnitePredicate<? super T> p, @Nullable final IgnitePredicate<? super T>... ps) {
    if (p == null && F.isEmptyOrNulls(ps))
        return F.alwaysTrue();
    if (F.isAlwaysFalse(p) && F.isAlwaysFalse(ps))
        return F.alwaysFalse();
    if (F.isAlwaysTrue(p) && F.isAlwaysTrue(ps))
        return F.alwaysTrue();
    if (isAllNodePredicates(p) && isAllNodePredicates(ps)) {
        assert ps != null;
        Set<UUID> ids = new GridLeanSet<>();
        for (IgnitePredicate<? super T> p0 : ps) {
            Collection<UUID> list = ((GridNodePredicate) p0).nodeIds();
            if (ids.isEmpty())
                ids.addAll(list);
            else
                ids.retainAll(list);
        }
        Collection<UUID> list = ((GridNodePredicate) p).nodeIds();
        if (ids.isEmpty())
            ids.addAll(list);
        else
            ids.retainAll(list);
        // T must be <T extends ClusterNode>.
        return (IgnitePredicate<T>) new GridNodePredicate(ids);
    } else {
        return new P1<T>() {

            @Override
            public boolean apply(T t) {
                assert ps != null;
                if (p != null && !p.apply(t))
                    return false;
                for (IgnitePredicate<? super T> p : ps) if (p != null && !p.apply(t))
                    return false;
                return true;
            }
        };
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) GridNodePredicate(org.apache.ignite.internal.util.lang.GridNodePredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 GridNodePredicate (org.apache.ignite.internal.util.lang.GridNodePredicate)2 P1 (org.apache.ignite.internal.util.typedef.P1)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2