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:ar.com.fdvs.dj.util.MultiPropertyComparator.java

private static int compare(final Comparable _value1, final Comparable _value2) {
    if (_value1 == null) {
        if (_value2 == null) {
            return 0;
        } else {// w ww .j av  a  2 s .c  om
            return -1;
        }
    } else if (_value2 == null) {
        return 1;
    } else {
        return _value1.compareTo(_value2);
    }
}

From source file:ch.systemsx.cisd.openbis.generic.client.web.server.resultset.CachedResultSetManager.java

private static <T> Comparator<T> createComparator(final SortDir sortDir, final IColumnDefinition<T> sortField) {
    Comparator<T> comparator = new Comparator<T>() {

        @SuppressWarnings("unchecked")
        public int compare(T o1, T o2) {
            Comparable v1 = sortField.getComparableValue(o1);
            Comparable v2 = sortField.getComparableValue(o2);
            return v1.compareTo(v2);
        }/*  w ww .  ja v a2s .co  m*/
    };
    return applySortDir(sortDir, comparator);
}

From source file:Main.java

/**
 * Compares the initial elements of two arrays.
 *
 * @param a1  array 1./*  w  ww .  j a  v  a 2 s  .com*/
 * @param a2  array 2.
 *
 * @return An integer showing the relative ordering.
 */
public static int compareVersionArrays(Comparable[] a1, Comparable[] a2) {
    int length = Math.min(a1.length, a2.length);
    for (int i = 0; i < length; i++) {
        Comparable o1 = a1[i];
        Comparable o2 = a2[i];
        if (o1 == null && o2 == null) {
            // cannot decide ..
            continue;
        }
        if (o1 == null) {
            return 1;
        }
        if (o2 == null) {
            return -1;
        }
        int retval = o1.compareTo(o2);
        if (retval != 0) {
            return retval;
        }
    }
    return 0;
}

From source file:com.clican.pluto.common.util.SearchUtils.java

/**
 * ????// ww w . j  a va 2  s .c  o m
 * 
 * @param <T>
 *            
 * @param <K>
 *            
 * @param source
 *            ?
 * @param comparablePropName
 *            ?
 * @param comp
 *            
 * @return
 */
@SuppressWarnings("unchecked")
public static <T, K> T searchPreviousNearestInput(List<T> source, String comparablePropName,
        Comparable<K> comp) {
    if (source == null || source.size() == 0) {
        return null;
    }
    for (int i = (source.size() - 1); i >= 0; i--) {
        T input = source.get(i);
        try {
            K value = (K) PropertyUtils.getProperty(input, comparablePropName);
            if (comp.compareTo(value) >= 0) {
                return input;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return null;
}

From source file:edu.utexas.cs.tactex.utils.BrokerUtils.java

/**
 * @param list/* w  w  w  . j  a v a2 s  . co m*/
 * @param value
 */
public static <T> void insertToSortedArrayList(ArrayList<T> list, T value) {
    list.add(value);
    @SuppressWarnings("unchecked")
    Comparable<T> cmp = (Comparable<T>) value;
    for (int i = list.size() - 1; i > 0 && cmp.compareTo(list.get(i - 1)) < 0; i--)
        Collections.swap(list, i, i - 1);
}

From source file:com.ebay.erl.mobius.core.model.TupleColumnComparator.java

private static int compare(Comparable v1, Comparable v2) {
    return v1.compareTo(v2);
}

From source file:org.eclipse.mylyn.commons.core.CoreUtil.java

/**
 * Compares <code>o1</code> and <code>o2</code>.
 * //from  www  .j  av  a2s.  c  o  m
 * @since 3.7
 * @return a negative integer, 0, or a positive, if o1 is less than o2, o1 equals o2 or o1 is more than o2; null is
 *         considered less than any value
 */
public static <T> int compare(Comparable<T> o1, T o2) {
    if (o1 == null) {
        return (o2 != null) ? 1 : 0;
    } else if (o2 == null) {
        return -1;
    }
    return o1.compareTo(o2);
}

From source file:org.kuali.rice.krms.impl.repository.mock.CriteriaMatcherInMemory.java

public static boolean matchesLessThan(Object dataValue, Object criteriaValue) {
    if (dataValue == criteriaValue) {
        return false;
    }/*from   ww w . j  a v  a2s. c  om*/
    if (dataValue == null && criteriaValue == null) {
        return false;
    }
    if (dataValue == null) {
        return false;
    }
    if (criteriaValue instanceof Comparable) {
        Comparable comp1 = (Comparable) dataValue;
        Comparable comp2 = (Comparable) criteriaValue;
        if (comp1.compareTo(comp2) < 0) {
            return true;
        }
        return false;
    }
    throw new IllegalArgumentException("The values are not comparable " + criteriaValue);
}

From source file:org.kuali.rice.krms.impl.repository.mock.CriteriaMatcherInMemory.java

public static boolean matchesGreaterThan(Object dataValue, Object criteriaValue) {
    if (dataValue == criteriaValue) {
        return false;
    }//from   ww w .  j ava 2  s.  c  o  m
    if (dataValue == null && criteriaValue == null) {
        return false;
    }
    if (dataValue == null) {
        return false;
    }
    if (criteriaValue instanceof Comparable) {
        Comparable comp1 = (Comparable) dataValue;
        Comparable comp2 = (Comparable) criteriaValue;
        if (comp1.compareTo(comp2) > 0) {
            return true;
        }
        return false;
    }
    throw new IllegalArgumentException("The values are not comparable " + criteriaValue);
}

From source file:org.jboss.dashboard.commons.comparator.ComparatorUtils.java

/**
 * Compares two comparable objects./*from   ww w  . ja  v  a 2s  .  c o  m*/
 *
 * @param ordering: 1=ascending, -1=descending
 */
public static int compare(Comparable o1, Comparable o2, int ordering) {
    // Compare
    int comp = 0;
    if (o1 == null && o2 != null)
        comp = -1;
    else if (o1 != null && o2 == null)
        comp = 1;
    else if (o1 == null && o2 == null)
        comp = 0;
    else {
        comp = o1.compareTo(o2);
        if (comp > 0)
            comp = 1;
        else if (comp < 0)
            comp = -1;
    }

    // Resolve ordering
    if (ordering == -1)
        return comp * ordering;
    return comp;
}