/** * Gets predicate which returns {@code true} if entry's key is contained in given collection. * Note that if collection of provided keys is empty this method returns predicate that * evaluates to {@code false} when applying. * * @param keys Keys to check. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns {@code true} if entry's key is contained in given collection. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheHasKeys( @Nullable final Collection<? extends K> keys) { return isEmpty(keys) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { return keys != null && keys.contains(e.getKey()); } }; }
/** * Gets predicate which returns {@code true} if cache entry matches any given key-value pair. * Note that if provided map is empty this method returns predicate that evaluates to * {@code false} when applying. * * @param map Key-value paris to check for containment. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns {@code true} if cache entry matches any given key-value pair. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheContainsPeek( @Nullable final Map<K, V> map) { return isEmpty(map) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { assert map != null; return eq(e.peek(), map.get(e.getKey())); } }; }
/** * Gets event predicate that returns {@code true} only if event type is one of the given. * Note that if array of provided types is {@code null} or empty this method returns * predicate that evaluates to {@code false} when applying. * * @param types Event types. * @return Event predicate. */ public static GridPredicate<GridEvent> eventType(@Nullable final int... types) { return isEmpty(types) ? F.<GridEvent>alwaysFalse() : new GridPredicate<GridEvent>() { @Override public boolean apply(GridEvent e) { assert e != null; assert types != null; for (int t : types) { if (e.type() == t) return true; } return false; } }; }
/** * Gets event predicate that returns {@code true} only if event was produced on one of * given nodes (specified by ids). Note that if array of provided node ids is {@code null} * or empty this method returns predicate that evaluates to {@code false} when applying. * * @param nodeIds Node ids. * @return Event predicate. */ public static GridPredicate<GridEvent> eventNodeId(@Nullable final UUID... nodeIds) { return isEmpty(nodeIds) ? F.<GridEvent>alwaysFalse() : new GridPredicate<GridEvent>() { // Don't set peer deploy aware as UUID is loaded by // system class loader. @Override public boolean apply(GridEvent e) { assert e != null; return F.isAll(e.node().id(), in(nodeIds)); } }; }
/** * Gets predicate which returns {@code true} if cache entry matches any given key-value pair. * Note that if provided map is empty this method returns predicate that evaluates to * {@code false} when applying. * * @param map Key-value paris to check for containment. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns {@code true} if cache contains all given key-value pairs. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheContainsGet(@Nullable final Map<K, V> map) { return isEmpty(map) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { assert map != null; try { return eq(e.get(), map.get(e.getKey())); } catch (GridException ex) { throw wrap(ex); } } }; }
/** * Gets predicate which returns true if {@link GridCacheEntry#get()} * method returns value that is contained in given collection. Note that if collection * of provided values is empty this method returns predicate that evaluates to {@code false} * when applying. * * @param vals Values to check in predicate. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns true if {@link GridCacheEntry#get()} methods returns * value that is contained in given collection. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheContainsGet( @Nullable final Collection<? extends V> vals) { return isEmpty(vals) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { try { V v = e.get(); assert vals != null; return v != null && vals.contains(v); } catch (GridException e1) { throw wrap(e1); } } }; }
/** * Gets predicate which returns true if {@link GridCacheEntry#peek()} methods returns * value that is contained in given collection. Note that if collection of provided values * is empty this method returns predicate that evaluates to {@code false} * when applying. * * @param vals Values to check in predicate. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns true if {@link GridCacheEntry#peek()} methods returns * value that is contained in given collection. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheContainsPeek( @Nullable final Collection<? extends V> vals) { return isEmpty(vals) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { V v = e.peek(); assert vals != null; return v != null && vals.contains(v); } @Override public String toString() { return "[Cache entry peek-values predicate [" + vals + "]]"; } }; }
/** * Gets event predicate that returns {@code true} only if node that produced the event * is one of the given. Note that if array of provided nodes is {@code null} or empty * this method returns predicate that evaluates to {@code false} when applying. * * @param nodes Nodes. * @return Event predicate. */ public static GridPredicate<GridEvent> eventNode(@Nullable final Collection<? extends GridNode> nodes) { return isEmpty(nodes) ? F.<GridEvent>alwaysFalse() : new GridPredicate<GridEvent>() { @Override public boolean apply(GridEvent e) { assert e != null; return !forAll(nodes, not(nodeForNodeId(e.node().id()))); } }; }
/** * Gets event predicate that returns {@code true} only if event id is one of the given. * Note that if array of provided ids is empty this method returns predicate that * evaluates to {@code false} when applying. * * @param ids Event ids. * @return Event predicate. */ public static GridPredicate<GridEvent> eventId(@Nullable final GridUuid... ids) { return isEmpty(ids) ? F.<GridEvent>alwaysFalse() : new GridPredicate<GridEvent>() { // Don't set peer deploy aware as UUID is loaded by // system class loader. @Override public boolean apply(GridEvent e) { assert e != null; return F.isAll(e.id(), in(ids)); } }; }
/** * Gets predicate which returns {@code true} if cache entry matches any given key-value pair. * Both, key and value will be checked for containment. Value will be retrieved using * {@link GridCacheEntry#peek()} method. Note that if collection * of provided entries is empty this method returns predicate that evaluates to {@code false} * when applying. * * @param entries Key-value paris to check for containment. * @param <K> Cache key type. * @param <V> Cache value type. * @return Predicate which returns {@code true} if cache entry matches any given key-value pair. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheContainsEntriesPeek( @Nullable final Collection<? extends Map.Entry<K, V>> entries) { return isEmpty(entries) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { K k = e.getKey(); V v = e.peek(); assert entries != null; for (Map.Entry<K, V> entry : entries) { if (eq(k, entry.getKey()) && eq(v, entry.getValue())) return true; } return false; } }; }
/** * Negates given predicates. * <p> * Gets predicate (not peer-deployable) that evaluates to {@code true} if any of given predicates * evaluates to {@code false}. If all predicates evaluate to {@code true} the * result predicate will evaluate to {@code false}. * * @param p Predicate to negate. * @param <T> Type of the free variable, i.e. the element the predicate is called on. * @return Negated predicate (not peer-deployable). */ public static <T> GridPredicate<T> not(@Nullable final GridPredicate<? super T>... p) { return F.isAlwaysFalse(p) ? F.<T>alwaysTrue() : F.isAlwaysTrue(p) ? F.<T>alwaysFalse() : new P1<T>() { @Override public boolean apply(T t) { return !F.isAll(t, p); } }; }
/** * Converts value filter to entry filter using {@link GridCacheEntry#get()} to get value. * Note that if array of provided filters is {@code null} or empty this method returns * predicate that evaluates to {@code true} when applying. * * @param ps Value filter(s) to convert. * @return Entry filter. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheValuesGet( @Nullable final GridPredicate<? super V>... ps) { return isEmpty(ps) || isAlwaysTrue(ps) ? F.<GridCacheEntry<K, V>>alwaysTrue() : isAlwaysFalse(ps) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { try { V v = e.get(); return v != null && F.isAll(v, ps); } catch (GridException ex) { throw wrap(ex); } } }; }
return isEmpty(entries) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) {
/** * Negates given predicates. * <p> * Gets predicate that evaluates to {@code true} if any of given predicates * evaluates to {@code false}. If all predicates evaluate to {@code true} the * result predicate will evaluate to {@code false}. * * @param p Predicate to negate. * @param <T> Type of the free variable, i.e. the element the predicate is called on. * @return Negated predicate. */ public static <T> GridPredicate<T> not(@Nullable final GridPredicate<? super T>... p) { return isAlwaysFalse(p) ? F.<T>alwaysTrue() : isAlwaysTrue(p) ? F.<T>alwaysFalse() : new P1<T>() { @Override public boolean apply(T t) { return !isAll(t, p); } }; }
/** * Gets event predicate that returns {@code true} only if node that produced the event * satisfies all given predicates. Note that if array of provided node predicates is * {@code null} or empty this method returns predicate that evaluates to {@code true} * assuming that any event produced by any node is ok. * * @param gridName Grid name. * @param p Node predicates. * @return Event predicate. */ public static GridPredicate<GridEvent> eventNode(@Nullable final String gridName, @Nullable final GridPredicate<GridNode>... p) { return isEmpty(p) || isAlwaysTrue(p) ? F.<GridEvent>alwaysTrue() : isAlwaysFalse(p) ? F.<GridEvent>alwaysFalse() : new GridPredicate<GridEvent>() { @Override public boolean apply(GridEvent e) { assert e != null; try { GridNode node = G.grid(gridName).node(e.node().id()); return isAll(node, p); } catch (IllegalStateException ex) { throw new GridRuntimeException("Invalid grid name: " + gridName, ex); } } }; }
/** * Converts value filter to entry filter using {@link GridCacheEntry#peek()} * to get value. Note that if array of provided filters is {@code null} or empty this method returns * predicate that evaluates to {@code true} when applying. * * @param ps Value filter(s) to convert. * @return Entry filter. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheValuesPeek( @Nullable final GridPredicate<? super V>... ps) { return isEmpty(ps) || isAlwaysTrue(ps) ? F.<GridCacheEntry<K, V>>alwaysTrue() : isAlwaysFalse(ps) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { V v = e.peek(); return v != null && F.isAll(v, ps); } }; }
return F.alwaysFalse();
/** * Converts key filter to entry filter using {@link GridCacheEntry#getKey()} * to get value. Note that if array of provided filters is {@code null} or empty this * method returns predicate that evaluates to {@code true} when applying. * * @param ps Key filter(s) to convert. * @return Entry filter. */ public static <K, V> GridPredicate<GridCacheEntry<K, V>> cacheKeys( @Nullable final GridPredicate<? super K>... ps) { return isEmpty(ps) || isAlwaysTrue(ps) ? F.<GridCacheEntry<K, V>>alwaysTrue() : isAlwaysFalse(ps) ? F.<GridCacheEntry<K, V>>alwaysFalse() : new GridPredicate<GridCacheEntry<K, V>>() { @Override public boolean apply(GridCacheEntry<K, V> e) { return F.isAll(e.getKey(), ps); } }; }
public static <T> GridPredicate<T> or(@Nullable final Collection<? extends GridPredicate<? super T>> ps) { if (isEmpty(ps)) return F.alwaysFalse(); else { assert ps != null;
/** * Gets a predicate that is composed of given predicate and closure. For every {@code x} * it returns predicate {@code p(f(x))}. Note that predicate and closure must be loaded * by the same class loader. * * @param p Predicate. * @param f Closure. * @param <X> Type of the free variable for the closure. * @param <Y> Type of the closure's return value. * @return Predicate that is composed of given predicate and closure. */ @SuppressWarnings({"JavaDoc"}) public static <X, Y> GridPredicate<X> compose(final GridPredicate<? super Y> p, final GridClosure<? super X, ? extends Y> f) { A.notNull(p, "p", f, "f"); return isAlwaysFalse(p) ? F.<X>alwaysFalse() : isAlwaysTrue(p) ? F.<X>alwaysTrue() : new P1<X>() { @Override public boolean apply(X x) { return p.apply(f.apply(x)); } }; }