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.clican.pluto.common.util.SearchUtils.java

/**
 * This method implements the binary search algorithm.
 * the list element and key should implement the comparable interface.
 * @param list//from   w ww. ja va  2s.  co  m
 *        must be sorted before, in ascendent order.
 * @param key
 *        the object that will be compared.
 * @param before
 *        if there is not a the same object, get the previous or next object
 * @return
 */
public static <V extends Comparable<V>> V binarySearch(List<V> list, V key, boolean before) {
    if (list == null || list.size() == 0) {
        return null;
    }
    int low = 0;
    int high = list.size() - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        Comparable<V> midVal = list.get(mid);
        midVal.compareTo(key);
        int cmp = midVal.compareTo(key);

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return list.get(mid); // key found
    }
    if (before) {
        int index = high;
        if (index < 0) {
            return null;
        }
        return list.get(index);
    } else {
        int index = low;
        if (index < 0) {
            return null;
        }
        if (index >= list.size()) {
            return null;
        }
        return list.get(index);
    }

}

From source file:Main.java

static void quicksort(Comparable[] a, int low, int high) {
    int CUTOFF = 10;
    if (low + CUTOFF > high)
        insertionSort(a, low, high);//from   www. ja v  a 2s .c o m
    else {
        int middle = (low + high) / 2;
        if (a[middle].compareTo(a[low]) < 0)
            swapReferences(a, low, middle);
        if (a[high].compareTo(a[low]) < 0)
            swapReferences(a, low, high);
        if (a[high].compareTo(a[middle]) < 0)
            swapReferences(a, middle, high);

        swapReferences(a, middle, high - 1);
        Comparable pivot = a[high - 1];

        int i, j;
        for (i = low, j = high - 1;;) {
            while (a[++i].compareTo(pivot) < 0)
                ;
            while (pivot.compareTo(a[--j]) < 0)
                ;
            if (i >= j)
                break;
            swapReferences(a, i, j);
        }
        swapReferences(a, i, high - 1);

        quicksort(a, low, i - 1);
        quicksort(a, i + 1, high);
    }
}

From source file:org.sonar.plugins.core.sensors.AlertUtils.java

private static boolean doesReachThresholds(Comparable measureValue, Comparable criteriaValue, Alert alert) {
    int comparison = measureValue.compareTo(criteriaValue);
    return !(isNotEquals(comparison, alert) || isGreater(comparison, alert) || isSmaller(comparison, alert)
            || isEquals(comparison, alert));
}

From source file:Main.java

public static <T> List<T> search(final String str, Collection<? extends T> coll, Comparable<? super T> comp) {
    if (comp == null)
        return null;
    List<T> list = new ArrayList<T>();
    Iterator<? extends T> i = coll.iterator();
    while (i.hasNext()) {
        T next = i.next();// w  ww.j a v a 2 s.  c om
        if (comp.compareTo(next) > 0) {
            list.add(next);
        }
    }
    return list;
}

From source file:org.sonar.server.computation.qualitygate.ConditionEvaluator.java

private static boolean doesReachThresholds(Comparable measureValue, Comparable criteriaValue,
        Condition condition) {/*from   ww w  .  j a v  a  2s.  co  m*/
    int comparison = measureValue.compareTo(criteriaValue);
    switch (condition.getOperator()) {
    case EQUALS:
        return comparison == 0;
    case NOT_EQUALS:
        return comparison != 0;
    case GREATER_THAN:
        return comparison > 0;
    case LESS_THAN:
        return comparison < 0;
    default:
        throw new IllegalArgumentException(String.format("Unsupported operator '%s'", condition.getOperator()));
    }
}

From source file:org.sonar.batch.qualitygate.ConditionUtils.java

private static boolean doesReachThresholds(Comparable measureValue, Comparable criteriaValue,
        ResolvedCondition condition) {/*from w  w  w .  ja v a 2 s.  c o  m*/
    int comparison = measureValue.compareTo(criteriaValue);
    return !(isNotEquals(comparison, condition) || isGreater(comparison, condition)
            || isSmaller(comparison, condition) || isEquals(comparison, condition));
}

From source file:Main.java

