Example usage for java.lang Comparable compareTo

List of usage examples for java.lang Comparable compareTo

Introduction

In this page you can find the example usage for java.lang Comparable compareTo.

Prototype

public int compareTo(T o);

Source Link

Document

Compares this object with the specified object for order.

Usage

From source file:ru.runa.wfe.presentation.BatchPresentationComparator.java

@Override
public int compare(Executor o1, Executor o2) {
    int result = 0;
    FieldDescriptor[] sortingFields = batchPresentation.getSortedFields();
    boolean[] sortingOrder = batchPresentation.getFieldsToSortModes();

    try {/* w ww  .  ja  va 2 s . c  o  m*/
        for (int i = 0; i < sortingFields.length; i++) {
            Comparable<Comparable<?>> field1 = (Comparable<Comparable<?>>) PropertyUtils.getProperty(o1,
                    sortingFields[i].dbSources[0].getValueDBPath(null, null));
            Comparable<Comparable<?>> field2 = (Comparable<Comparable<?>>) PropertyUtils.getProperty(o2,
                    sortingFields[i].dbSources[0].getValueDBPath(null, null));
            result = field1.compareTo(field2);
            if (!sortingOrder[i]) {
                result *= -1;
            }
            if (result != 0) {
                break;
            }
        }
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException(
                o1 + " of class" + o1.getClass() + ", " + o2 + " of class" + o2.getClass());
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(
                o1 + " of class" + o1.getClass() + ", " + o2 + " of class" + o2.getClass());
    } catch (InvocationTargetException e) {
        throw new IllegalArgumentException(
                o1 + " of class" + o1.getClass() + ", " + o2 + " of class" + o2.getClass());
    }
    return result;
}

From source file:net.sf.click.jquery.examples.services.CustomerService.java

@SuppressWarnings("unchecked")
public List<Customer> getCustomersForPage(int offset, int pageSize, final String sortColumn,
        final boolean ascending) {

    // Ok this implementation is a cheat since all customers are in memory
    List<Customer> customers = StartupListener.CUSTOMERS;

    if (StringUtils.isNotBlank(sortColumn)) {
        Collections.sort(customers, new Comparator() {

            public int compare(Object o1, Object o2) {
                int ascendingSort = ascending ? 1 : -1;
                Comparable v1 = (Comparable) PropertyUtils.getValue(o1, sortColumn);
                Comparable v2 = (Comparable) PropertyUtils.getValue(o2, sortColumn);
                if (v1 == null) {
                    return -1 * ascendingSort;
                } else if (v2 == null) {
                    return 1 * ascendingSort;
                }// w w w .  j  ava 2s .  c o  m
                return v1.compareTo(v2) * ascendingSort;
            }
        });
    }

    return customers.subList(offset, Math.min(offset + pageSize, customers.size()));
}

From source file:org.jspare.core.collections.TreeNode.java

/**
 * Find tree node./*from   ww  w.java2 s . co  m*/
 *
 * @param cmp
 *            the cmp
 * @return the tree node
 */
public TreeNode<T> findTreeNode(Comparable<T> cmp) {
    for (TreeNode<T> element : this.elementsIndex) {
        T elData = element.data;
        if (cmp.compareTo(elData) == 0) {
            return element;
        }
    }

    return null;
}

From source file:ca.sqlpower.architect.ddl.TypeMap.java

/**
 * convenience method for checking if a rule criteria is satisifed
 * /*from w w w  .j ava2s . c  o m*/
 * @return boolean to tell if the rule is satisfied.  Works equally well with Strings 
 * and Integer.
 */
protected boolean satisfiesComparison(Comparable c1, Comparable c2, String operator) {
    if (c1.compareTo(c2) == 0 && operator.equals("=") || c1.compareTo(c2) > 0 && operator.equals(">")
            || c1.compareTo(c2) < 0 && operator.equals("<")) {
        return true;
    }
    return false;
}

From source file:org.mitre.mpf.wfm.data.entities.transients.Track.java

private int nullSafeCompare(Comparable a, Comparable b) {
    if (a == null && b == null) {
        return 0;
    } else if (a == null) {
        return -1;
    } else if (b == null) {
        return 1;
    } else {/*from w  w w.  j a  va2  s . c o  m*/
        return a.compareTo(b);
    }
}

From source file:com.jd.survey.domain.survey.QuestionAnswer.java

@Override
public int compareTo(QuestionAnswer that) {
    final int BEFORE = -1;
    final int AFTER = 1;
    if (that == null) {
        return BEFORE;
    }//w w  w.  j ava2 s.co m
    Comparable<Short> thisQuestionAnswer = this.getOrder();
    Comparable<Short> thatQuestionAnswer = that.getOrder();
    if (thisQuestionAnswer == null) {
        return AFTER;
    } else if (thatQuestionAnswer == null) {
        return BEFORE;
    } else {
        return thisQuestionAnswer.compareTo(that.getOrder());
    }
}

