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:com.cyclopsgroup.waterview.utils.BeanPropertyComparator.java

/**
 * Overwrite or implement method compare()
 *
 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 *///from  ww w  .  ja v a2 s .  c  om
public int compare(Object o1, Object o2) {
    try {
        Comparable p1 = (Comparable) PropertyUtils.getProperty(o1, propertyName);
        Comparable p2 = (Comparable) PropertyUtils.getProperty(o2, propertyName);
        return p1.compareTo(p2);
    } catch (Exception e) {
        return 0;
    }
}

From source file:org.kineticsystem.commons.data.model.bean.BeanComparator.java

/**
 * Compare the two elements delegating the comparison to the specified
 * property.//from  w w w  .ja v  a 2  s .  c  om
 * @param obj1 The first element to compare.
 * @param obj2 The second element to compare.
 * @return An integer value as specified by the <tt>Comparator</tt>
 *     interface.
 * @see java.util.Comparator
 */
@SuppressWarnings("unchecked")
public int compare(Object obj1, Object obj2) {

    int i = 0;
    int comparison = 0;

    while ((comparison == 0) && (i < propertyNames.length)) {

        String propertyName = propertyNames[i++];

        try {
            Object v1 = PropertyUtils.getProperty(obj1, propertyName);
            Object v2 = PropertyUtils.getProperty(obj2, propertyName);

            if ((v1 instanceof Comparable) && (v2 instanceof Comparable)) {
                Comparable c1 = (Comparable) v1;
                Comparable c2 = (Comparable) v2;
                comparison = c1.compareTo(c2);
            }
        } catch (Exception ex) {
            logger.error(ex);
        }
    }

    return comparison;
}

From source file:com.nzion.util.UtilMisc.java

public static <T> int compare(Comparable<T> obj1, T obj2) {
    if (obj1 == null) {
        if (obj2 == null) {
            return 0;
        } else {/*from w ww .  j a  v a2s.co  m*/
            return 1;
        }
    } else {
        return obj1.compareTo(obj2);
    }
}

From source file:com.intuit.tank.PropertyComparer.java

/**
 * @{inheritDoc/* ww w  . java 2s .co m*/
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public int compare(T src, T tgt) {
    int retVal = 0;
    if (src == null && tgt == null) {
        retVal = 0;
    } else if (src != null && tgt == null) {
        retVal = 1;
    } else if (src == null && tgt != null) {
        retVal = -1;
    } else {
        try {
            Object property = PropertyUtils.getProperty(src, propertyName);
            Object property2 = PropertyUtils.getProperty(tgt, propertyName);

            if (property == null && property2 == null) {
                retVal = 0;
            } else if (property == null && property2 != null) {
                retVal = -1;
            } else if (property != null && property2 == null) {
                retVal = 1;
            } else if (Comparable.class.isAssignableFrom(property.getClass())) {
                Comparable c1 = (Comparable) property;
                Comparable c2 = (Comparable) property2;
                retVal = c1.compareTo(c2);
            } else {
                retVal = property.toString().compareTo(property2.toString());
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(
                    "Cannot access the method.  Possible error in setting the access type for the getter setters of "
                            + propertyName);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getMessage());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("No getter/setter method found for " + propertyName);
        }
    }

    if (sortOrder == SortOrder.DESCENDING) {
        retVal = retVal * -1;
    }
    return retVal;
}

From source file:com.linkedin.pinot.core.segment.creator.impl.stats.AbstractColumnStatisticsCollector.java

void addressSorted(Object entry) {
    if (isSorted) {
        if (previousValue != null) {
            if (!entry.equals(previousValue) && previousValue != null) {
                final Comparable prevValue = (Comparable) previousValue;
                final Comparable origin = (Comparable) entry;
                if (origin.compareTo(prevValue) < 0) {
                    isSorted = false;//from   w  ww .j av a2s.c  o  m
                }
            }
        }
        previousValue = entry;
    }
}

From source file:com.vaadin.tests.tb3.AbstractTB3Test.java

/**
 * Asserts that {@literal a} is &gt; {@literal b}
 *
 * @param message//from   www.  ja  v  a2 s.co m
 *            The message to include in the {@link AssertionError}
 * @param a
 * @param b
 * @throws AssertionError
 *             If comparison fails
 */
