/** * Tests whether or not given set of predicates consists only of one predicate returned from * {@link #alwaysFalse()} method. * * @param p Predicate to check. * @return {@code true} if given contains only {@code ALWAYS_FALSE} predicate. */ @Deprecated public static boolean isAlwaysFalse(@Nullable IgnitePredicate[] p) { return p != null && p.length == 1 && isAlwaysFalse(p[0]); }
/** * Gets size of the given iterator with provided optional predicates. Iterator * will be traversed to get the count. * * @param it Iterator to size. * @param p Optional predicates that filters out elements from count. * @param <T> Type of the iterator. * @return Number of elements in the iterator for which all given predicates * evaluates to {@code true}. If no predicates is provided - all elements are counted. */ public static <T> int size(@Nullable Iterator<? extends T> it, @Nullable IgnitePredicate<? super T>... p) { if (it == null) return 0; int n = 0; if (!isAlwaysFalse(p)) { while (it.hasNext()) { if (isAll(it.next(), p)) n++; } } return n; }
/** * Adds (copies) to given collection using provided predicates. Element is copied if all * predicates evaluate to {@code true}. * * @param to Collection to copy to. * @param from Collection to copy from. * @param p Optional set of predicates to use for filtration. * @param <T> Type of the free variable for the predicate and type of the collection elements. * @return Collection to copy to. */ @Deprecated public static <T> Collection<T> copy(Collection<T> to, Iterable<? extends T> from, @Nullable IgnitePredicate<? super T>... p) { A.notNull(to, "to", from, "from"); if (!isAlwaysFalse(p)) { for (T t : from) { if (isAll(t, p)) to.add(t); } } return to; }
/** * Creates a view on given list with provided transformer and predicates. * Resulting list will only "have" elements for which all provided predicates, if any, * evaluate to {@code true}. Note that a new collection will be created and data will * be copied. * * @param c Input list that serves as a base for the view. * @param trans Transforming closure from T1 to T2. * @param p Optional predicates. If predicates are not provided - all elements will be in the view. * @return View on given list with provided predicate. */ @Deprecated public static <T1, T2> List<T2> transformList(Collection<? extends T1> c, IgniteClosure<? super T1, T2> trans, @Nullable IgnitePredicate<? super T1>... p) { A.notNull(c, "c", trans, "trans"); if (isAlwaysFalse(p)) return Collections.emptyList(); return new ArrayList<>(transform(retain(c, true, p), trans)); }
/** * Creates read-only light-weight view on given collection with transformation and provided * predicates. Resulting collection will only "have" {@code transformed} elements for which * all provided predicate, if any, evaluates 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 trans Transformation closure. * @param p Optional predicated. If predicates are not provided - all elements will be in the view. * @param <T1> Type of the collection. * @return Light-weight view on given collection with provided predicate. */ @SafeVarargs public static <T1, T2> Collection<T2> viewReadOnly(@Nullable final Collection<? extends T1> c, final IgniteClosure<? super T1, T2> trans, @Nullable final IgnitePredicate<? super T1>... p) { A.notNull(trans, "trans"); if (isEmpty(c) || isAlwaysFalse(p)) return Collections.emptyList(); return new TransformCollectionView<>(c, trans, p); }
/** * Read-only view on map that supports transformation of values and key filtering. 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 trans Transformer for map value transformation. * @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 input map value. * @param <V1> Type of the output map value. * @return Light-weight view on given map with provided predicate and transformer. */ @Deprecated public static <K0, K extends K0, V0, V extends V0, V1> Map<K, V1> viewReadOnly(@Nullable final Map<K, V> m, final IgniteBiClosure<K, V, V1> trans, @Nullable final IgnitePredicate<? super K>... p) { A.notNull(trans, "trans"); if (isEmpty(m) || isAlwaysFalse(p)) return Collections.emptyMap(); return new TransformMapView2<>(m, trans, p); }
/** * Finds 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 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. */ @SafeVarargs @Nullable public static <V> V find(Iterable<? extends V> c, @Nullable V dfltVal, @Nullable IgnitePredicate<? super V>... p) { A.notNull(c, "c"); if (!isEmpty(p) && !isAlwaysFalse(p)) { for (V v : c) { if (isAny(v, p)) return v; } } return dfltVal; }
/** * Read-only view on map that supports transformation of values and key filtering. 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 trans Transformer for map value transformation. * @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 input map value. * @param <V1> Type of the output map value. * @return Light-weight view on given map with provided predicate and transformer. */ public static <K0, K extends K0, V0, V extends V0, V1> Map<K, V1> viewReadOnly(@Nullable final Map<K, V> m, final IgniteClosure<V, V1> trans, @Nullable final IgnitePredicate<? super K>... p) { A.notNull(trans, "trans"); if (isEmpty(m) || isAlwaysFalse(p)) return Collections.emptyMap(); return new TransformMapView<>(m, trans, 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); }
/** * Read-only map view of a collection. Resulting map is a lightweight view of an input collection, * with filtered elements of an input collection as keys, and closure execution results * as values. The map will only contain keys for which all provided predicates, if any, evaluate * 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 c Input collection. * @param mapClo Mapping closure, that maps key to value. * @param p Optional predicates to filter input collection. If predicates are not provided - all * elements will be in the view. * @param <K> Key type. * @param <V> Value type. * @return Light-weight view on given map with provided predicates and mapping. */ public static <K0, K extends K0, V0, V extends V0> Map<K, V> viewAsMap(@Nullable final Set<K> c, final IgniteClosure<? super K, V> mapClo, @Nullable final IgnitePredicate<? super K>... p) { A.notNull(mapClo, "trans"); if (isEmpty(c) || isAlwaysFalse(p)) return Collections.emptyMap(); return new PredicateSetView<>(c, mapClo, p); }
/** * @param c Input iterator. * @param trans Transforming closure to convert from T1 to T2. * @param readOnly If {@code true}, then resulting iterator will not allow modifications * to the underlying collection. * @param p Optional filtering predicates. * @return Iterator from given iterator and optional filtering predicate. */ @SafeVarargs public static <T1, T2> Iterator<T2> iterator(final Iterator<? extends T1> c, final IgniteClosure<? super T1, T2> trans, final boolean readOnly, @Nullable final IgnitePredicate<? super T1>... p) { A.notNull(c, "c", trans, "trans"); if (isAlwaysFalse(p)) return F.emptyIterator(); return new TransformFilteringIterator<>(c, trans, readOnly, 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); }
/** * Creates and returns transforming iterator from given collection and optional * filtering predicates. Returned iterator will only have elements for which all * given predicates evaluates to {@code true} ( if provided). Note that this method * will not create new collection but will simply "skip" elements in the provided * collection that given predicates doesn't evaluate to {@code true} for. * * @param c Input collection. * @param trans Transforming closure to convert from T1 to T2. * @param readOnly If {@code true}, then resulting iterator will not allow modifications * to the underlying collection. * @param p Optional filtering predicates. * @param <T1> Type of the collection elements. * @param <T2> Type of returned elements. * @return Iterator from given collection and optional filtering predicate. */ public static <T1, T2> GridIterator<T2> iterator(final Iterable<? extends T1> c, final IgniteClosure<? super T1, T2> trans, final boolean readOnly, @Nullable final IgnitePredicate<? super T1>... p) { A.notNull(c, "c", trans, "trans"); if (isAlwaysFalse(p)) return F.emptyIterator(); return new TransformFilteringIterator<>(c.iterator(), trans, readOnly, p); }
/** * 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; } }
else if (!isAlwaysFalse(p)) for (Iterator<T> iter = res.iterator(); iter.hasNext();) if (isAll(iter.next(), 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; }
/** * 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 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; }