本文整理汇总了Java中org.apache.commons.collections.comparators.ComparableComparator.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java ComparableComparator.getInstance方法的具体用法?Java ComparableComparator.getInstance怎么用?Java ComparableComparator.getInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.collections.comparators.ComparableComparator
的用法示例。
在下文中一共展示了ComparableComparator.getInstance方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sortListBeans
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* 根据给定的条件,把 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);
}
示例2: getAppropriateComparatorForPropertyClass
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* 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();
}
}
示例3: getAppropriateValueComparatorForPropertyClass
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* 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();
}
}
示例4: sortByProperties
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static void sortByProperties(List<? extends Object> list, boolean isNullHigh,
boolean isReversed, String... props) {
if (CollectionUtils.isNotEmpty(list)) {
Comparator<?> typeComp = ComparableComparator.getInstance();
if (isNullHigh) {
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);
}
}
}
示例5: BeanPropertyComparator
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* 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();
}
示例6: BucketDefinition
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* 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.getInstance();
}
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;
}
示例7: sortComparator
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public Comparator<Object> sortComparator() {
return ComparableComparator.getInstance();
}
示例8: groupComparator
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public Comparator<Object> groupComparator() {
return ComparableComparator.getInstance();
}
示例9: BeanComparator
import org.apache.commons.collections.comparators.ComparableComparator; //导入方法依赖的package包/类
/**
* Constructs a property-based comparator for beans.
* This constructor creates
* a BeanComparator that uses the supplied Comparator to compare
* the property values.
*
* @param property Name of a bean property, can contain the name
* of a simple, nested, indexed, mapped, or combined
* property. See {@link PropertyUtilsBean} for property query language
* syntax.
* @param comparator BeanComparator will pass the values of the
* specified bean property to this Comparator.
* If your bean property is not a comparable or
* contains null values, a suitable comparator
* may be supplied in this constructor.
*/
public BeanComparator( final String property, final Comparator<?> comparator ) {
setProperty( property );
if (comparator != null) {
this.comparator = comparator;
} else {
this.comparator = ComparableComparator.getInstance();
}
}