Java Code Examples for org.apache.commons.collections4.ComparatorUtils#chainedComparator()
The following examples show how to use
org.apache.commons.collections4.ComparatorUtils#chainedComparator() .
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: AbstractMetadataRepository.java From archiva with Apache License 2.0 | 5 votes |
protected static Comparator<ArtifactMetadata> getArtifactMetadataComparator(final QueryParameter queryParameter, String defaultAttr) { List<Comparator<ArtifactMetadata>> compList = new ArrayList<>(); List<String> sortFields = new ArrayList<>(); if (queryParameter.getSortFields().size() == 0) { sortFields.add(defaultAttr); } else { sortFields = queryParameter.getSortFields(); } for (String attribute : sortFields) { switch (attribute) { case "id": compList.add(Comparator.comparing(ArtifactMetadata::getId)); break; case "whenGathered": compList.add(Comparator.comparing(ArtifactMetadata::getWhenGathered)); break; case "fileLastModified": compList.add(Comparator.comparing(ArtifactMetadata::getFileLastModified)); case "version": compList.add(Comparator.comparing(ArtifactMetadata::getVersion)); break; case "projectVersion": compList.add(Comparator.comparing(ArtifactMetadata::getProjectVersion)); break; case "project": compList.add(Comparator.comparing(ArtifactMetadata::getProject)); break; default: // } } Comparator<ArtifactMetadata> comp = ComparatorUtils.chainedComparator(compList); if (queryParameter.isAscending()) { return comp; } else { return comp.reversed(); } }
Example 2
Source File: BeanQuery.java From bean-query with Apache License 2.0 | 4 votes |
/** * Using an array of Comparators, applied in sequence until one returns not equal or the array is exhausted. */ public BeanQuery<T> orderBy(Comparator... beanComparator) { this.comparator = new DelegatedSortOrderableComparator(ComparatorUtils.chainedComparator(beanComparator)); return this; }
Example 3
Source File: BeanComparatorUtil.java From feilong-core with Apache License 2.0 | 3 votes |
/** * 按照不同指定属性 <code>propertyNameAndOrders</code> 排序的 {@link Comparator}. * * @param <T> * the generic type * @param propertyNameAndOrders * 属性名称和排序因子, * * <p> * 格式可以是纯的属性名称, 比如 "name"; 也可以是属性名称+排序因子(以空格分隔),比如 "name desc" * </p> * * <h3>说明:</h3> * <blockquote> * * <dl> * <dt>关于属性名称</dt> * <dd> * 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be * modified,参见<a href="../../bean/BeanUtil.html#propertyName">propertyName</a>,<br> * 该属性对应的value 必须实现 {@link Comparable}接口. * </dd> * * <dt>关于排序因子</dt> * <dd> * 可以没有排序因子<br> * 如果有,值可以是asc(顺序),desc(倒序)两种;<br> * 如果没有,默认按照asc(顺序)排序;<br> * 此外,asc/desc忽略大小写 * </dd> * * </dl> * * </blockquote> * @return 如果propertyNameAndOrders是单值,那么直接调用 {@link #propertyComparator(String)} 返回 * @throws NullPointerException * 如果 <code>propertyNameAndOrders</code> 是null,<br> * 或者有元素是 null; * @throws IllegalArgumentException * 如果 <code>propertyNameAndOrders</code> 是empty,<br> * 或者有元素是 blank * @see org.apache.commons.collections4.ComparatorUtils#chainedComparator(java.util.Collection) * @since 1.10.2 support propertyNameAndOrder */ public static <T> Comparator<T> chainedComparator(String...propertyNameAndOrders){ Validate.notEmpty(propertyNameAndOrders, "propertyNameAndOrders can't be null/empty!"); //如果propertyNameAndOrders是单值,那么直接调用 com.feilong.core.util.comparator.BeanComparatorUtil.propertyComparator(String) 返回 if (1 == propertyNameAndOrders.length){ return propertyComparator(propertyNameAndOrders[0]); } //--------------------------------------------------------------- List<Comparator<T>> comparators = newArrayList(); for (String propertyNameAndOrder : propertyNameAndOrders){ Validate.notBlank(propertyNameAndOrder, "propertyNameAndOrder can't be blank!"); String[] propertyNameAndOrderArray = SortHelper.parsePropertyNameAndOrder(propertyNameAndOrder); //注意:此处不要使用 propertyComparator(propertyName) //因为,PropertyComparator 如果属性值相同,会使用其他规则继续比较(为了TreeMap/treeSet), //也就是说,通常而言一次就比较出顺序,后续的propertyNameAndOrders 就没作用了 Comparator instance = ComparatorUtils.nullHighComparator(ComparableComparator.comparableComparator()); //null排在最后面 BeanComparator<T> beanComparator = new BeanComparator<>(propertyNameAndOrderArray[0], instance); comparators.add(isAsc(propertyNameAndOrderArray) ? beanComparator : reversedComparator(beanComparator)); } return ComparatorUtils.chainedComparator(comparators); }
Example 4
Source File: SortUtil.java From feilong-core with Apache License 2.0 | 2 votes |
/** * 如果 <code>comparators length ==1</code>,返回 comparators[0]; 否则返回 {@link ComparatorUtils#chainedComparator(Comparator...)}; * * @param <O> * the generic type * @param comparators * the comparators * @return the comparator * @since 1.8.2 */ @SafeVarargs private static <O> Comparator<O> toComparator(Comparator<O>...comparators){ return 1 == comparators.length ? comparators[0] : ComparatorUtils.chainedComparator(comparators); }