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:org.apache.hadoop.hive.ql.optimizer.calcite.druid.DruidIntervalUtils.java

protected static boolean mergeable(Range range1, Range range2) {
    Comparable x1 = range1.upperEndpoint();
    Comparable x2 = range2.lowerEndpoint();
    int compare = x1.compareTo(x2);
    return compare > 0 || (compare == 0 && range1.upperBoundType() == BoundType.CLOSED
            && range2.lowerBoundType() == BoundType.CLOSED);
}

From source file:com.softmotions.commons.bean.BeanUtils.java

/**
 * Compares the given values.// ww  w  .ja  va  2 s.  c  o  m
 *
 * @param a The object to compare with object b.
 * @param b The object to compare with object a.
 * @return true if the objects are equal or are both null.
 */
public static int compare(Comparable a, Comparable b) {
    if ((a == null) || (b == null)) {
        if (a == b) {
            return 0;
        }
        if (a == null) {
            return -1;
        }
        return 1;
    }
    return a.compareTo(b);
}

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

/**
 * ???????source???//from w w  w.  j  av  a  2 s .c om
 * 
 * @param <T>
 *            
 * @param <K>
 *            
 * @param source
 *            ?
 * @param comparablePropName
 *            ?
 * @param comp
 *            
 * @return
 */
@SuppressWarnings("unchecked")
public static <T, K> T binarySearch(List<T> source, String comparablePropName, Comparable<K> comp) {
    if (source == null || source.size() == 0) {
        return null;
    }
    int first = 0, upto = source.size();
    while (first < upto) {
        int midIdx = (first + upto) / 2;// Compute mid point.
        T mid = source.get(midIdx);
        try {
            K value = (K) PropertyUtils.getProperty(mid, comparablePropName);
            if (comp.compareTo(value) < 0) {
                upto = midIdx; // repeat search in bottom half.
            } else if (comp.compareTo(value) > 0) {
                first = midIdx + 1; // Repeat search in top half.
            } else {
                return source.get(midIdx);// Found it. return position
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return null; // Failed to find key
}

From source file:org.kuali.kfs.gl.businessobject.LedgerSummaryDetailLine.java

/**
 * @return a standard comparator for comparing NightlyOutPendingEntryLedgerSummaryDetailLine objects
 *///ww  w  . ja v a2 s .  c  om
public static Comparator<LedgerSummaryDetailLine> getStandardComparator() {
    return new Comparator<LedgerSummaryDetailLine>() {

        /**
         * Compares two NightlyOutPendingEntryLedgerSummaryDetailLine objects
         * 
         * @param detail1 the first NightlyOutPendingEntryLedgerSummaryDetailLine object
         * @param detail2 the second NightlyOutPendingEntryLedgerSummaryDetailLine other
         * @return the standard 0 for equals, greater than 0 for greater than, less than 0 for less than
         */
        public int compare(LedgerSummaryDetailLine detail1, LedgerSummaryDetailLine detail2) {
            int comp = 0;
            comp = nullSafeCompare(detail1.getFinancialBalanceTypeCode(),
                    detail2.getFinancialBalanceTypeCode());

            if (comp == 0) {
                comp = nullSafeCompare(detail1.getFinancialSystemOriginationCode(),
                        detail2.getFinancialSystemOriginationCode());
            }

            if (comp == 0) {
                comp = nullSafeCompare(detail1.getUniversityFiscalYear(), detail2.getUniversityFiscalYear());
            }

            if (comp == 0) {
                comp = nullSafeCompare(detail1.getUniversityAccountPeriodCode(),
                        detail2.getUniversityAccountPeriodCode());
            }

            return comp;
        }

        /**
         * Checks for nulls in the two comparables before calling the compare. If one is null and not the other, the null is
         * considered less. If both are null they are considered equal.
         * 
         * @param o1 object to compare
         * @param o2 object to compare o1 to
         * @return -1 for less, 0 for equal, 1 for greater
         */
        protected int nullSafeCompare(Comparable o1, Comparable o2) {
            if (o1 == null && o2 != null) {
                return -1;
            }

            if (o1 != null && o2 == null) {
                return 1;
            }

            if (o1 == null && o2 == null) {
                return 0;
            }

            return o1.compareTo(o2);
        }
    };
}

From source file:org.kuali.rice.krad.datadictionary.validation.ValidationUtils.java

/**
 * checks whether the provided value is greater than the limit given
 *
 * @param value - the object to check//from   www  .j a v  a  2 s  .  c o  m
 * @param limit - the limit to use
 * @param <T>
 * @return one of the values in {@link  Result}
 */
public static <T> Result isGreaterThan(T value, Comparable<T> limit) {
    return limit == null ? Result.UNDEFINED : (limit.compareTo(value) < 0 ? Result.VALID : Result.INVALID);
}

From source file:org.kuali.rice.krad.datadictionary.validation.ValidationUtils.java

/**
 * checks whether the provided value is greater than or equal to the limit given
 *
 * @param value - the object to check//w w  w .  jav  a2 s.co m
 * @param limit - the limit to use
 * @param <T>
 * @return one of the values in {@link  Result}
 */
public static <T> Result isGreaterThanOrEqual(T value, Comparable<T> limit) {
    return limit == null ? Result.UNDEFINED : (limit.compareTo(value) <= 0 ? Result.VALID : Result.INVALID);
}

From source file:org.kuali.rice.krad.datadictionary.validation.ValidationUtils.java

/**
 * checks whether the provided value is less than the limit given
 *
 * @param value - the object to check// w ww.  j a  v a2s . co m
 * @param limit - the limit to use
 * @param <T>
 * @return one of the values in {@link  Result}
 */
public static <T> Result isLessThan(T value, Comparable<T> limit) {
    return limit == null ? Result.UNDEFINED : (limit.compareTo(value) > 0 ? Result.VALID : Result.INVALID);
}

From source file:org.kuali.rice.krad.datadictionary.validation.ValidationUtils.java

/**
 * checks whether the provided value is greater than the limit given
 *
 * @param value - the object to check//ww  w  . ja  va 2  s .c  o  m
 * @param limit - the limit to use
 * @param <T>
 * @return one of the values in {@link  Result}
 */
public static <T> Result isLessThanOrEqual(T value, Comparable<T> limit) {
    return limit == null ? Result.UNDEFINED : (limit.compareTo(value) >= 0 ? Result.VALID : Result.INVALID);
}

From source file:RandomChooser.java

public static <T> void gt(Comparable<T> c1, T c2) {
    if (c1.compareTo(c2) > 0)
        return;//from   w w w  .j a v  a2 s . c o m
    throw new IllegalArgumentException(c1 + GT + c2);
}

From source file:RandomChooser.java

public static <T> void lt(Comparable<T> c1, T c2) {
    if (c1.compareTo(c2) < 0)
        return;//from  w  w  w. j a v a 2 s . c om
    throw new IllegalArgumentException(c1 + LT + c2);
}