Java Code Examples for org.apache.commons.collections.comparators.ComparableComparator#getInstance()

The following examples show how to use org.apache.commons.collections.comparators.ComparableComparator#getInstance() . 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: ExcelUtil.java    From jframework with Apache License 2.0 6 votes vote down vote up
private static void sortByProperties(List<? extends Object> list, boolean isNullHigh,
                                     boolean isReversed, String... props) {
    if (CollectionUtils.isNotEmpty(list)) {
        Comparator<?> typeComp = ComparableComparator.getInstance();
        if (isNullHigh == true) {
            typeComp = ComparatorUtils.nullHighComparator(typeComp);
        } else {
            typeComp = ComparatorUtils.nullLowComparator(typeComp);
        }
        if (isReversed) {
            typeComp = ComparatorUtils.reversedComparator(typeComp);
        }

        List<Object> sortCols = new ArrayList<Object>();

        if (props != null) {
            for (String prop : props) {
                sortCols.add(new BeanComparator(prop, typeComp));
            }
        }
        if (sortCols.size() > 0) {
            Comparator<Object> sortChain = new ComparatorChain(sortCols);
            Collections.sort(list, sortChain);
        }
    }
}
 
Example 2
Source File: MyBeanUtils.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 根据给定的条件,把 list 中的 javabean 排序。
 * 用到了 commons beanutils 和  commons.collections
 *
 * @param list           待排序的 list
 * @param listOrderedMap 排序条件。
 *                       这是一个有序的 list ,排序条件按照加入到 list 的 bean 的属性(map 的 key)的先后顺序排序。
 *                       listOrderedMap 的 key 为待排序的 bean 的属性名称,值为是否按该属性的正序排序,true 为正序,false 为逆序。
 *                       使用方法见本类的 testSortListBeans() 方法例子,使用时注意不要写错 bean 的属性名称。
 * @param <T>            list 中的 bean 类型
 */
public static <T> void sortListBeans(List<T> list, ListOrderedMap listOrderedMap) {

    int num = listOrderedMap.size();
    ArrayList sortFields = new ArrayList();

    for (int i = 0; i < num; i++) {
        //  System.out.println("key =" + listOrderedMap.get(i) + " , value=" + listOrderedMap.getValue(i));
        Comparator comp = ComparableComparator.getInstance();

        comp = ComparatorUtils.nullLowComparator(comp);  //允许null

        if ((Boolean) listOrderedMap.getValue(i) == false)
            comp = ComparatorUtils.reversedComparator(comp); //逆序

        Comparator cmp = new BeanComparator((String) listOrderedMap.get(i), comp);
        sortFields.add(cmp);
    }

    ComparatorChain multiSort = new ComparatorChain(sortFields);
    Collections.sort(list, multiSort);
}
 
Example 3
Source File: CellComparatorHelper.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * This method returns a comparator to be used for comparing the contents of cells, that is
 * the compareTo method will be invoked w/ displaytag Cell objects
 * @param propClass
 * @return
 */
public static Comparator getAppropriateComparatorForPropertyClass(Class propClass) {
    // TODO, do we really need to create so many comparators (1 per each cell)?
    if (propClass == null) {
        return new NullCellComparator();
    }
    else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
        return new NumericCellComparator();
    }
    else if (TypeUtils.isTemporalClass(propClass)) {
        return new TemporalCellComparator();
    }
    else if (String.class.equals(propClass)) {
        // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
        return new StringCellComparator();
    }
    else {
        return ComparableComparator.getInstance();
    }
}
 
Example 4
Source File: CellComparatorHelper.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * This method returns a comparator to be used for comparing propertyValues (in String form)
 * @param propClass
 * @return
 */
public static Comparator getAppropriateValueComparatorForPropertyClass(Class propClass) {
    if (propClass == null) {
        return NullValueComparator.getInstance();
    }
    else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
        return NumericValueComparator.getInstance();
    }
    else if (TypeUtils.isTemporalClass(propClass)) {
        return TemporalValueComparator.getInstance();
    }
    else if (String.class.equals(propClass)) {
        // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
        return StringValueComparator.getInstance();
    }
    else {
        return ComparableComparator.getInstance();
    }
}
 
Example 5
Source File: BeanPropertyComparator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Constructs a PropertyComparator for comparing beans using the properties named in the given List.
 *
 * <p>Properties will be compared
 * in the order in which they are listed. Case will be ignored if ignoreCase is true.</p>
 * 
 * @param propertyNames List of property names (as Strings) used to compare beans
 * @param ignoreCase if true, case will be ignored during String comparisons
 */
public BeanPropertyComparator(List propertyNames, boolean ignoreCase) {
    if (propertyNames == null) {
        throw new IllegalArgumentException("invalid (null) propertyNames list");
    }
    if (propertyNames.size() == 0) {
        throw new IllegalArgumentException("invalid (empty) propertyNames list");
    }
    this.propertyNames = Collections.unmodifiableList(propertyNames);
    this.ignoreCase = ignoreCase;

    if (ignoreCase) {
        this.stringComparator = String.CASE_INSENSITIVE_ORDER;
    }
    else {
        this.stringComparator = ComparableComparator.getInstance();
    }
    this.booleanComparator = new Comparator() {
        public int compare(Object o1, Object o2) {
            int compared = 0;

            Boolean b1 = (Boolean) o1;
            Boolean b2 = (Boolean) o2;

            if (!b1.equals(b2)) {
                if (b1.equals(Boolean.FALSE)) {
                    compared = -1;
                }
                else {
                    compared = 1;
                }
            }

            return compared;
        }

    };
    this.genericComparator = ComparableComparator.getInstance();
}
 
Example 6
Source File: HadoopAbstractMapTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public Comparator<Object> sortComparator() {
    return ComparableComparator.getInstance();
}
 
Example 7
Source File: HadoopAbstractMapTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public Comparator<Object> groupComparator() {
    return ComparableComparator.getInstance();
}