Example usage for java.util Comparator compare

List of usage examples for java.util Comparator compare

Introduction

In this page you can find the example usage for java.util Comparator compare.

Prototype

int compare(T o1, T o2);

Source Link

Document

Compares its two arguments for order.

Usage

From source file:Main.java

private static <T> void sortRecursive(List<T> list, List<T> unsorted, Comparator<? super T> comp, int begin,
        int end) {
    //A sorting section with no elements or one element needs not be sorted
    if (begin == end || begin == end - 1) {
        return;/*from  w  w w.  jav a  2s  . co m*/
    }
    //A sorting section with two elements can be sorted trivially
    //Swap if the larger element precedes the smaller
    if (begin == end - 2) {
        T a = list.get(begin);
        T b = list.get(begin + 1);
        if (comp.compare(a, b) > 0) {
            list.set(begin, b);
            list.set(begin + 1, a);
        }
        return;
    }
    //Split the array into half
    int split = (end + begin) / 2;
    //Sort each half of the array
    sortRecursive(list, unsorted, comp, begin, split);
    sortRecursive(list, unsorted, comp, split, end);
    int indexCounter = 0;
    int counterLeft = begin;
    int counterRight = split;
    boolean complete = false;
    //Merging the arrays
    //Fills in the indexes; one plots the final position of
    //the element to the element's index; the other plots the
    //element at the given index to its final position
    while (!complete) {
        T a = unsorted.get(counterLeft);
        T b = unsorted.get(counterRight);
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            list.set(begin + indexCounter++, b);
            counterRight++;
            if (counterRight >= end) {
                //Copy the remaining values
                while (counterLeft < split) {
                    list.set(begin + indexCounter++, unsorted.get(counterLeft++));
                }
                complete = true;
            }
        } else {
            list.set(begin + indexCounter++, a);
            counterLeft++;
            if (counterLeft >= split) {
                //Copy the remaining values
                while (counterRight < end) {
                    list.set(begin + indexCounter++, unsorted.get(counterRight++));
                }
                complete = true;
            }
        }
    }
    //The list should now be sorted
}

From source file:org.rhq.test.arquillian.FakeServerInventory.java

private static void findResources(Resource root, Resource template, Set<Resource> result,
        Comparator<Resource> comparator) {
    if (root == null)
        return;//from w  w w.j  av a2  s.  c  o  m
    if (comparator.compare(root, template) == 0) {
        result.add(root);
    } else {
        for (Resource child : root.getChildResources()) {
            findResources(child, template, result, comparator);
        }
    }
}

From source file:Main.java

private static <T> void eqSort(List<T> list, int lo0, int hi0, Comparator<? super T> c) {
    int lo = lo0;
    int hi = hi0;
    if ((hi - lo) <= 3) {
        eqBrute(list, lo, hi, c);/*  w  w  w . j  a  v  a 2 s . c  o m*/
        return;
    } /* * Pick a pivot and move it out of the way */
    T e, pivot = list.get((lo + hi) / 2);
    list.set((lo + hi) / 2, list.get(hi));
    list.set(hi, pivot);
    while (lo < hi) { /* * Search forward from a[lo] until an element is found that * is greater than the pivot or lo >= hi */
        while (c.compare(list.get(lo), pivot) <= 0 && lo < hi) {
            lo++;
        } /* * Search backward from a[hi] until element is found that * is less than the pivot, or hi <= lo */
        while (c.compare(pivot, list.get(hi)) <= 0 && lo < hi) {
            hi--;
        } /* * Swap elements a[lo] and a[hi] */
        if (lo < hi) {
            e = list.get(lo);
            list.set(lo, list.get(hi));
            list.set(hi, e);
        }
    } /* * Put the median in the "center" of the list */
    list.set(hi0, list.get(hi));
    list.set(hi,
            pivot); /* * Recursive calls, elements a[lo0] to a[lo-1] are less than or * equal to pivot, elements a[hi+1] to a[hi0] are greater than * pivot. */
    eqSort(list, lo0, lo - 1, c);
    eqSort(list, hi + 1, hi0, c);
}

From source file:org.rhq.test.arquillian.FakeServerInventory.java

private static Resource findResource(Resource root, Resource template, Comparator<Resource> comparator) {
    if (root == null)
        return null;
    if (comparator.compare(root, template) == 0) {
        return root;
    } else {//from  w w  w  .j  a  v a  2  s  .c o  m
        for (Resource child : root.getChildResources()) {
            Resource found = findResource(child, template, comparator);
            if (found != null)
                return found;
        }
    }

    return null;
}

