Example usage for org.apache.commons.collections4.functors ComparatorPredicate ComparatorPredicate

List of usage examples for org.apache.commons.collections4.functors ComparatorPredicate ComparatorPredicate

Introduction

In this page you can find the example usage for org.apache.commons.collections4.functors ComparatorPredicate ComparatorPredicate.

Prototype

public ComparatorPredicate(final T object, final Comparator<T> comparator, final Criterion criterion) 

Source Link

Document

Constructor that performs no validation.

Usage

From source file:com.feilong.core.util.AggregateUtilTest.java

/**
 * Test sum4.//from  w w  w.ja  va 2  s.  c  o m
 */
@Test
public void testSum4() {
    List<User> list = toList(//
            new User(2L), new User(50L), new User(50L));

    BigDecimal expected = new BigDecimal(100L);
    assertEquals(expected, AggregateUtil.sum(list, "id", new Predicate<User>() {

        @Override
        public boolean evaluate(User user) {
            return user.getId() > 10L;
        }
    }));

    //*****************************************************************

    Predicate<Long> predicate = new ComparatorPredicate<Long>(10L, ComparatorUtils.<Long>naturalComparator(),
            Criterion.LESS);
    BigDecimal sum = AggregateUtil.sum(list, "id", new BeanPredicate<User>("id", predicate));
    assertEquals(new BigDecimal(100L), sum);
}

From source file:com.feilong.core.util.AggregateUtilTest.java

/**
 * TestStatisticsUtilTest.//  w ww. j ava  2 s  .co m
 */
@Test
public void testStatisticsUtilTest() {
    String[] planets = { "Mercury", "Venus", "Earth", "Mars" };
    Comparator<String> distanceFromSun = new FixedOrderComparator<>(planets);
    Predicate<String> predicate = new ComparatorPredicate<String>("Venus", distanceFromSun, Criterion.GREATER);

    LOGGER.debug("{}", predicate.evaluate("Earth"));
}

From source file:com.feilong.core.util.AggregateUtilTest.java

@Test
public void testStatisticsUtilTest1() {

    // >10 /* w ww  . jav  a2  s  .  c  o m*/
    Predicate<Integer> predicate = new ComparatorPredicate<Integer>(10,
            ComparatorUtils.<Integer>naturalComparator(), Criterion.LESS);

    List<Integer> result = CollectionsUtil.select(toList(1, 5, 10, 30, 55, 88, 1, 12, 3), predicate);
    LOGGER.debug(JsonUtil.format(result, 0, 0));
}

From source file:com.feilong.core.util.predicate.BeanPredicateUtil.java

/**
 * Comparator predicate./*from  w  w  w.  ja  v  a2  s  .c o  m*/
 * 
 * <p>
 * <code>valueToCompare</code>  ???t<code>propertyName</code>,( <code>comparator</code> )<br>
 * ?, <code><b>comparator.compare(valueToCompare, propertyValue)</b></code>
 * </p>
 * 
 * <h3> {@link Criterion}:</h3>
 * 
 * <blockquote>
 * <table border="1" cellspacing="0" cellpadding="4" summary="">
 * 
 * <tr style="background-color:#ccccff">
 * <th align="left"></th>
 * <th align="left"></th>
 * </tr>
 * 
 * 
 * <tr valign="top">
 * <td>{@link Criterion#EQUAL}</td>
 * <td>comparator.compare(valueToCompare, propertyValue) == 0</td>
 * </tr>
 * 
 * <tr valign="top" style="background-color:#eeeeff">
 * <td>{@link Criterion#LESS}</td>
 * <td>comparator.compare(valueToCompare, propertyValue) < 0</td>
 * </tr>
 * 
 * <tr valign="top">
 * <td>{@link Criterion#GREATER}</td>
 * <td>comparator.compare(valueToCompare, propertyValue) > 0</td>
 * </tr>
 * 
 * <tr valign="top" style="background-color:#eeeeff">
 * <td>{@link Criterion#GREATER_OR_EQUAL}</td>
 * <td>comparator.compare(valueToCompare, propertyValue) >= 0</td>
 * </tr>
 * 
 * <tr valign="top">
 * <td>{@link Criterion#LESS_OR_EQUAL}</td>
 * <td>comparator.compare(valueToCompare, propertyValue) <= 0</td>
 * </tr>
 * 
 * </table>
 * </blockquote>
 *
 * @param <T>
 *            the generic type
 * @param <V>
 *            the value type
 * @param propertyName
 *            T??,Possibly indexed and/or nested name of the property to be modified,??
 *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>
 * @param valueToCompare
 *            the value to compare
 * @param comparator
 *            the comparator
 * @param criterion
 *            the criterion
 * @return  <code>propertyName</code> null, {@link NullPointerException}<br>
 *          <code>propertyName</code> blank, {@link IllegalArgumentException}<br>
 * @see org.apache.commons.collections4.functors.ComparatorPredicate
 * @since commons-collections 4
 */
public static <T, V extends Comparable<? super V>> Predicate<T> comparatorPredicate(String propertyName,
        V valueToCompare, Comparator<V> comparator, Criterion criterion) {
    return new BeanPredicate<T>(propertyName,
            new ComparatorPredicate<V>(valueToCompare, comparator, criterion));
}