/** * Tests whether or not given set of predicates consists only of one predicate returned from * {@link #alwaysTrue()} method. * * @param p Predicate to check. * @return {@code true} if given contains only {@code ALWAYS_TRUE} predicate. */ public static boolean isAlwaysTrue(@Nullable IgnitePredicate[] p) { return p != null && p.length == 1 && isAlwaysTrue(p[0]); }
/** * Gets size of the given collection with provided optional predicates. * * @param c Collection to size. * @param p Optional predicates that filters out elements from count. * @param <T> Type of the iterator. * @return Number of elements in the collection for which all given predicates * evaluates to {@code true}. If no predicates is provided - all elements are counted. */ public static <T> int size(@Nullable Collection<? extends T> c, @Nullable IgnitePredicate<? super T>... p) { return c == null || c.isEmpty() ? 0 : isEmpty(p) || isAlwaysTrue(p) ? c.size() : size(c.iterator(), p); }
/** * Creates write-through light-weight view on given collection with provided predicates. Resulting * collection will only "have" elements for which all provided predicates, if any, evaluate * to {@code true}. Note that only wrapping collection will be created and no duplication of * data will occur. Also note that if array of given predicates is not empty then method * {@code size()} uses full iteration through the collection. * * @param c Input collection that serves as a base for the view. * @param p Optional predicates. If predicates are not provided - all elements will be in the view. * @param <T> Type of the collection. * @return Light-weight view on given collection with provided predicate. */ @SafeVarargs public static <T> Collection<T> view(@Nullable final Collection<T> c, @Nullable final IgnitePredicate<? super T>... p) { if (isEmpty(c) || isAlwaysFalse(p)) return Collections.emptyList(); return isEmpty(p) || isAlwaysTrue(p) ? c : new PredicateCollectionView<>(c, p); }
/** * Creates light-weight view on given map with provided predicates. Resulting map will * only "have" keys for which all provided predicates, if any, evaluates to {@code true}. * Note that only wrapping map will be created and no duplication of data will occur. * Also note that if array of given predicates is not empty then method {@code size()} * uses full iteration through the entry set. * * @param m Input map that serves as a base for the view. * @param p Optional predicates. If predicates are not provided - all will be in the view. * @param <K> Type of the key. * @param <V> Type of the value. * @return Light-weight view on given map with provided predicate. */ public static <K0, K extends K0, V0, V extends V0> Map<K, V> view(@Nullable final Map<K, V> m, @Nullable final IgnitePredicate<? super K>... p) { if (isEmpty(m) || isAlwaysFalse(p)) return Collections.emptyMap(); return isEmpty(p) || isAlwaysTrue(p) ? m : new PredicateMapView<>(m, p); }
/** * 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. */ @SafeVarargs public static <T> IgnitePredicate<T> not(@Nullable final IgnitePredicate<? super T>... p) { return isAlwaysFalse(p) ? F.<T>alwaysTrue() : isAlwaysTrue(p) ? F.<T>alwaysFalse() : new IsNotAllPredicate<>(p); }
res = U.newHashMap(m.size()); if (!isEmpty(p) && !isAlwaysTrue(p)) for (Map.Entry<K, V> e : m.entrySet()) if (!F.isAll(e, p))
/** * Finds, transforms and returns first element in given collection for which any of * the provided predicates evaluates to {@code true}. * * @param c Input collection. * @param dfltVal Default value to return when no element is found. * @param f Transforming closure. * @param p Optional set of finder predicates. * @param <V> Type of the collection elements. * @return First element in given collection for which predicate evaluates to * {@code true} - or {@code null} if such element cannot be found. */ @Deprecated public static <V, Y> Y find(Iterable<? extends V> c, @Nullable Y dfltVal, IgniteClosure<? super V, Y> f, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c", f, "f"); if (isAlwaysTrue(p) && c.iterator().hasNext()) return f.apply(c.iterator().next()); if (!isEmpty(p) && !isAlwaysFalse(p)) { for (V v : c) { if (isAny(v, p)) return f.apply(v); } } return dfltVal; }
/** * Applies all given predicates to all elements in given input collection and returns * {@code true} if all predicates evaluate to {@code true} for at least one element. Returns * {@code false} otherwise. Processing will short-circuit after first element evaluates to * {@code true} for all predicates. * * @param c Input collection. * @param p Optional set of checking predicates. If none provided - {@code true} is returned. * @param <V> Type of the collection element. * @return Returns {@code true} if all given predicates evaluate to {@code true} for * at least one element. Returns {@code false} otherwise. */ @Deprecated public static <V> boolean forAny(Iterable<? extends V> c, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c"); if (!c.iterator().hasNext()) return false; else if (isEmpty(p)) return true; else if (isAlwaysFalse(p)) return false; else if (isAlwaysTrue(p)) return true; else { for (V v : c) if (isAll(v, p)) return true; return false; } }
res = new LinkedList<>(); if (!isEmpty(p) && !isAlwaysTrue(p)) for (T t : c) if (!isAll(t, p))
/** * Applies given predicates to all entries in given input map and returns {@code true} * if all of them evaluates to {@code true} for all entries. Returns {@code false} otherwise. * * @param m Input map. * @param p Optional set of checking predicate. * @param <K> Type of the map keys. * @param <V> Type of the map values. * @return Returns {@code true} if all given predicate evaluates to {@code true} for all * entries. Returns {@code false} otherwise. */ @Deprecated public static <K1, K extends K1, V1, V extends V1> boolean forAll(Map<K, V> m, @Nullable IgnitePredicate<? super Map.Entry<K, V>>... p) { A.notNull(m, "m"); if (isAlwaysFalse(p)) return false; else if (isAlwaysTrue(p)) return true; else if (!isEmpty(p)) for (Map.Entry<K, V> e : m.entrySet()) if (!isAll(e, p)) return false; return true; }
/** * Applies all given predicates to all elements in given input collection and returns * {@code true} if all of them evaluate to {@code true} for all elements. Returns * {@code false} otherwise. * * @param c Input collection. * @param p Optional set of checking predicates. If none provided - {@code true} is returned. * @param <V> Type of the collection element. * @return Returns {@code true} if all given predicates evaluate to {@code true} for * all elements. Returns {@code false} otherwise. */ @Deprecated public static <V> boolean forAll(Iterable<? extends V> c, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c"); if (isAlwaysFalse(p)) return false; else if (isAlwaysTrue(p)) return true; else if (!isEmpty(p)) { for (V v : c) { if (!isAll(v, p)) return false; } } return true; }
/** * Checks for existence of the element in input collection for which all provided predicates * evaluate to {@code true}. * * @param c Input collection. * @param p Optional set of checking predicates. * @param <V> Type of the collection elements. * @return {@code true} if input collection contains element for which all the provided * predicates evaluates to {@code true} - otherwise returns {@code false}. */ public static <V> boolean exist(Iterable<? extends V> c, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c"); if (isAlwaysFalse(p)) return false; else if (isAlwaysTrue(p)) return true; else if (isEmpty(p)) return true; else for (V v : c) if (isAll(v, p)) return true; return false; }
return F.alwaysFalse(); if (isAlwaysTrue(ps)) return F.alwaysTrue();
/** * Tests whether or not given set of predicates consists only of one predicate returned from * {@link #alwaysTrue()} method. * * @param p Predicate to check. * @return {@code true} if given contains only {@code ALWAYS_TRUE} predicate. */ public static boolean isAlwaysTrue(@Nullable IgnitePredicate[] p) { return p != null && p.length == 1 && isAlwaysTrue(p[0]); }
/** * Gets size of the given collection with provided optional predicates. * * @param c Collection to size. * @param p Optional predicates that filters out elements from count. * @param <T> Type of the iterator. * @return Number of elements in the collection for which all given predicates * evaluates to {@code true}. If no predicates is provided - all elements are counted. */ public static <T> int size(@Nullable Collection<? extends T> c, @Nullable IgnitePredicate<? super T>... p) { return c == null || c.isEmpty() ? 0 : isEmpty(p) || isAlwaysTrue(p) ? c.size() : size(c.iterator(), p); }
/** * Creates write-through light-weight view on given collection with provided predicates. Resulting * collection will only "have" elements for which all provided predicates, if any, evaluate * to {@code true}. Note that only wrapping collection will be created and no duplication of * data will occur. Also note that if array of given predicates is not empty then method * {@code size()} uses full iteration through the collection. * * @param c Input collection that serves as a base for the view. * @param p Optional predicates. If predicates are not provided - all elements will be in the view. * @param <T> Type of the collection. * @return Light-weight view on given collection with provided predicate. */ @SafeVarargs public static <T> Collection<T> view(@Nullable final Collection<T> c, @Nullable final IgnitePredicate<? super T>... p) { if (isEmpty(c) || isAlwaysFalse(p)) return Collections.emptyList(); return isEmpty(p) || isAlwaysTrue(p) ? c : new PredicateCollectionView<>(c, p); }
/** * 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. */ @SafeVarargs public static <T> IgnitePredicate<T> not(@Nullable final IgnitePredicate<? super T>... p) { return isAlwaysFalse(p) ? F.<T>alwaysTrue() : isAlwaysTrue(p) ? F.<T>alwaysFalse() : new IsNotAllPredicate<>(p); }
/** * Creates light-weight view on given map with provided predicates. Resulting map will * only "have" keys for which all provided predicates, if any, evaluates to {@code true}. * Note that only wrapping map will be created and no duplication of data will occur. * Also note that if array of given predicates is not empty then method {@code size()} * uses full iteration through the entry set. * * @param m Input map that serves as a base for the view. * @param p Optional predicates. If predicates are not provided - all will be in the view. * @param <K> Type of the key. * @param <V> Type of the value. * @return Light-weight view on given map with provided predicate. */ public static <K0, K extends K0, V0, V extends V0> Map<K, V> view(@Nullable final Map<K, V> m, @Nullable final IgnitePredicate<? super K>... p) { if (isEmpty(m) || isAlwaysFalse(p)) return Collections.emptyMap(); return isEmpty(p) || isAlwaysTrue(p) ? m : new PredicateMapView<>(m, p); }
/** * Finds, transforms and returns first element in given collection for which any of * the provided predicates evaluates to {@code true}. * * @param c Input collection. * @param dfltVal Default value to return when no element is found. * @param f Transforming closure. * @param p Optional set of finder predicates. * @param <V> Type of the collection elements. * @return First element in given collection for which predicate evaluates to * {@code true} - or {@code null} if such element cannot be found. */ @Deprecated public static <V, Y> Y find(Iterable<? extends V> c, @Nullable Y dfltVal, IgniteClosure<? super V, Y> f, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c", f, "f"); if (isAlwaysTrue(p) && c.iterator().hasNext()) return f.apply(c.iterator().next()); if (!isEmpty(p) && !isAlwaysFalse(p)) { for (V v : c) { if (isAny(v, p)) return f.apply(v); } } return dfltVal; }
/** * Applies given predicates to all entries in given input map and returns {@code true} * if all of them evaluates to {@code true} for all entries. Returns {@code false} otherwise. * * @param m Input map. * @param p Optional set of checking predicate. * @param <K> Type of the map keys. * @param <V> Type of the map values. * @return Returns {@code true} if all given predicate evaluates to {@code true} for all * entries. Returns {@code false} otherwise. */ @Deprecated public static <K1, K extends K1, V1, V extends V1> boolean forAll(Map<K, V> m, @Nullable IgnitePredicate<? super Map.Entry<K, V>>... p) { A.notNull(m, "m"); if (isAlwaysFalse(p)) return false; else if (isAlwaysTrue(p)) return true; else if (!isEmpty(p)) for (Map.Entry<K, V> e : m.entrySet()) if (!isAll(e, p)) return false; return true; }