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:com.shenit.commons.utils.ValidationUtils.java

/**
 * ??// w w w  .  ja  v a 2 s  .com
 * @param validator 
 * @param base 
 * @param vals 
 * @return
 */
@SafeVarargs
public static <T> boolean all(Comparator validator, T base, T... vals) {
    for (T val : vals) {
        if (!validator.compare(val, base))
            return false;
    }
    return true;
}

From source file:com.shenit.commons.utils.ValidationUtils.java

/**
 * Check whether any values compare to the base value is true.
 * @param comparator Compare logic// www  .ja v  a2 s  .  co m
 * @param base
 * @param vals
 * @return
 */
@SafeVarargs
public static <T> boolean any(Comparator comparator, T base, T... vals) {
    for (T val : vals) {
        if (comparator.compare(val, base))
            return true;
    }
    return false;
}

From source file:net.darkmist.clf.Util.java

public static <T, U extends Collection<T>> U merge(Iterator<T> ai, Iterator<T> bi, U dest,
        Comparator<T> comparator) {
    T af, bf; // first elements

    // get the initial stat of the iterators
    if (!ai.hasNext())
        return append(dest, bi);
    if (!bi.hasNext())
        return append(dest, ai);
    af = ai.next();/*  w w w  . ja v  a  2  s.c o  m*/
    bf = bi.next();

    while (true) {
        if (comparator.compare(af, bf) <= 0) {
            checkedAdd(dest, af);
            if (!ai.hasNext()) { // nothing left in a
                checkedAdd(dest, bf);
                return append(dest, bi);
            }
            af = ai.next();
        } else {
            checkedAdd(dest, bf);
            if (!bi.hasNext()) { // nothing left in b
                checkedAdd(dest, af);
                return append(dest, ai);
            }
            bf = bi.next();
        }
    }
}

From source file:Main.java

public static <T> int compareAsSet(@Nonnull Comparator<? super T> elementComparator,
        @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> set1 = toSortedSet(elementComparator, list1);
    SortedSet<? extends T> set2 = toSortedSet(elementComparator, list2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }//from  w  w  w .  j  av  a 2s.c om
    return 0;
}

From source file:org.apache.cassandra.thrift.ThriftValidation.java

public static void validateRange(String keyspace, ColumnParent column_parent, SliceRange range)
        throws InvalidRequestException {
    AbstractType comparator = ColumnFamily.getComparatorFor(keyspace, column_parent.column_family,
            column_parent.super_column);
    try {//from w w w. j ava  2  s .c  om
        comparator.validate(range.start);
        comparator.validate(range.finish);
    } catch (MarshalException e) {
        throw new InvalidRequestException(e.getMessage());
    }

    if (range.count < 0)
        throw new InvalidRequestException("get_slice requires non-negative count");

    Comparator<byte[]> orderedComparator = range.isReversed() ? comparator.getReverseComparator() : comparator;
    if (range.start.length > 0 && range.finish.length > 0
            && orderedComparator.compare(range.start, range.finish) > 0) {
        throw new InvalidRequestException("range finish must come after start in the order of traversal");
    }
}

From source file:Main.java

public static <E> boolean addToSortedList(List<E> list, E item, Comparator<? super E> comparator,
        BiPredicate<? super E, ? super E> distincter) {
    int addIndex = list.size();
    boolean foundAddIndex = false;
    ListIterator<E> iter = list.listIterator();
    while (iter.hasNext()) {
        int currIndex = iter.nextIndex();
        E currItem = iter.next();//from  ww  w . j  ava  2  s  . c om
        if (distincter != null && distincter.test(currItem, item)) {
            return false;
        }
        if (!foundAddIndex) {
            int comparison = comparator.compare(currItem, item);
            if (comparison > 0) {
                addIndex = currIndex;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            } else if (comparison == 0) {
                addIndex = currIndex + 1;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            }
        }
    }
    list.add(addIndex, item);
    return true;
}

From source file:Main.java

private static <T> void sortRecursive(T[] array, 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;//  ww  w . ja v  a  2 s.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 = 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, unsorted, comp, begin, split);
    sortRecursive(array, 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[counterLeft];
        T b = unsorted[counterRight];
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            array[begin + indexCounter++] = b;
            counterRight++;
            if (counterRight >= end) {
                //Copy the remaining values
                System.arraycopy(unsorted, counterLeft, array, begin + indexCounter,
                        end - begin - indexCounter);
                complete = true;
            }
        } else {
            array[begin + indexCounter++] = a;
            counterLeft++;
            if (counterLeft >= split) {
                //Copy the remaining values
                System.arraycopy(unsorted, counterRight, array, begin + indexCounter,
                        end - begin - indexCounter);
                complete = true;
            }
        }
    }
    //The array should now be sorted
}

From source file:Main.java

public static <E> E getSmallest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/*from  w  w  w. j ava 2s  .c o m*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallest(comparator, (List<? extends E>) c);
    }

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

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

    return result;
}

From source file:Main.java

public static <E> E getGreatest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }//w  w w.  j  a  va  2  s. c o  m

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatest(comparator, (List<? extends E>) c);
    }

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

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

    return result;
}

From source file:com.shenit.commons.utils.ValidationUtils.java

/**
 * None value of values compare to base is true. 
 * @param comparator Comparator logic to compare base and vals input
 * @param base Base value/* www.j  ava 2 s  .  c om*/
 * @param vals Compare targets.
 * @return
 */
@SafeVarargs
public static <T> boolean none(Comparator comparator, T base, T... vals) {
    if (vals == null)
        return false;
    for (T val : vals) {
        if (comparator.compare(val, base))
            return false;
    }
    return true;
}