Java Code Examples for org.apache.commons.collections4.PredicateUtils#allPredicate()

The following examples show how to use org.apache.commons.collections4.PredicateUtils#allPredicate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseRule.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public boolean evaluate(Object arg0) {
	Predicate judgement = (Predicate) new NullPredicate();
	if (predicates.size() == 1) {
		judgement = predicates.get(0);
	} else {
		if (conj == Conjunction.AND) {
			judgement = PredicateUtils.allPredicate(predicates);
		}
		else if (conj == Conjunction.OR) {
			judgement = PredicateUtils.anyPredicate(predicates);
		}
	}
	
	return judgement.evaluate(arg0);
}
 
Example 2
Source File: BaseRule.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public boolean evaluate(Object arg0) {
	Predicate judgement = (Predicate) new NullPredicate();
	if (predicates.size() == 1) {
		judgement = predicates.get(0);
	} else {
		if (conj == Conjunction.AND) {
			judgement = PredicateUtils.allPredicate(predicates);
		}
		else if (conj == Conjunction.OR) {
			judgement = PredicateUtils.anyPredicate(predicates);
		}
	}
	
	return judgement.evaluate(arg0);
}
 
Example 3
Source File: ResourceReleaseRule.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public boolean evaluate(Object arg0) {
	Predicate judgement = new NullPredicate();
	if (predicates.size() == 1) {
		judgement = predicates.get(0);
	} else {
		if (conj == Conjunction.AND) {
			judgement = PredicateUtils.allPredicate(predicates);
		}
		else if (conj == Conjunction.OR) {
			judgement = PredicateUtils.anyPredicate(predicates);
		}
	}
	
	return judgement.evaluate(arg0);
}
 
Example 4
Source File: FilterPredicate.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
private FilterPredicate(@NonNull Settings settings, @NonNull Set<WiFiBand> wiFiBands) {
    Predicate<WiFiDetail> ssidPredicate = makeSSIDPredicate(settings.getSSIDs());
    Predicate<WiFiDetail> wiFiBandPredicate = EnumUtils.predicate(WiFiBand.class, wiFiBands, new WiFiBandTransformer());
    Predicate<WiFiDetail> strengthPredicate = EnumUtils.predicate(Strength.class, settings.getStrengths(), new StrengthTransformer());
    Predicate<WiFiDetail> securityPredicate = EnumUtils.predicate(Security.class, settings.getSecurities(), new SecurityTransformer());
    List<Predicate<WiFiDetail>> predicates = Arrays.asList(ssidPredicate, wiFiBandPredicate, strengthPredicate, securityPredicate);
    this.predicate = PredicateUtils.allPredicate(CollectionUtils.select(predicates, new NoTruePredicate()));
}
 
Example 5
Source File: ResourceReleaseRule.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public boolean evaluate(Object arg0) {
	Predicate judgement = new NullPredicate();
	if (predicates.size() == 1) {
		judgement = predicates.get(0);
	} else {
		if (conj == Conjunction.AND) {
			judgement = PredicateUtils.allPredicate(predicates);
		}
		else if (conj == Conjunction.OR) {
			judgement = PredicateUtils.anyPredicate(predicates);
		}
	}
	
	return judgement.evaluate(arg0);
}
 
Example 6
Source File: SiteItemServiceImpl.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
@Override
public SiteItem getSiteItem(String url, ItemProcessor processor, Predicate<Item> predicate) {
    SiteContext context = getSiteContext();

    if(!storeService.exists(context.getContext(), url)) {
        return null;
    }

    if (CollectionUtils.isNotEmpty(defaultPredicates)) {
        List<Predicate<Item>> predicates = new ArrayList<>(defaultPredicates);

        if (predicate != null) {
            predicates.add(predicate);
        }

        predicate = PredicateUtils.allPredicate(predicates);
    }
    if (CollectionUtils.isNotEmpty(defaultProcessors)) {
        ItemProcessorPipeline processorPipeline = new ItemProcessorPipeline(new ArrayList<>(defaultProcessors));

        if (processor != null) {
            processorPipeline.addProcessor(processor);
        }

        processor = processorPipeline;
    }

    Item item = storeService.findItem(context.getContext(), null, url, processor);
    if (item != null && (predicate == null || predicate.evaluate(item))) {
        return createItemWrapper(item);
    } else {
        return null;
    }
}
 