From source file:es.logongas.ix3.web.controllers.endpoint.EndPoint.java

public static EndPoint getBestEndPoint(List<EndPoint> endPoints, String path, String method) {
    Comparator<String> comparatorPath = antPathMatcher.getPatternComparator(path);
    Comparator<String> comparatorMethod = antPathMatcher.getPatternComparator(method);

    EndPoint bestEndPoint = null;//from ww w. j  a  v  a  2 s . c o m

    for (EndPoint newEndPoint : endPoints) {
        if (bestEndPoint == null) {
            //Si no hay ninguno seguro que este es el mejor endPoint
            bestEndPoint = newEndPoint;
        } else {
            int valuePath = comparatorPath.compare(bestEndPoint.getPath(), newEndPoint.getPath());
            if (valuePath < 0) {
                //El mejor sigue siendo el actual
                bestEndPoint = bestEndPoint;
            } else if (valuePath > 0) {
                bestEndPoint = newEndPoint;
            } else if (valuePath == 0) {
                //Si son iguales por path veamos ahora por mtodo.

                int valueMethod = comparatorMethod.compare(bestEndPoint.getMethod(), newEndPoint.getMethod());
                if ((bestEndPoint.getMethod() == null) && (newEndPoint.getMethod() == null)) {
                    throw new RuntimeException(
                            "EndPoits repetidos:" + bestEndPoint.toString() + " y " + newEndPoint.toString());
                } else if ((bestEndPoint.getMethod() == null) && (newEndPoint.getMethod() != null)) {
                    bestEndPoint = newEndPoint;
                } else if ((bestEndPoint.getMethod() != null) && (newEndPoint.getMethod() == null)) {
                    bestEndPoint = bestEndPoint;
                } else if ((bestEndPoint.getMethod() != null) && (newEndPoint.getMethod() != null)) {

                    if (bestEndPoint.getMethod().equals(newEndPoint.getMethod())) {
                        throw new RuntimeException("EndPoits repetidos:" + bestEndPoint.toString() + " y "
                                + newEndPoint.toString());
                    } else if (bestEndPoint.getMethod().equals("*")) {
                        bestEndPoint = newEndPoint;
                    } else if (newEndPoint.getMethod().equals("*")) {
                        bestEndPoint = bestEndPoint;
                    } else {
                        throw new RuntimeException(
                                "Error de logioca:" + bestEndPoint.toString() + " y " + newEndPoint.toString());
                    }

                } else {
                    throw new RuntimeException("Error de logioca:" + valueMethod);
                }

            } else {
                throw new RuntimeException("Error de logioca:" + valuePath);
            }
        }
    }

    return bestEndPoint;
}

From source file:Main.java

private static <T> void sortRecursive(T[] array, Comparator<T> comp, int begin, int end) {
    //A sorting section with no elements or one element needs not be sorted
    if (begin == end || begin == end - 1) {
        return;//from w w w . j a v a2 s  .  com
    }
    //A sorting section with two elements can be sorted trivially
    //Swap if the larger element precedes the smaller
    if (begin == end - 2) {
        T a = array[begin];
        T b = array[begin + 1];
        if (comp.compare(a, b) > 0) {
            array[begin] = b;
            array[begin + 1] = a;
        }
        return;
    }
    //Split the array into half
    int split = (end + begin) / 2;
    //Sort each half of the array
    sortRecursive(array, comp, begin, split);
    sortRecursive(array, comp, split, end);
    //Create indexes for storing sorting data
    int[] indexA = new int[end - begin];
    int[] indexB = new int[end - begin];
    int indexCounter = 0;
    int counterLeft = begin;
    int counterRight = split;
    boolean complete = false;
    //Merging the arrays
    //Fills in the indexes; one plots the final position of
    //the element to the element's index; the other plots the
    //element at the given index to its final position
    while (!complete) {
        T a = array[counterLeft];
        T b = array[counterRight];
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            indexA[indexCounter] = counterRight - begin;
            indexB[counterRight++ - begin] = indexCounter++;
            if (counterRight >= end) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterLeft < split) {
                    indexA[indexCounter] = counterLeft - begin;
                    indexB[counterLeft++ - begin] = indexCounter++;
                }
                complete = true;
            }
        } else {
            indexA[indexCounter] = counterLeft - begin;
            indexB[counterLeft++ - begin] = indexCounter++;
            if (counterLeft >= split) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterRight < end) {
                    indexA[indexCounter] = counterRight - begin;
                    indexB[counterRight++ - begin] = indexCounter++;
                }
                complete = true;
            }
        }
    }
    //Swaps the elements in the array such that the array is sorted
    for (int i = 0; i < end - begin; i++) {
        int target = indexA[i];
        T a = array[begin + target];
        T b = array[begin + i];
        array[begin + target] = b;
        array[begin + i] = a;
        int ai = indexB[target];
        int bi = indexB[i];
        indexB[target] = bi;
        indexB[i] = ai;
        indexA[bi] = target;
    }
    //The array should now be sorted
}

