List<Cat> bigList = ....; // master list Collection<Cat> smallList = CollectionUtils.select(bigList, new Predicate() { public boolean evaluate(Object o) { Cat c = (Cat)o; return c.getFavoriteFood().equals("Wiskas") && c.getWhateverElse().equals(Something); } });
PredicateGroup group = new PredicateGroup("tags"); // This will set operator between each predicate to OR group.setAllRequired(false); Predicate tagPredicate = new Predicate("tags"); tagPredicate.set(JcrPropertyPredicateEvaluator.OPERATION, JcrPropertyPredicateEvaluator.OP_LIKE); tagPredicate.set(JcrPropertyPredicateEvaluator.PROPERTY, "./jcr:content/cq:tags"); tagPredicate.set(JcrPropertyPredicateEvaluator.VALUE, cookieValue); group.add(tagPredicate); Predicate metaTagPredicate = new Predicate("metatags"); metaTagPredicate.set(JcrPropertyPredicateEvaluator.OPERATION, JcrPropertyPredicateEvaluator.OP_LIKE); metaTagPredicate.set(JcrPropertyPredicateEvaluator.PROPERTY, "./jcr:content/metadata/cq:tags"); metaTagPredicate.set(JcrPropertyPredicateEvaluator.VALUE, cookieValue); group.add(metaTagPredicate); search.addPredicate(group); // To debug and view your predicate tree you can invoke toString() on your predicate group
Predicate condition = new Predicate() { boolean evaluate(Object sample) { return ((Sample)sample).value3.equals("three"); } }; List result = CollectionUtils.select( list, condition );
Customer findCustomerByid(final int id){ return (Customer) CollectionUtils.find(customers, new Predicate() { public boolean evaluate(Object arg0) { return ((Customer) arg0).getId()==id; } }); }
Collection<SomeObject> selectedObjects = CollectionUtils .select(list, new Predicate() { boolean evaluate(Object input) { return ((SomeObject) input).someValue().equals(value); } });
Predicate even = new Predicate() { public boolean evaluate(Object object) { return ((Integer)object) % 2 == 0; } }
Predicate test1 = new Predicate() { @Override public boolean eval(int a, int b) { return a >= b; } }; // etc.
Predicate inactiveCompaniesPredicate = new Predicate() { @Override public boolean apply(PredicateContext ctx) { if(ctx.item(Map.class).containsKey("errorMsg") && ((JSONArray)((JSONArray) ctx.item(Map.class).get("errorMsg")).get(0)).get(0).equals("INACTIVE")) { return true; } else { return false; } } };
Iterables.all(collection, new Predicate() { boolean apply(T element) { .... //check your condition } }
List<Toy> toys; List<Toy> playstations = Collections2.filter( toys, new Predicate() { boolean apply(TOY toy){ return toy == TOY.PLAYSTATION; } });
Collection matches = CollectionUtils.select(collection, new Predicate() { public boolean evaluate(Object object) { // evaluate predicate } });
Predicate p = new Predicate() { public boolean evaluate(Object o) { return ((Adress) o).getTypeAddress().getType().equals(TYPE_INPUTED_BY_USER); } } return CollectionUtils.exists(p);
Predicate predicate = new Predicate(){{ public boolean evaluate(Object object){ // If statement comparison goes here } }}; filter(HexValuesEnd, predicate);
public static Predicate ALL = new Predicate(){ @Override boolean accepts(Object in) { return true; } }; @SuppressWarnings("unchecked") static final <T> Predicate<T> all(){ return (Predicate<T>)ALL;}
assertTrue(Collections2.filter(list, new Predicate() { public boolean apply(Integer val) { return val > 0; }).count() > 0);
public static int getHighestMarkByType(ArrayList<eProperty> allData, String type) { Predicate predicate = new Predicate() { public boolean evaluate(Object data) { if ((eProperty) data).getType().equals(type)) { return true; } else { return false; } } }; ArrayList<eProperty> filteredData = (ArrayList<eProperty>) CollectionUtils.select(allData,predicate); int maxMarks = 0; for (eProperty data : filteredData) { if (data.getMarks() > maxMark) { maxMarks = data.getMark(); } } return maxMarks; }
Predicate<MyInput> a = new Predicate(){ @Override public boolean apply(MyInput i) { return condition A; } }; Predicate<MyInput> b = new Predicate(){ @Override public boolean apply(MyInput i) { try { return condition B; } catch (IndexOutOfBoundsException e) { return false; } } }; Predicate<MyInput> c = ... if(Predicates.or(a, b, c).apply(myInput)) { // do something }
void foo(final int n) { CollectionUtils.filter(collection, new Predicate() { public boolean evaluate(Object input) { return input.equals(n); } }); }
List<Person> filteredList = new ArrayList<Person>(allPersons); CollectionUtils.filter( filteredList, new Predicate() { boolean evaluate(Object object) { //do whatever you want } });
boolean contains = CollectionUtils.exists(listFoo, new Predicate() { public boolean evaluate(Object input) { return "Baz".equals(((Foo)input).getBar()); } });