Example 7
Source File: children.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response){

        final SlingScriptHelper sling = getScriptHelper(request);

        final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
        final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE));
        final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class);

        final String query = ex.getString(dsCfg.get("query", String.class));

        final String parentPath;
        final String searchName;
        final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath()));

        if (query != null) {
            final int slashIndex = query.lastIndexOf('/');
            if (slashIndex < 0) {
                parentPath = rootPath;
                searchName = query.toLowerCase();
            } else if (!query.startsWith(rootPath)) {
                parentPath = rootPath;
                searchName = null;
            } else if (slashIndex == query.length() - 1) {
                parentPath = query;
                searchName = null;
            } else {
                parentPath = query.substring(0, slashIndex + 1);
                searchName = query.substring(slashIndex + 1).toLowerCase();
            }
        } else {
            parentPath = ex.getString(dsCfg.get("path", String.class));
            searchName = null;
        }

        final Resource parent = request.getResourceResolver().getResource(parentPath);

        final DataSource ds;
        if (parent == null) {
            ds = EmptyDataSource.instance();
        } else {
            final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class);
            final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class);
            final String itemRT = dsCfg.get("itemResourceType", String.class);
            final String filter = ex.getString(dsCfg.get("filter", String.class));

            final Collection<Predicate<Resource>> predicates = new ArrayList<>(2);
            predicates.add(createPredicate(filter));

            if (searchName != null) {
                final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE);
                predicates.add(resource -> searchNamePattern.matcher(resource.getName()).lookingAt());
            }

            final Predicate predicate = PredicateUtils.allPredicate(predicates);
            final Transformer transformer = createTransformer(itemRT, predicate);


            final List<Resource> list;
            if (FILTER_CATEGORY.equals(filter)) {
                class CategoryFinder extends AbstractResourceVisitor {
                    private CategoryPredicate categoryPredicate = new CategoryPredicate();
                    private List<Resource> categories = new ArrayList<Resource>();
                    @Override
                    protected void visit(Resource res) {
                        if (categoryPredicate.evaluate(res)) {
                            categories.add(res);
                        }
                    }
                };
                CategoryFinder categoryFinder = new CategoryFinder();
                categoryFinder.accept(parent);
                list = IteratorUtils.toList(new FilterIterator(categoryFinder.categories.iterator(), predicate));
            } else {
                list =IteratorUtils.toList(new FilterIterator(parent.listChildren(), predicate));
            }

            //force reloading the children of the root node to hit the virtual resource provider
            if (rootPath.equals(parentPath)) {
                for (int i = 0; i < list.size(); i++) {
                    list.set(i, request.getResourceResolver().getResource(list.get(i).getPath()));
                }
            }

            @SuppressWarnings("unchecked")
            DataSource datasource = new AbstractDataSource() {

                public Iterator<Resource> iterator() {
                    Collections.sort(list, Comparator.comparing(Resource::getName));
                    return new TransformIterator(new PagingIterator<>(list.iterator(), offset, limit), transformer);
                }
            };

            ds = datasource;
        }

        request.setAttribute(DataSource.class.getName(), ds);
    }
 
Example 8
Source File: BeanPredicateUtil.java    From feilong-core with Apache License 2.0 4 votes vote down vote up
/**
 * 用来判断一个对象指定的属性以及属性值是否相等.
 * 
 * <h3>说明:</h3>
 * <blockquote>
 * <ol>
 * <li>
 * 常用于解析集合,如 {@link CollectionsUtil#select(Iterable, Predicate) select},{@link CollectionsUtil#find(Iterable, Predicate) find},
 * {@link CollectionsUtil#selectRejected(Iterable, Predicate) selectRejected},
 * {@link CollectionsUtil#group(Iterable, String, Predicate) group},
 * {@link AggregateUtil#groupCount(Iterable, String, Predicate) groupCount},
 * {@link AggregateUtil#sum(Iterable, String, Predicate) sum} 等方法.
 * </li>
 * </ol>
 * </blockquote>
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * <b>场景:</b> 在list中查找 名字是 关羽,并且 年龄是30 的user
 * </p>
 * 
 * <pre class="code">
 * 
 * User guanyu30 = new User("关羽", 30);
 * List{@code <User>} list = toList(//
 *                 new User("张飞", 23),
 *                 new User("关羽", 24),
 *                 new User("刘备", 25),
 *                 guanyu30);
 * 
 * Predicate{@code <User>} predicate = PredicateUtils
 *                 .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30));
 * 
 * assertEquals(guanyu30, CollectionsUtil.find(list, predicate));
 * </pre>
 * 
 * <p>
 * 此时你可以优化成:
 * </p>
 * 
 * <pre class="code">
 * 
 * User guanyu30 = new User("关羽", 30);
 * List{@code <User>} list = toList(//
 *                 new User("张飞", 23),
 *                 new User("关羽", 24),
 *                 new User("刘备", 25),
 *                 guanyu30);
 * 
 * Map{@code <String, Object>} map = ConvertUtil.toMap("name", "关羽", "age", 30);
 * assertEquals(guanyu30, find(list, BeanPredicateUtil.{@code <User>} equalPredicate(map)));
 * </pre>
 * 
 * </blockquote>
 *
 * @param <T>
 *            the generic type
 * @param propertyNameAndPropertyValueMap
 *            属性和指定属性值对应的map,其中key是泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
 *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>
 * @return 如果 <code>propertyNameAndPropertyValueMap</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>propertyNameAndPropertyValueMap</code> 是empty,抛出{@link IllegalArgumentException}<br>
 *         如果 <code>propertyNameAndPropertyValueMap</code> 中有key是null,抛出{@link NullPointerException}<br>
 *         如果 <code>propertyNameAndPropertyValueMap</code> 中有key是blank,抛出{@link IllegalArgumentException}<br>
 * @see #equalPredicate(String, Object)
 * @since 1.9.5
 */
public static <T> Predicate<T> equalPredicate(Map<String, ?> propertyNameAndPropertyValueMap){
    Validate.notEmpty(propertyNameAndPropertyValueMap, "propertyNameAndPropertyValueMap can't be null!");

    @SuppressWarnings("unchecked")
    BeanPredicate<T>[] beanPredicates = ArrayUtil.newArray(BeanPredicate.class, propertyNameAndPropertyValueMap.size());

    int index = 0;
    for (Map.Entry<String, ?> entry : propertyNameAndPropertyValueMap.entrySet()){
        String propertyName = entry.getKey();
        Object propertyValue = entry.getValue();

        Validate.notBlank(propertyName, "propertyName can't be blank!");

        Array.set(beanPredicates, index, equalPredicate(propertyName, propertyValue));
        index++;
    }
    return PredicateUtils.allPredicate(beanPredicates);
}