public static boolean containsAll(Collection a, Collection b) {
    // fast paths
    if (a == b)//from   www.  java 2  s .c  o  m
        return true;
    if (b.size() == 0)
        return true;
    if (a.size() < b.size())
        return false;

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                    ao = (Comparable) ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else {
                    return false;
                }
            }
        } else if (bbc.equals(aac)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = bi.next();
                    ao = ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else {
                    return false;
                }
            }
        }
    }
    return a.containsAll(b);
}

From source file:Main.java

/**
 * @param a/*from   w  w  w  . j a  v a2 s .  co  m*/
 * @param b
 * @return
 */
public static boolean containsSome(Collection a, Collection b) {
    // fast paths
    if (a.size() == 0 || b.size() == 0)
        return false;
    if (a == b)
        return true; // must test after size test.

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                } else {
                    return true;
                }
            }
        } else if (bbc.equals(a)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = bi.next();
                } else {
                    return true;
                }
            }
        }
    }
    for (Iterator it = a.iterator(); it.hasNext();) {
        if (b.contains(it.next()))
            return true;
    }
    return false;
}

From source file:Main.java

/**
 * Similar to java.lang.Comparable.compareTo, but can handle 2 different
 * data types.//from  ww w  . jav  a 2 s.  c  om
 *
 * @param val1
 * @param val2
 *
 */
static int compare(Comparable val1, Comparable val2) {
    if (val1.getClass() == val2.getClass()) {
        //both the same type, no conversion is necessary.
        return val1.compareTo(val2);
    }

    //find most appropriate class
    Object[] objects = new Object[] { val1, val2 };
    Class bestClass = bestClass(objects);

    if (bestClass != val1.getClass()) {
        val1 = (Comparable) convert(val1, bestClass);
    }

    if (bestClass != val2.getClass()) {
        val2 = (Comparable) convert(val2, bestClass);
    }

    //now do the comparison
    return val1.compareTo(val2);
}

From source file:de.openali.odysseus.chart.framework.model.data.DataRange.java

@Deprecated
public static <T> DataRange<T> create(final T min, final T max) {
    if (min instanceof Number && max instanceof Number) {
        final Double minNum = ((Number) min).doubleValue();
        final Double maxNum = ((Number) max).doubleValue();

        // Beide gleich => dataRange automatisch so anpassen, dass der Wert
        // in der Intervallmitte liegt
        if (minNum.compareTo(maxNum) == 0) {
            final double doubleValue = minNum.doubleValue();
            // falls != 0 werden einfach 10% addiert oder subtrahiert
            if (doubleValue != 0) {
                final T minExpanded = (T) new Double(doubleValue - doubleValue * 0.1);
                final T maxExpanded = (T) new Double(doubleValue + doubleValue * 0.1);
                return new DataRange<>(minExpanded, maxExpanded);
            }/*  w  w w. j  a v  a 2 s .  c  o m*/
            // falls == 0 wird 1 addiert oder subtrahiert
            else {
                final T min_1 = (T) new Double(doubleValue - 1);
                final T max_1 = (T) new Double(doubleValue + 1);
                return new DataRange<>(min_1, max_1);
            }
        }

        if (minNum.compareTo(maxNum) > 0)
            return new DataRange<>(max, min);
        else
            return new DataRange<>(min, max);

    } else if (min instanceof Comparable && max instanceof Comparable
            && (min.getClass().isInstance(max) || max.getClass().isInstance(min))) {
        // FIXME: this is nonsense! REMOVE
        final Comparable<Comparable<?>> minComp = (Comparable<Comparable<?>>) min;
        final Comparable<?> maxComp = (Comparable<?>) max;
        if (minComp.compareTo(maxComp) == 0) {
            // kann leider nicht automatisch angepasst werden; das muss
            // jemand anders abfangen
        }

        if (minComp.compareTo(maxComp) > 0)
            return new DataRange<>(max, min);
        else
            return new DataRange<>(min, max);

    }
    /*
     * das wre dann der ungnstigste Fall: nicht vergleichbar und nicht numerisch TODO: berlegen, ob dieser Fall
     * berhaupt zugelassen werden soll; alternativ sollte eine InvalidRangeIntervalObjectsException
     */
    else {
        return new DataRange<>(min, max);
    }
}