List of usage examples for org.apache.commons.collections.functors NotPredicate NotPredicate
public NotPredicate(Predicate predicate)
From source file:com.discursive.jccook.collections.predicate.XPathPredicatedMapExample.java
public void start() { // Create the Predicates Predicate teamName = new XPathPredicate("./name"); Predicate coachFirstName = new XPathPredicate("./coach/firstName"); Predicate coachLastName = new XPathPredicate("./coach/lastName"); // Create a predicate that rejects all teams with name Dodgers Map variables = new HashMap(); variables.put("name", "Dodgers"); Predicate noDodgers = new NotPredicate(new XPathPredicate("./name[. = $name]", variables)); // Tie three Predicates together into an AllPredicate Predicate[] predicateArray = new Predicate[] { teamName, coachFirstName, coachLastName, noDodgers }; AllPredicate valuePredicate = new AllPredicate(predicateArray); // Decorate a HashMap with a predicate on the value teamMap = PredicatedMap.decorate(new HashMap(), null, valuePredicate); // Manufacture some teams Team redSox = new Team("Red Sox", new Coach("Patrick", "Moloney")); Team yankees = new Team("Yankees", new Coach("David", "McGarry")); Team dodgers = new Team("Dodgers", new Coach("Nick", "Taylor")); Team twins = new Team(null, new Coach("Patrick", "Moloney")); Team braves = new Team("Braves", null); // The follow put calls should work fine teamMap.put("RedSox", redSox); teamMap.put("Yankees", yankees); // This should fail because we have a predicate checking for "Dodgers" and rejecting try {/*www.j a va 2 s. co m*/ teamMap.put("Dodgers", dodgers); } catch (IllegalArgumentException iae) { System.out.println("Dodgers put failed, as expected"); } // This put should fail because the team name is null try { teamMap.put("Twins", twins); } catch (IllegalArgumentException iae) { System.out.println("Twins put failed, as expected"); } // This put should fail because the coach is null try { teamMap.put("Braves", braves); } catch (IllegalArgumentException iae) { System.out.println("Braves put failed, as expected"); } }
From source file:com.mindquarry.persistence.mock.SessionMock.java
private Map<String, Predicate> properties(String queryPredicate) { Map<String, Predicate> result = new HashMap<String, Predicate>(); for (String simplePredicate : queryPredicate.split("and")) { String comparator = comparator(simplePredicate); String[] nameValue = simplePredicate.trim().split(comparator); Predicate predicate = new EqualPredicate(nameValue[1].trim().replace("'", "")); if (isNotEquals(comparator)) predicate = new NotPredicate(predicate); result.put(nameValue[0], predicate); }/*from ww w .j ava2 s. c om*/ return result; }
From source file:org.apache.atlas.repository.util.FilterUtil.java
public static Predicate getPredicateFromSearchFilter(SearchFilter searchFilter) { List<Predicate> predicates = new ArrayList<>(); final String type = searchFilter.getParam(SearchFilter.PARAM_TYPE); final String name = searchFilter.getParam(SearchFilter.PARAM_NAME); final String supertype = searchFilter.getParam(SearchFilter.PARAM_SUPERTYPE); final String notSupertype = searchFilter.getParam(SearchFilter.PARAM_NOT_SUPERTYPE); // Add filter for the type/category if (StringUtils.isNotBlank(type)) { predicates.add(getTypePredicate(type)); }//from w ww . j a v a 2 s . c o m // Add filter for the name if (StringUtils.isNotBlank(name)) { predicates.add(getNamePredicate(name)); } // Add filter for the supertype if (StringUtils.isNotBlank(supertype)) { predicates.add(getSuperTypePredicate(supertype)); } // Add filter for the supertype negation if (StringUtils.isNotBlank(notSupertype)) { predicates.add(new NotPredicate(getSuperTypePredicate(notSupertype))); } return PredicateUtils.allPredicate(predicates); }
From source file:org.openfaces.component.filter.PredicateBuilder.java
private static Predicate convertToPredicate(final ExpressionFilterCriterion expressionFilterCriterion) { final FilterCondition condition = expressionFilterCriterion.getCondition(); final Object parameter = expressionFilterCriterion.getArg1(); if (parameter == null && condition != FilterCondition.EMPTY) return TruePredicate.getInstance(); final Predicate predicateFunctor = condition.process(new FilterConditionProcessor<Predicate>() { public Predicate processEmpty() { return new Predicate() { public boolean evaluate(Object o) { return o == null || o.equals(""); }// w w w .ja v a2s. co m }; } public Predicate processEquals() { Comparator comparator = getComparatorForParameter(parameter); if (parameter instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); Date dayStart = ParametersInterpreter.dayStart((Date) parameter, timeZone); Predicate preficateForBefore = new ComparePredicate(dayStart, FilterCondition.GREATER_OR_EQUAL, comparator); Date dayEnd = ParametersInterpreter.dayEnd((Date) parameter, timeZone); Predicate preficateForAfter = new ComparePredicate(dayEnd, FilterCondition.LESS_OR_EQUAL, comparator); return new AllPredicate(new Predicate[] { preficateForBefore, preficateForAfter }); } else if (parameter instanceof String) { boolean caseSensitive = expressionFilterCriterion.isCaseSensitive(); return new AbstractStringPredicate(parameter.toString(), caseSensitive) { public boolean evaluate(String parameter, String value) { return value.equals(parameter); } }; } else { return new ComparePredicate(parameter, condition, comparator); } } public Predicate processContains() { boolean caseSensitive = expressionFilterCriterion.isCaseSensitive(); return new AbstractStringPredicate(parameter.toString(), caseSensitive) { public boolean evaluate(String parameter, String value) { return value.contains(parameter); } }; } public Predicate processBegins() { boolean caseSensitive = expressionFilterCriterion.isCaseSensitive(); return new AbstractStringPredicate(parameter.toString(), caseSensitive) { public boolean evaluate(String parameter, String value) { return value.startsWith(parameter); } }; } public Predicate processEnds() { boolean caseSensitive = expressionFilterCriterion.isCaseSensitive(); return new AbstractStringPredicate(parameter.toString(), caseSensitive) { public boolean evaluate(String parameter, String value) { return value.endsWith(parameter); } }; } public Predicate processLess() { Comparator comparator = getComparatorForParameter(parameter); Object correctedParameter = parameter; if (parameter instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); correctedParameter = ParametersInterpreter.dayStart((Date) parameter, timeZone); } return new ComparePredicate(correctedParameter, condition, comparator); } public Predicate processGreater() { Comparator comparator = getComparatorForParameter(parameter); Object correctedParameter = parameter; if (parameter instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); correctedParameter = ParametersInterpreter.dayEnd((Date) parameter, timeZone); } return new ComparePredicate(correctedParameter, condition, comparator); } public Predicate processLessOrEqual() { Comparator comparator = getComparatorForParameter(parameter); Object correctedParameter = parameter; if (parameter instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); correctedParameter = ParametersInterpreter.dayEnd((Date) parameter, timeZone); } return new ComparePredicate(correctedParameter, condition, comparator); } public Predicate processGreaterOrEqual() { Comparator comparator = getComparatorForParameter(parameter); Object correctedParameter = parameter; if (parameter instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); correctedParameter = ParametersInterpreter.dayStart((Date) parameter, timeZone); } return new ComparePredicate(correctedParameter, condition, comparator); } public Predicate processBetween() { Comparator comparator = getComparatorForParameter(parameter); Object parameter1 = parameter; Object parameter2 = expressionFilterCriterion.getArg2(); if (parameter2 == null) return TruePredicate.getInstance(); if (parameter1 instanceof Date && parameter2 instanceof Date) { TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone"); parameter1 = ParametersInterpreter.dayStart((Date) parameter1, timeZone); parameter2 = ParametersInterpreter.dayEnd((Date) parameter1, timeZone); } Predicate preficateForBefore = new ComparePredicate(parameter1, FilterCondition.GREATER_OR_EQUAL, comparator); Predicate preficateForAfter = new ComparePredicate(parameter2, FilterCondition.LESS_OR_EQUAL, comparator); return new AllPredicate(new Predicate[] { preficateForBefore, preficateForAfter }); } }); final PropertyLocator propertyLocator = expressionFilterCriterion.getPropertyLocator(); Predicate predicate = new Predicate() { public boolean evaluate(Object o) { return predicateFunctor.evaluate(propertyLocator.getPropertyValue(o)); } }; return (expressionFilterCriterion.isInverse()) ? new NotPredicate(predicate) : predicate; }
From source file:org.openvpms.web.component.im.act.ActHierarchyFilter.java
/** * Helper to return a predicate that includes/excludes acts based on their short name. * * @param shortNames the act short names * @param include if {@code true} include the acts, otherwise exclude them * @return a new predicate/*from w w w . j av a 2s . c o m*/ */ protected static Predicate createIsA(final String[] shortNames, boolean include) { Predicate result = new IsA(RelationshipRef.TARGET, shortNames); return (include) ? result : new NotPredicate(result); }
From source file:org.openvpms.web.component.im.lookup.LookupFilter.java
/** * Creates a new <tt>LookupFilter</tt>. * * @param query the source to filter from * @param include determines if the lookups should be included or excluded * @param codes the codes to include or exclude */// w ww . ja va 2 s.c o m public LookupFilter(LookupQuery query, boolean include, String... codes) { this.query = query; if (include) { predicate = new Match(); } else { predicate = new NotPredicate(new Match()); } this.codes = codes; }