org.apache.commons.collections4.comparators.ComparableComparator Java Examples

The following examples show how to use org.apache.commons.collections4.comparators.ComparableComparator. 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: MainLogicImpl.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
private <E> void sort(List<E> entities, List<SortOrder<String>> sortOrders) {
	if (sortOrders != null && sortOrders.size() > 0) {
		ComparatorChain<E> comparatorChain = new ComparatorChain<>();
		for (SortOrder<String> sortOrder : sortOrders) {
			comparatorChain.addComparator(new BeanComparator<>(sortOrder.getKey(), new NullComparator<>(ComparableComparator.comparableComparator(), true)), sortOrder.getDirection() == SortDir.DESC);
		}
		Collections.sort(entities, comparatorChain);
	}
}
 
Example #2
Source File: BucketDefinition.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Comparator<Object> createOrderComparator(Comparator<Object> comparator, BucketOrder order)
{
	Comparator<Object> orderComparator;
	switch (order)
	{
		case DESCENDING:
		{
			if (comparator == null)
			{
				orderComparator = new ReverseComparator<Object>();
			}
			else
			{
				orderComparator = new ReverseComparator<Object>(comparator);
			}
			break;
		}
		case ASCENDING:				
		{
			if (comparator == null)
			{
				orderComparator = ComparableComparator.INSTANCE;
			}
			else
			{
				orderComparator = comparator;
			}
			break;
		}
		case NONE:
		default:
			throw 
				new JRRuntimeException(
					EXCEPTION_MESSAGE_KEY_UNSUPPORTED_ORDER_TYPE,
					new Object[]{order});
	}
	return orderComparator;
}
 
Example #3
Source File: SortedMapTest.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Constructor: If map param is null, must throw an IllegalArgumentException. */
@Test(expectedExceptions=NullPointerException.class)
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public void nullMapTest()
{
   new SortedMap(null, new ComparableComparator<>());
}
 
Example #4
Source File: DynamicValueSortedTreeMap.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public DynamicValueSortedTreeMap() {
	this(new ComparableComparator());
}
 
Example #5
Source File: BucketDefinition.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a bucket.
 * 
 * @param valueClass the class of the bucket values
 * @param orderer bucket entries orderer
 * @param comparator the comparator to use for bucket sorting
 * @param order the order type, {@link BucketOrder#ASCENDING}, {@link BucketOrder#DESCENDING} or {@link BucketOrder#NONE}
 * @param totalPosition the position of the total bucket
 * @throws JRException
 */
public BucketDefinition(Class<?> valueClass, 
		BucketOrderer orderer, Comparator<Object> comparator, BucketOrder order, 
		CrosstabTotalPositionEnum totalPosition) throws JRException
{
	this.orderer = orderer;
	this.order = order;
	
	if (orderer == null)
	{
		// we don't have a bucket orderer
		if (order == BucketOrder.NONE)
		{
			// no ordering, values are inserted in the order in which they come
			this.bucketValueComparator = null;
		}
		else
		{
			// the buckets are ordered using the bucket values
			// if there's no comparator, we're assuming that the values are Comparable
			this.bucketValueComparator = createOrderComparator(comparator, order);
		}
	}
	else
	{
		// we have an order by expression
		// we only need an internal ordering for bucket values
		if (Comparable.class.isAssignableFrom(valueClass))
		{
			// using natural order
			this.bucketValueComparator = ComparableComparator.INSTANCE;
		}
		else
		{
			// using an arbitrary rank comparator
			// TODO lucianc couldn't we just set here bucketValueComparator to null?
			if (log.isDebugEnabled())
			{
				log.debug("Using arbitrary rank comparator for bucket");
			}
			
			this.bucketValueComparator = new ArbitraryRankComparator();
		}
	}
	
	this.totalPosition = totalPosition;
	computeTotal = totalPosition != CrosstabTotalPositionEnum.NONE || orderer != null;
}
 
Example #6
Source File: BeanComparatorUtil.java    From feilong-core with Apache License 2.0 3 votes vote down vote up
/**
 * 按照不同指定属性 <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);
}