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.pureinfo.srm.reports.table.data.sci.SCIBySchoolStatistic.java

public static void main(String[] args) throws PureException {
    IProductMgr mgr = (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class);
    IStatement stat = mgr.createQuery(//from  w  ww. j  av a 2  s. co m
            "select count({this.id}) _NUM, {this.englishScience} AS _SUB from {this} group by _SUB", 0);
    IObjects nums = stat.executeQuery(false);
    DolphinObject num = null;
    Map map = new HashedMap();
    while ((num = nums.next()) != null) {
        String subest = num.getStrProperty("_SUB");
        if (subest == null || subest.trim().length() == 0)
            continue;
        String[] subs = subest.split(";");
        int nNum = num.getIntProperty("_NUM", 0);
        for (int i = 0; i < subs.length; i++) {
            String sSub = subs[i].trim();
            Integer odValue = (Integer) map.get(sSub);
            int sum = odValue == null ? nNum : (nNum + odValue.intValue());
            map.put(sSub, new Integer(sum));
        }
    }
    List l = new ArrayList(map.size());

    for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
        Map.Entry en = (Map.Entry) iter.next();
        l.add(new Object[] { en.getKey(), en.getValue() });
    }
    Collections.sort(l, new Comparator() {

        public int compare(Object _sO1, Object _sO2) {
            Object[] arr1 = (Object[]) _sO1;
            Object[] arr2 = (Object[]) _sO2;
            Comparable s1 = (Comparable) arr1[1];
            Comparable s2 = (Comparable) arr2[01];
            return s1.compareTo(s2);
        }
    });
    for (Iterator iter = l.iterator(); iter.hasNext();) {
        Object[] arr = (Object[]) iter.next();
        System.out.println(arr[0] + " = " + arr[1]);
    }

}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean equalTo(Comparable a, Comparable b) {
    return a.compareTo(b) == 0;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean greaterThan(Comparable a, Comparable b) {
    return a.compareTo(b) < 0;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean lessThan(Comparable a, Comparable b) {
    return a.compareTo(b) < 0;
}

From source file:Main.java

public static int compare(Comparable arg1, Comparable arg2) {
    return arg1.compareTo(arg2);
}

From source file:Main.java

private static void insertionSort(Comparable[] a, int low, int high) {
    for (int p = low + 1; p <= high; p++) {
        Comparable tmp = a[p];
        int j;// w  w  w  .  ja  v a 2  s  .  c o  m

        for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
            a[j] = a[j - 1];
        a[j] = tmp;
    }
}

From source file:Main.java

public static int binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        Comparable midVal = (Comparable) a[mid];
        int cmp = midVal.compareTo(key);

        if (cmp < 0) {
            low = mid + 1;/*ww w. j  av  a  2s  .  co m*/
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            return mid; // key found
        }
    }
    return -(low + 1); // key not found.
}

From source file:org.mifos.framework.util.helpers.NumberUtils.java

public static boolean isBetween(Comparable start, Comparable end, Comparable value) {
    return start.compareTo(value) <= 0 && end.compareTo(value) >= 0;
}

From source file:org.openvpms.web.component.im.util.ObjectHelper.java

/**
 * Compares two objects for equality, where either one or both
 * objects may be <tt>null</tt>.
 * This method will use {@link Comparable#compareTo} in preference
 * of {@link Object#equals} if both instances implement {@link Comparable}
 * and <tt>obj2</tt> is assignable from <tt>obj1</tt>.
 * This is primarily to check equality of {@link BigDecimal}s.
 *
 * @param obj1//from   w w w.j  a v  a 2s. co m
 * @param obj2
 * @return <tt>true</tt> if the objects are equal, otherwise <tt>false</tt>
 */
public static boolean equals(Object obj1, Object obj2) {
    if (obj1 instanceof Comparable && obj2 != null && obj1.getClass().isAssignableFrom(obj2.getClass())) {
        Comparable<Object> c1 = (Comparable<Object>) obj1;
        Comparable<Object> c2 = (Comparable<Object>) obj2;
        return (c1.compareTo(c2) == 0);
    }
    return ObjectUtils.equals(obj1, obj2);
}

From source file:Main.java

/**
 * Inserts into an ascending sorted list an element.
 *
 * Preconditions: The element has to implement the {@code Comparable}
 * interface and the list have to be sorted ascending. Both conditions will
 * not be checked: At runtime a class cast exception will be thrown
 * if the element does not implement the comparable interface and and if the
 * list is not sorted, the element can't be insert sorted.
 *
 * @param <T>     element type/*from   w  ww .j a v  a2 s . co  m*/
 * @param list
 * @param element
 */
@SuppressWarnings("unchecked")
public static <T> void binaryInsert(LinkedList<? super T> list, T element) {
    if (list == null) {
        throw new NullPointerException("list == null");
    }

    if (element == null) {
        throw new NullPointerException("element == null");
    }

    boolean isComparable = element instanceof Comparable<?>;

    if (!isComparable) {
        throw new IllegalArgumentException("Not a comparable: " + element);
    }

    int size = list.size();
    int low = 0;
    int high = size - 1;
    int index = size;
    int cmp = 1;

    while ((low <= high) && (cmp > 0)) {
        int mid = (low + high) >>> 1;
        Comparable<? super T> midVal = (Comparable<? super T>) list.get(mid);

        cmp = midVal.compareTo(element);

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        }
    }

    for (int i = low; (i >= 0) && (i < size) && (index == size); i++) {
        Comparable<? super T> elt = (Comparable<? super T>) list.get(i);

        if (elt.compareTo(element) >= 0) {
            index = i;
        }
    }

    list.add(index, element);
}