List of usage examples for org.apache.commons.collections.comparators ComparableComparator getInstance
public static ComparableComparator getInstance()
From source file:com.safetys.framework.jmesa.core.sort.ComparableAwareColumnSort.java
@SuppressWarnings("unchecked") public Collection<?> sortItems(Collection<?> items, Limit limit) { if (items.isEmpty()) { return items; }/*from w ww.java 2 s.c om*/ ComparatorChain chain = new ComparatorChain(); SortSet sortSet = limit.getSortSet(); for (Sort sort : sortSet.getSorts()) { Class<?> type = null; try { type = getPropertyClassType(items, sort.getProperty()); } catch (Exception e) { logger.error("Had problems getting the column sort type.", e); } if (type != null && Comparable.class.isAssignableFrom(type)) { if (sort.getOrder() == Order.ASC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator(ComparableComparator.getInstance()))); } else if (sort.getOrder() == Order.DESC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator(ComparableComparator.getInstance())), true); } } else if (sort.getOrder() == Order.ASC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator())); } else if (sort.getOrder() == Order.DESC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator()), true); } } if (chain.size() > 0) { Collections.sort((List<?>) items, chain); } return items; }
From source file:com.ineunet.knife.core.query.QueryParamParser.java
/** * Create a Comparator by <code>orderBy</code> and <code>sort</code>. *///from w w w .ja v a 2 s .c om @SuppressWarnings("unchecked") public <X> Comparator<X> getComparator() { parse(); if (StringUtils.isBlank(orderByField)) return ComparatorUtils.NATURAL_COMPARATOR; Comparator<X> mycmp = ComparableComparator.getInstance(); if ("DESC".equalsIgnoreCase(sort)) { mycmp = ComparatorUtils.reversedComparator(mycmp); } return new BeanComparator(orderByField, mycmp); }
From source file:com.projity.util.ClassUtils.java
public static Comparator getComparator(Class clazz) { if (comparatorMap == null) { comparatorMap = new HashMap<Class, Comparator>(); comparatorMap.put(String.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((String) o1).compareTo((String) o2); }/*from w w w.ja v a 2s. c o m*/ }); comparatorMap.put(Date.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Date) o1).compareTo((Date) o2); } }); comparatorMap.put(Integer.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Integer) o1).compareTo((Integer) o2); } }); comparatorMap.put(Long.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Long) o1).compareTo((Long) o2); } }); comparatorMap.put(Short.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Short) o1).compareTo((Short) o2); } }); comparatorMap.put(Float.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Float) o1).compareTo((Float) o2); } }); comparatorMap.put(Double.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Double) o1).compareTo((Double) o2); } }); comparatorMap.put(Byte.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Byte) o1).compareTo((Byte) o2); } }); comparatorMap.put(Boolean.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Boolean) o1).compareTo((Boolean) o2); } }); comparatorMap.put(Money.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Money) o1).compareTo((Money) o2); } }); comparatorMap.put(Duration.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Duration) o1).compareTo(o2); } }); comparatorMap.put(Work.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Work) o1).compareTo(o2); } }); comparatorMap.put(Rate.class, new Comparator() { public int compare(Object o1, Object o2) { if (o1 == null) return (o2 == null ? 0 : -1); else if (o2 == null) return 1; return ((Rate) o1).compareTo(o2); } }); } Comparator result = comparatorMap.get(clazz); if (result == null) { // in case none found, try comparing if the class is comparable if (Comparable.class.isAssignableFrom(clazz)) return ComparableComparator.getInstance(); else return defaultTextComparator; } return comparatorMap.get(clazz); }
From source file:com.projity.field.Field.java
/** * Given a collection, if each elements shares the same value for this * field, the value is returned, Otherwise null is returned. * * @param collection//from w w w. j a v a 2 s . co m * @param useMultipleValue * If true, the default value will be used if no elements or * values differ * @return */ public Object getCommonValue(Collection collection, boolean useMultipleValue, boolean text) { if (collection == null) return null; Iterator i = collection.iterator(); Object value = null; Object current; Object currentValue; Comparator comparatorToUse = (text ? ComparableComparator.getInstance() : getComparator()); while (i.hasNext()) { current = i.next(); if (text) currentValue = getText(current, null); else currentValue = getValue(current, null); if (value == null) value = currentValue; else if (0 != comparatorToUse.compare(currentValue, value)) { value = null; break; } } if (value == null && useMultipleValue) { value = getMultipleValueForType(); } return value; }
From source file:org.janusgraph.util.datastructures.PointInterval.java
@Override public T getStart() { Preconditions.checkArgument(!isEmpty(), "There are no points in this interval"); return (T) Collections.min(points, ComparableComparator.getInstance()); }
From source file:org.janusgraph.util.datastructures.PointInterval.java
@Override public T getEnd() { Preconditions.checkArgument(!isEmpty(), "There are no points in this interval"); return (T) Collections.max(points, ComparableComparator.getInstance()); }
From source file:org.jmesa.core.sort.ComparableAwareColumnSort.java
@SuppressWarnings("unchecked") @Override/* w w w . j a v a2 s .co m*/ public Collection<?> sortItems(Collection<?> items, Limit limit) { if (items.isEmpty()) { return items; } ComparatorChain chain = new ComparatorChain(); SortSet sortSet = limit.getSortSet(); for (Sort sort : sortSet.getSorts()) { Class<?> type = null; try { type = getPropertyClassType(items, sort.getProperty()); } catch (Exception e) { logger.error("Had problems getting the column sort type.", e); } if (type != null && Comparable.class.isAssignableFrom(type)) { if (sort.getOrder() == Order.ASC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator(ComparableComparator.getInstance()))); } else if (sort.getOrder() == Order.DESC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator(ComparableComparator.getInstance())), true); } } else if (sort.getOrder() == Order.ASC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator())); } else if (sort.getOrder() == Order.DESC) { chain.addComparator(new BeanComparator(sort.getProperty(), new NullComparator()), true); } } if (chain.size() > 0) { Collections.sort((List<?>) items, chain); } return items; }
From source file:org.kuali.rice.kns.util.BeanPropertyComparator.java
/** * Constructs a PropertyComparator for comparing beans using the properties named in the given List. Properties will be compared * in the order in which they are listed. Case will be ignored if ignoreCase is true. * //from w w w .java 2 s . c o m * @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(); }
From source file:org.kuali.rice.kns.web.comparator.CellComparatorHelper.java
/** * 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//from w ww . j a v a 2 s.co m * @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(); } }
From source file:org.kuali.rice.kns.web.comparator.CellComparatorHelper.java
/** * This method returns a comparator to be used for comparing propertyValues (in String form) * @param propClass/*w w w .j a v a 2 s .c o m*/ * @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(); } }