From source file:Main.java

private static <T> void sortRecursive(List<T> array, Comparator<? super T> comp, int begin, int end) {
    //A sorting section with no elements or one element needs not be sorted
    if (begin == end || begin == end - 1) {
        return;//from   w ww. j  ava2 s  . c o  m
    }
    //A sorting section with two elements can be sorted trivially
    //Swap if the larger element precedes the smaller
    if (begin == end - 2) {
        T a = array.get(begin);
        T b = array.get(begin + 1);
        if (comp.compare(a, b) > 0) {
            array.set(begin, b);
            array.set(begin + 1, a);
        }
        return;
    }
    //Split the array into half
    int split = (end + begin) / 2;
    //Sort each half of the array
    sortRecursive(array, comp, begin, split);
    sortRecursive(array, comp, split, end);
    //Create indexes for storing sorting data
    int[] indexA = new int[end - begin];
    int[] indexB = new int[end - begin];
    int indexCounter = 0;
    int counterLeft = begin;
    int counterRight = split;
    boolean complete = false;
    //Merging the arrays
    //Fills in the indexes; one plots the final position of
    //the element to the element's index; the other plots the
    //element at the given index to its final position
    while (!complete) {
        T a = array.get(counterLeft);
        T b = array.get(counterRight);
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            indexA[indexCounter] = counterRight - begin;
            indexB[counterRight++ - begin] = indexCounter++;
            if (counterRight >= end) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterLeft < split) {
                    indexA[indexCounter] = counterLeft - begin;
                    indexB[counterLeft++ - begin] = indexCounter++;
                }
                complete = true;
            }
        } else {
            indexA[indexCounter] = counterLeft - begin;
            indexB[counterLeft++ - begin] = indexCounter++;
            if (counterLeft >= split) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterRight < end) {
                    indexA[indexCounter] = counterRight - begin;
                    indexB[counterRight++ - begin] = indexCounter++;
                }
                complete = true;
            }
        }
    }
    //Swaps the elements in the array such that the array is sorted
    for (int i = 0; i < end - begin; i++) {
        int target = indexA[i];
        T a = array.get(begin + target);
        T b = array.get(begin + i);
        array.set(begin + target, b);
        array.set(begin + i, a);
        int ai = indexB[target];
        int bi = indexB[i];
        indexB[target] = bi;
        indexB[i] = ai;
        indexA[bi] = target;
    }
    //The array should now be sorted
}

From source file:Main.java

public static <E> E getSmallestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallestNotNull(comparator, (List<? extends E>) c);
    }/*from w w  w  .jav a  2 s.c o m*/

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) < 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

public static <E> E getGreatestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatestNotNull(comparator, (List<? extends E>) c);
    }//from www .  j a  va 2  s.c om

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) > 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

/**
 * Finds the index where an object should be inserted in a collection based
 * on the following algorithm: if the list is empty the insertion index is 0 <br/>
 * If equal element exists, the insertion index is the index of the existing
 * element+1. If multiple equal elements exist, the insertion index is after
 * the elements./*ww w. j  ava  2s .  c o  m*/
 * 
 * @param <E>
 *            type of the object
 * @param list
 *            list where the element should be inserted.
 * @param value
 *            element that should be inserted.
 * @param comparator
 *            comparator used to compare objects
 * @return index (place) where the element should be inserted.
 */
public static <E> int findInsertionIndex(List<E> list, E object, Comparator<E> comparator) {
    // call binary search
    int binarySearchResult = Collections.binarySearch(list, object, comparator);

    int index;
    if (binarySearchResult < 0) {
        // if the result is negative turn it to positive and decrease it by
        // one (see binarySearch doc)
        index = Math.abs(binarySearchResult) - 1;
    } else {
        // if the result is positive, increase it by one
        index = binarySearchResult + 1;

        // if there are multiple equal elements iterate to find the latest
        while (index < list.size() && comparator.compare(list.get(index), object) == 0) {
            index++;
        }
    }

    return index;
}