public static final <T> void assertGreater(String message, Comparable<T> a, T b) throws AssertionError {
    if (a.compareTo(b) > 0) {
        return;
    }
    throw new AssertionError(decorate(message, a, b));
}

From source file:com.vaadin.tests.tb3.AbstractTB3Test.java

/**
 * Asserts that {@literal a} is &lt; {@literal b}
 *
 * @param message/*from   w w  w. j  a  v  a  2  s.c o m*/
 *            The message to include in the {@link AssertionError}
 * @param a
 * @param b
 * @throws AssertionError
 *             If comparison fails
 */
public static final <T> void assertLessThan(String message, Comparable<T> a, T b) throws AssertionError {
    if (a.compareTo(b) < 0) {
        return;
    }
    throw new AssertionError(decorate(message, a, b));
}

From source file:com.vaadin.tests.tb3.AbstractTB3Test.java

/**
 * Asserts that {@literal a} is &gt;= {@literal b}
 *
 * @param message//from www  .j  a v a 2s  .c  om
 *            The message to include in the {@link AssertionError}
 * @param a
 * @param b
 * @throws AssertionError
 *             If comparison fails
 */
public static final <T> void assertGreaterOrEqual(String message, Comparable<T> a, T b) throws AssertionError {
    if (a.compareTo(b) >= 0) {
        return;
    }

    throw new AssertionError(decorate(message, a, b));
}

From source file:com.vaadin.tests.tb3.AbstractTB3Test.java

/**
 * Asserts that {@literal a} is &lt;= {@literal b}
 *
 * @param message//from   ww  w. j  a  v  a2  s .  c o m
 *            The message to include in the {@link AssertionError}
 * @param a
 * @param b
 * @throws AssertionError
 *             If comparison fails
 */
public static final <T> void assertLessThanOrEqual(String message, Comparable<T> a, T b) throws AssertionError {
    if (a.compareTo(b) <= 0) {
        return;
    }

    throw new AssertionError(decorate(message, a, b));
}

From source file:org.rhq.enterprise.gui.legacy.taglib.display.BeanSorter.java

/**
 * Compares two objects by first fetching a property from each object and then comparing that value. If there are
 * any errors produced while trying to compare these objects then a RunTimeException will be thrown as any error
 * found here will most likely be a programming error that needs to be quickly addressed (like trying to compare
 * objects that are not comparable, or trying to read a property from a bean that is invalid, etc...)
 *
 * @throws RuntimeException if there are any problems making a comparison of the object properties.
 *//*from   w w  w .j a  v  a 2 s  .c o m*/

public int compare(Object o1, Object o2) throws RuntimeException {
    if (property == null) {
        throw new NullPointerException("Null property provided which " + "prevents BeanSorter sort");
    }

    try {
        Object p1 = null;
        Object p2 = null;

        // If they have supplied a decorator, then make sure and use it for
        // the sorting as well... TODO - Major hack....

        if (this.dec != null) {
            try {
                dec.initRow(o1, -1, -1);
                p1 = PropertyUtils.getProperty(dec, property);

                dec.initRow(o2, -1, -1);
                p2 = PropertyUtils.getProperty(dec, property);
            } catch (Exception e) {
                // If there were any problems, then assume that the decorator
                // does not implement that method, and instead fall down to
                // the original object...

                p1 = PropertyUtils.getProperty(o1, property);
                p2 = PropertyUtils.getProperty(o2, property);
            }
        } else {
            p1 = PropertyUtils.getProperty(o1, property);
            p2 = PropertyUtils.getProperty(o2, property);
        }

        if ((p1 instanceof Comparable) && (p2 instanceof Comparable)) {
            Comparable c1 = (Comparable) p1;
            Comparable c2 = (Comparable) p2;
            return c1.compareTo(c2);
        } else {
            throw new RuntimeException(
                    "Object returned by property \"" + property + "\" is not a Comparable object");
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException("IllegalAccessException thrown while " + "trying to fetch property \""
                + property + "\" during sort");
    } catch (InvocationTargetException e) {
        throw new RuntimeException("InvocationTargetException thrown while " + "trying to fetch property \""
                + property + "\" during sort");
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("NoSuchMethodException thrown while " + "trying to fetch property \""
                + property + "\" during sort");
    }
}