From source file:jp.co.acroquest.endosnipe.report.converter.compressor.SamplingCompressor.java

/**
 * ????????//  w  w  w .j  a  va2s .co  m
 * 
 * @param rawSamples  ?
 * @param targetField ?
 * @return ??
 * @throws IllegalAccessException ?????????
 * @throws InvocationTargetException ??????
 * @throws NoSuchMethodException ??????
 * @throws NoSuchFieldException 
 * @throws SecurityException 
 * @throws InstantiationException 
 */
private Object getMaxValueFromSampleList(List rawSamples, String targetField, Class clazz)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException,
        NoSuchFieldException, InstantiationException {
    if (rawSamples.size() < 1) {
        Calculator calculator = getFieldCalculator(clazz, targetField);
        return calculator.immediate("0");
    }

    List values = getValuesByFieldName(rawSamples, targetField);

    if (!(values.get(0) instanceof Comparable)) {
        return values.get(0);
    }

    Comparable maxValue = (Comparable) values.get(0);
    for (Object value : values) {
        if (maxValue.compareTo(value) < 0) {
            maxValue = (Comparable) value;
        }
    }

    return maxValue;
}

From source file:jp.co.acroquest.endosnipe.report.converter.compressor.SamplingCompressor.java

/**
 * ?????????/*w w w .  jav  a  2 s .c o m*/
 * 
 * @param rawSamples  ?
 * @param targetField ??
 * @return ???
 * @throws IllegalAccessException ?????????
 * @throws InvocationTargetException ??????
 * @throws NoSuchMethodException ??????
 * @throws NoSuchFieldException 
 * @throws SecurityException 
 * @throws InstantiationException 
 */
private Object getMinValueFromSampleList(List rawSamples, String targetField, Class clazz)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException,
        NoSuchFieldException, InstantiationException {
    if (rawSamples.size() < 1) {
        Calculator calculator = getFieldCalculator(clazz, targetField);
        return calculator.immediate("0");
    }

    List values = getValuesByFieldName(rawSamples, targetField);

    if (!(values.get(0) instanceof Comparable)) {
        return values.get(0);
    }

    Comparable minValue = (Comparable) values.get(0);
    for (Object value : values) {
        if (minValue.compareTo(value) > 0) {
            minValue = (Comparable) value;
        }
    }

    return minValue;
}

From source file:com.opensymphony.xwork2.validator.validators.AbstractRangeValidator.java

public void validate(Object object) throws ValidationException {
    Object obj = getFieldValue(getFieldName(), object);
    Comparable<T> value = (Comparable<T>) obj;

    // if there is no value - don't do comparison
    // if a value is required, a required validator should be added to the field
    if (value == null) {
        return;//from  w  w  w . j  a v  a  2s. co m
    }

    // only check for a minimum value if the min parameter is set
    T minComparatorValue = getMin();
    if ((minComparatorValue != null) && (value.compareTo(minComparatorValue) < 0)) {
        addFieldError(getFieldName(), object);
    }

    // only check for a maximum value if the max parameter is set
    T maxComparatorValue = getMax();
    if ((maxComparatorValue != null) && (value.compareTo(maxComparatorValue) > 0)) {
        addFieldError(getFieldName(), object);
    }
}

From source file:org.codhaus.groovy.grails.validation.RangeConstraint.java

@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    if (range.contains(propertyValue)) {
        return;/*w w w. ja  v  a2  s.c  o m*/
    }

    Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue,
            range.getFrom(), range.getTo() };

    Comparable from = range.getFrom();
    Comparable to = range.getTo();

    if (from instanceof Number && propertyValue instanceof Number) {
        // Upgrade the numbers to Long, so all integer types can be compared.
        from = ((Number) from).longValue();
        to = ((Number) to).longValue();
        propertyValue = ((Number) propertyValue).longValue();
    }

    if (from.compareTo(propertyValue) > 0) {
        rejectValue(target, errors, ConstrainedPropertyGunn.DEFAULT_INVALID_RANGE_MESSAGE_CODE,
                ConstrainedPropertyGunn.RANGE_CONSTRAINT + ConstrainedPropertyGunn.TOOSMALL_SUFFIX, args);
    } else if (to.compareTo(propertyValue) < 0) {
        rejectValue(target, errors, ConstrainedPropertyGunn.DEFAULT_INVALID_RANGE_MESSAGE_CODE,
                ConstrainedPropertyGunn.RANGE_CONSTRAINT + ConstrainedPropertyGunn.TOOBIG_SUFFIX, args);
    }
}