Search in sources :

Example 11 with P1

use of org.apache.ignite.internal.util.typedef.P1 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)

Example 12 with P1

use of org.apache.ignite.internal.util.typedef.P1 in project ignite by apache.

the class GridEventConsumeSelfTest method testNodeJoin.

/**
 * @throws Exception If failed.
 */
public void testNodeJoin() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT + 1);
    UUID consumeId = grid(0).events().remoteListen(notSerializableProxy(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }), notSerializableProxy(new P1<Event>() {

        @Override
        public boolean apply(Event evt) {
            return evt.type() == EVT_JOB_STARTED;
        }
    }), EVT_JOB_STARTED, EVT_JOB_FINISHED);
    try {
        assertNotNull(consumeId);
        include = true;
        startGrid("anotherGrid");
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT + 1, nodeIds.size());
        assertEquals(GRID_CNT + 1, cnt.get());
    } finally {
        stopGrid("anotherGrid");
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) P2(org.apache.ignite.internal.util.typedef.P2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Aggregations

P1 (org.apache.ignite.internal.util.typedef.P1)12 UUID (java.util.UUID)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 Event (org.apache.ignite.events.Event)4 Ignite (org.apache.ignite.Ignite)3 LinkedList (java.util.LinkedList)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 JobEvent (org.apache.ignite.events.JobEvent)2 GridNodePredicate (org.apache.ignite.internal.util.lang.GridNodePredicate)2 P2 (org.apache.ignite.internal.util.typedef.P2)2 IgniteFuture (org.apache.ignite.lang.IgniteFuture)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 CacheRebalancingEvent (org.apache.ignite.events.CacheRebalancingEvent)1