Java Code Examples for org.apache.commons.collections4.Predicate#evaluate()

The following examples show how to use org.apache.commons.collections4.Predicate#evaluate() . 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: AnyExistsPredicate.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean evaluate(T o) {
	for (Predicate<T> subPredicate : subPredicates) {
		if (subPredicate.evaluate(o)) {
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: AllExistsPredicate.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean evaluate(T o) {
	for (Predicate<T> subPredicate : subPredicates) {
		if (!subPredicate.evaluate(o)) {
			return false;
		}
	}
	return true;
}
 
Example 5
Source File: CentralAuthenticationServiceImpl.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Timed(name = "GET_TICKETS_TIMER")
@Metered(name = "GET_TICKETS_METER")
@Counted(name="GET_TICKETS_COUNTER", monotonic=true)
@Override
public Collection<Ticket> getTickets(final Predicate predicate) {
    final Collection<Ticket> c = new HashSet<>(this.ticketRegistry.getTickets());
    final Iterator<Ticket> it = c.iterator();
    while (it.hasNext()) {
        if (!predicate.evaluate(it.next())) {
            it.remove();
        }
    }
    return c;
}
 
Example 6
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 7
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 8
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 9
Source File: FlowController.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private ProcessorStatus getProcessorStatus(final RepositoryStatusReport report, final ProcessorNode procNode, final Predicate<Authorizable> isAuthorized) {
    final boolean isProcessorAuthorized = isAuthorized.evaluate(procNode);

    final ProcessorStatus status = new ProcessorStatus();
    status.setId(procNode.getIdentifier());
    status.setGroupId(procNode.getProcessGroup().getIdentifier());
    status.setName(isProcessorAuthorized ? procNode.getName() : procNode.getIdentifier());
    status.setType(isProcessorAuthorized ? procNode.getComponentType() : "Processor");

    final FlowFileEvent entry = report.getReportEntries().get(procNode.getIdentifier());
    if (entry == null) {
        status.setInputBytes(0L);
        status.setInputCount(0);
        status.setOutputBytes(0L);
        status.setOutputCount(0);
        status.setBytesWritten(0L);
        status.setBytesRead(0L);
        status.setProcessingNanos(0);
        status.setInvocations(0);
        status.setAverageLineageDuration(0L);
        status.setFlowFilesRemoved(0);
    } else {
        final int processedCount = entry.getFlowFilesOut();
        final long numProcessedBytes = entry.getContentSizeOut();
        status.setOutputBytes(numProcessedBytes);
        status.setOutputCount(processedCount);

        final int inputCount = entry.getFlowFilesIn();
        final long inputBytes = entry.getContentSizeIn();
        status.setInputBytes(inputBytes);
        status.setInputCount(inputCount);

        final long readBytes = entry.getBytesRead();
        status.setBytesRead(readBytes);

        final long writtenBytes = entry.getBytesWritten();
        status.setBytesWritten(writtenBytes);

        status.setProcessingNanos(entry.getProcessingNanoseconds());
        status.setInvocations(entry.getInvocations());

        status.setAverageLineageDuration(entry.getAverageLineageMillis());

        status.setFlowFilesReceived(entry.getFlowFilesReceived());
        status.setBytesReceived(entry.getBytesReceived());
        status.setFlowFilesSent(entry.getFlowFilesSent());
        status.setBytesSent(entry.getBytesSent());
        status.setFlowFilesRemoved(entry.getFlowFilesRemoved());
    }

    // Determine the run status and get any validation error... only validating while STOPPED
    // is a trade-off we are willing to make, even though processor validity could change due to
    // environmental conditions (property configured with a file path and the file being externally
    // removed). This saves on validation costs that would be unnecessary most of the time.
    if (ScheduledState.DISABLED.equals(procNode.getScheduledState())) {
        status.setRunStatus(RunStatus.Disabled);
    } else if (ScheduledState.RUNNING.equals(procNode.getScheduledState())) {
        status.setRunStatus(RunStatus.Running);
    } else if (!procNode.isValid()) {
        status.setRunStatus(RunStatus.Invalid);
    } else {
        status.setRunStatus(RunStatus.Stopped);
    }

    status.setActiveThreadCount(processScheduler.getActiveThreadCount(procNode));

    return status;
}
 
Example 10
Source File: AggregateUtil.java    From feilong-core with Apache License 2.0 4 votes vote down vote up
/**
 * 迭代<code>beanIterable</code>,提取符合 <code>includePredicate</code>的元素的指定 <code>propertyNames</code> 元素的值 ,累计总和.
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * User user1 = new User(10L);
 * user1.setName("刘备");
 * user1.setAge(50);
 * 
 * User user2 = new User(20L);
 * user1.setName("关羽");
 * user2.setAge(50);
 * 
 * User user3 = new User(100L);
 * user3.setName("张飞");
 * user3.setAge(100);
 * 
 * List{@code <User>} list = toList(user1, user2, user3);
 * Map{@code <String, BigDecimal>} map = AggregateUtil.sum(list, ConvertUtil.toArray("id", "age"), new Predicate{@code <User>}(){
 * 
 *     {@code @Override}
 *     public boolean evaluate(User user){
 *         return !"张飞".equals(user.getName());
 *     }
 * });
 * LOGGER.debug(JsonUtil.format(map));
 * 
 * </pre>
 * 
 * <b>返回:</b>
 * 
 * <pre class="code">
 * {
 * "id": 30,
 * "age": 100
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param <O>
 *            the generic type
 * @param beanIterable
 *            bean Iterable,诸如List{@code <User>},Set{@code <User>}等
 * @param propertyNames
 *            泛型O对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
 *            <a href="../bean/BeanUtil.html#propertyName">propertyName</a>
 * @param includePredicate
 *            the include predicate
 * @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br>
 *         如果通过反射某个元素值是null,则使用默认值0代替,再进行累加<br>
 *         如果 <code>includePredicate</code> 是null,那么迭代所有的元素<br>
 *         如果<code>beanIterable</code>没有符合 <code>includePredicate</code>的元素,返回 <code>new LinkedHashMap</code>
 * @throws NullPointerException
 *             如果<code>propertyNames</code> 是null
 * @throws IllegalArgumentException
 *             果<code>propertyNames</code> 有元素 是null <br>
 */
public static <O> Map<String, BigDecimal> sum(Iterable<O> beanIterable,String[] propertyNames,Predicate<O> includePredicate){
    if (isNullOrEmpty(beanIterable)){
        return emptyMap();
    }
    Validate.noNullElements(propertyNames, "propertyNames can't be null/empty!");

    Map<String, BigDecimal> sumMap = newLinkedHashMap(IterableUtils.size(beanIterable));
    for (O obj : beanIterable){
        if (null != includePredicate && !includePredicate.evaluate(obj)){
            continue;
        }

        for (String propertyName : propertyNames){
            //如果通过反射某个元素值是null,则使用默认值0 代替
            BigDecimal addValue = NumberUtil.getAddValue(
                            defaultIfNull(sumMap.get(propertyName), 0),
                            defaultIfNull(PropertyUtil.<Number> getProperty(obj, propertyName), 0));
            sumMap.put(propertyName, addValue);
        }
    }
    return sumMap;
}
 
Example 11
Source File: AggregateUtil.java    From feilong-core with Apache License 2.0 4 votes vote down vote up
/**
 * 迭代<code>beanIterable</code>,提取符合 <code>includePredicate</code>的元素,取元素 <code>keyPropertyName</code> 的值为 key ,累计
 * <code>sumPropertyName</code> 属性值 为 value,返回 map.
 * 
 * <p>
 * 统计 user list 所有 age 小于等于30的, 按照姓名分组, 累加每个人的 age 总和
 * </p>
 * 
 * <pre class="code">
 * 
 * Predicate{@code <User>} comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 30, Criterion.GREATER_OR_EQUAL);
 * 
 * List{@code <User>} list = toList(//
 *                 new User("张飞", 20),
 *                 new User("关羽", 20),
 *                 new User("刘备", 20),
 *                 new User("刘备", 50),
 *                 new User("刘备", 20));
 * 
 * Map{@code <String, BigDecimal>} map = AggregateUtil.groupSum(list, "name", "age",comparatorPredicate);
 * 
 * </pre>
 * 
 * <b>assertThat:</b>
 * 
 * <pre class="code">
 * assertThat(
 *                 map,
 *                 allOf(//
 *                                 hasEntry("刘备", toBigDecimal(40)),
 *                                 hasEntry("张飞", toBigDecimal(20)),
 *                                 hasEntry("关羽", toBigDecimal(20))));
 * </pre>
 * 
 * </blockquote>
 *
 * @param <O>
 *            the generic type
 * @param <T>
 *            the generic type
 * @param beanIterable
 *            bean Iterable,诸如List{@code <User>},Set{@code <User>}等
 * @param keyPropertyName
 *            泛型O对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
 *            <a href="../bean/BeanUtil.html#propertyName">propertyName</a>
 * @param sumPropertyName
 *            泛型O对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
 *            <a href="../bean/BeanUtil.html#propertyName">propertyName</a>
 * @param includePredicate
 *            the include predicate
 * @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br>
 *         如果通过反射某个元素值是null,则使用默认值0代替,再进行累加<br>
 *         如果 <code>includePredicate</code> 是null,那么迭代所有的元素<br>
 *         如果 <code>beanIterable</code>没有符合 <code>includePredicate</code>的元素,返回 <code>new LinkedHashMap</code>
 * 
 *         如果 <code>keyPropertyName</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>keyPropertyName</code> 是blank,抛出 {@link IllegalArgumentException}<br>
 *         如果 <code>sumPropertyName</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>sumPropertyName</code> 是blank,抛出 {@link IllegalArgumentException}<br>
 * @since 1.13.2
 */
public static <O, T> Map<T, BigDecimal> groupSum(
                Iterable<O> beanIterable,
                String keyPropertyName,
                String sumPropertyName,
                Predicate<O> includePredicate){
    if (isNullOrEmpty(beanIterable)){
        return emptyMap();
    }

    //---------------------------------------------------------------
    Validate.notBlank(keyPropertyName, "keyPropertyName can't be null/empty!");
    Validate.notBlank(sumPropertyName, "sumPropertyName can't be null/empty!");

    Map<T, BigDecimal> map = newLinkedHashMap();
    for (O obj : beanIterable){
        if (null != includePredicate && !includePredicate.evaluate(obj)){
            continue;
        }

        T keyPropertyValue = PropertyUtil.<T> getProperty(obj, keyPropertyName);
        BigDecimal value = toBigDecimal(defaultIfNull(PropertyUtil.<Number> getProperty(obj, sumPropertyName), ZERO));
        MapUtil.putSumValue(map, keyPropertyValue, value);
    }
    return map;
}
 
Example 12
Source File: AggregateUtil.java    From feilong-core with Apache License 2.0 4 votes vote down vote up
/**
 * 循环 <code>beanIterable</code>,只选择符合 <code>includePredicate</code>的对象,统计 <code>propertyName</code>的值出现的次数.
 * 
 * <h3>说明:</h3>
 * <blockquote>
 * <ol>
 * <li>返回的{@link LinkedHashMap},key是<code>propertyName</code>对应的值,value是该值出现的次数;<br>
 * 顺序是 <code>beanIterable</code> <code>propertyName</code>的值的顺序</li>
 * </ol>
 * </blockquote>
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * <b>场景:</b> 统计user list(条件是 age {@code >} 30 的user),name属性值的数量
 * </p>
 * 
 * <pre class="code">
 * List{@code <User>} list = new ArrayList{@code <>}();
 * list.add(new User("张飞", 20));
 * list.add(new User("关羽", 30));
 * list.add(new User("刘备", 40));
 * list.add(new User("赵云", 50));
 * 
 * Map{@code <String, Integer>} map = AggregateUtil.groupCount(list, "name", new Predicate{@code <User>}(){
 *     {@code @Override}
 *     public boolean evaluate(User user){
 *         return user.getAge() {@code >} 30;
 *     }
 * });
 * LOGGER.debug(JsonUtil.format(map));
 * 
 * </pre>
 * 
 * <b>返回:</b>
 * 
 * <pre class="code">
 * {
 * "刘备": 1,
 * "赵云": 1
 * }
 * 
 * </pre>
 * 
 * </blockquote>
 *
 * @param <O>
 *            the generic type
 * @param <T>
 *            the generic type
 * @param beanIterable
 *            bean Iterable,诸如List{@code <User>},Set{@code <User>}等
 * @param propertyName
 *            泛型O对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
 *            <a href="../bean/BeanUtil.html#propertyName">propertyName</a>
 * @param includePredicate
 *            只选择 符合 <code>includePredicate</code>的对象,如果是null 则统计集合中全部的元素
 * @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br>
 *         如果 <code>propertyName</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>propertyName</code> 是blank,抛出 {@link IllegalArgumentException}<br>
 *         如果 <code>includePredicate</code> 是null,则统计集合中全部的元素<br>
 * @see org.apache.commons.collections4.CollectionUtils#getCardinalityMap(Iterable)
 */
public static <O, T> Map<T, Integer> groupCount(Iterable<O> beanIterable,String propertyName,Predicate<O> includePredicate){
    if (isNullOrEmpty(beanIterable)){
        return emptyMap();
    }
    Validate.notBlank(propertyName, "propertyName can't be null/empty!");

    Map<T, Integer> map = newLinkedHashMap();
    for (O obj : beanIterable){
        if (null != includePredicate && !includePredicate.evaluate(obj)){
            continue;
        }
        MapUtil.putSumValue(map, PropertyUtil.<T> getProperty(obj, propertyName), 1);
    }
    return map;
}
 
Example 13
Source File: StandardEventAccess.java    From nifi with Apache License 2.0 4 votes vote down vote up
private ProcessorStatus getProcessorStatus(final FlowFileEvent flowFileEvent, final ProcessorNode procNode, final Predicate<Authorizable> isAuthorized) {
    final boolean isProcessorAuthorized = isAuthorized.evaluate(procNode);

    final ProcessScheduler processScheduler = flowController.getProcessScheduler();

    final ProcessorStatus status = new ProcessorStatus();
    status.setId(procNode.getIdentifier());
    status.setGroupId(procNode.getProcessGroup().getIdentifier());
    status.setName(isProcessorAuthorized ? procNode.getName() : procNode.getIdentifier());
    status.setType(isProcessorAuthorized ? procNode.getComponentType() : "Processor");

    if (flowFileEvent != null && flowFileEvent != EmptyFlowFileEvent.INSTANCE) {
        final int processedCount = flowFileEvent.getFlowFilesOut();
        final long numProcessedBytes = flowFileEvent.getContentSizeOut();
        status.setOutputBytes(numProcessedBytes);
        status.setOutputCount(processedCount);

        final int inputCount = flowFileEvent.getFlowFilesIn();
        final long inputBytes = flowFileEvent.getContentSizeIn();
        status.setInputBytes(inputBytes);
        status.setInputCount(inputCount);

        final long readBytes = flowFileEvent.getBytesRead();
        status.setBytesRead(readBytes);

        final long writtenBytes = flowFileEvent.getBytesWritten();
        status.setBytesWritten(writtenBytes);

        status.setProcessingNanos(flowFileEvent.getProcessingNanoseconds());
        status.setInvocations(flowFileEvent.getInvocations());

        status.setAverageLineageDuration(flowFileEvent.getAverageLineageMillis());

        status.setFlowFilesReceived(flowFileEvent.getFlowFilesReceived());
        status.setBytesReceived(flowFileEvent.getBytesReceived());
        status.setFlowFilesSent(flowFileEvent.getFlowFilesSent());
        status.setBytesSent(flowFileEvent.getBytesSent());
        status.setFlowFilesRemoved(flowFileEvent.getFlowFilesRemoved());

        if (isProcessorAuthorized) {
            status.setCounters(flowFileEvent.getCounters());
        }
    }

    // Determine the run status and get any validation error... only validating while STOPPED
    // is a trade-off we are willing to make, even though processor validity could change due to
    // environmental conditions (property configured with a file path and the file being externally
    // removed). This saves on validation costs that would be unnecessary most of the time.
    if (ScheduledState.DISABLED.equals(procNode.getScheduledState())) {
        status.setRunStatus(RunStatus.Disabled);
    } else if (ScheduledState.RUNNING.equals(procNode.getScheduledState())) {
        status.setRunStatus(RunStatus.Running);
    } else if (procNode.getValidationStatus() == ValidationStatus.VALIDATING) {
        status.setRunStatus(RunStatus.Validating);
    } else if (procNode.getValidationStatus() == ValidationStatus.INVALID) {
        status.setRunStatus(RunStatus.Invalid);
    } else {
        status.setRunStatus(RunStatus.Stopped);
    }

    status.setExecutionNode(procNode.getExecutionNode());
    status.setTerminatedThreadCount(procNode.getTerminatedThreadCount());
    status.setActiveThreadCount(processScheduler.getActiveThreadCount(procNode));

    return status;
}