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:org.nuclos.common2.LangUtils.java

/**
 * compares two object based on the given comparator.
 * @param t1 may be <code>null</code>
 * @param t2 may be <code>null</code>
 * @param comparator//  w w  w  . ja  v  a 2  s  .c  o  m
 */
public static <T> int compare(T t1, T t2, Comparator<? super T> comparator) {
    final int result;

    if (t1 == null) {
        result = (t2 == null) ? 0 : 1;
    } else if (t2 == null) {
        result = -1;
    } else {
        result = comparator.compare(t1, t2);
    }
    return result;
}

From source file:aarddict.Volume.java

static <T> int binarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
    int lo = 0;/*  w ww  .  ja v a2s . c  o m*/
    int hi = l.size();
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        T midVal = l.get(mid);
        int cmp = c.compare(midVal, key);
        if (cmp < 0) {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    return lo;
}

From source file:com.ricemap.spateDB.core.PrismNN.java

public static <S1 extends Shape, S2 extends Shape> int SpatialJoin_planeSweep(final S1[] R, final S2[] S,
        ResultCollector2<S1, S2> output) {
    int count = 0;

    final Comparator<Shape> comparator = new Comparator<Shape>() {
        @Override/*from w w  w . j a  v  a2  s  .c o m*/
        public int compare(Shape o1, Shape o2) {
            return o1.getMBR().x1 < o2.getMBR().x1 ? -1 : 1;
        }
    };

    LOG.info("Joining " + R.length + " with " + S.length);
    Arrays.sort(R, comparator);
    Arrays.sort(S, comparator);

    int i = 0, j = 0;

    while (i < R.length && j < S.length) {
        S1 r;
        S2 s;
        if (comparator.compare(R[i], S[j]) < 0) {
            r = R[i];
            int jj = j;

            while ((jj < S.length) && ((s = S[jj]).getMBR().x1 <= r.getMBR().x2)) {
                if (r.isIntersected(s)) {
                    if (output != null)
                        output.collect(r, s);
                    count++;
                }
                jj++;
            }
            i++;
        } else {
            s = S[j];
            int ii = i;

            while ((ii < R.length) && ((r = R[ii]).getMBR().x1 <= s.getMBR().x2)) {
                if (r.isIntersected(s)) {
                    if (output != null)
                        output.collect(r, s);
                    count++;
                }
                ii++;
            }
            j++;
        }
    }
    LOG.info("Finished plane sweep and found " + count + " pairs");
    return count;
}

From source file:com.ricemap.spateDB.core.PrismNN.java

/**
 * @param R/*www .jav  a2s  .co m*/
 * @param S
 * @param output
 * @return
 * @throws IOException
 */
public static <S1 extends Shape, S2 extends Shape> int SpatialJoin_planeSweep(List<S1> R, List<S2> S,
        ResultCollector2<S1, S2> output) throws IOException {
    int count = 0;

    Comparator<Shape> comparator = new Comparator<Shape>() {
        @Override
        public int compare(Shape o1, Shape o2) {
            return o1.getMBR().x1 < o2.getMBR().x1 ? -1 : 1;
        }
    };

    LOG.info("Joining " + R.size() + " with " + S.size());
    Collections.sort(R, comparator);
    Collections.sort(S, comparator);

    int i = 0, j = 0;

    while (i < R.size() && j < S.size()) {
        S1 r;
        S2 s;
        if (comparator.compare(R.get(i), S.get(j)) < 0) {
            r = R.get(i);
            int jj = j;

            while ((jj < S.size()) && ((s = S.get(jj)).getMBR().x1 <= r.getMBR().x2)) {
                if (r.isIntersected(s)) {
                    if (output != null)
                        output.collect(r, s);
                    count++;
                }
                jj++;
            }
            i++;
        } else {
            s = S.get(j);
            int ii = i;

            while ((ii < R.size()) && ((r = R.get(ii)).getMBR().x1 <= s.getMBR().x2)) {
                if (r.isIntersected(s)) {
                    if (output != null)
                        output.collect(r, s);
                    count++;
                }
                ii++;
            }
            j++;
        }
    }
    LOG.info("Finished plane sweep and found " + count + " pairs");
    return count;
}

From source file:org.pepstock.jem.ant.tasks.utilities.SortTask.java

/**
 * This merges a bunch of temporary flat files
 * /*w w  w. jav  a  2s  .c om*/
 * @param files
 * @param fileOutput 
 * @param cmp 
 * @param cs 
 * @param output file
 * @param Charset character set to use to load the strings
 * @return The number of lines sorted.
 * @throws IOException 
 */
public static int mergeSortedFiles(List<File> files, FileOutputStream fileOutput, final Comparator<String> cmp,
        Charset cs) throws IOException {
    PriorityQueue<BinaryFileBuffer> pq = new PriorityQueue<BinaryFileBuffer>(11,
            new Comparator<BinaryFileBuffer>() {
                public int compare(BinaryFileBuffer i, BinaryFileBuffer j) {
                    return cmp.compare(i.peek(), j.peek());
                }
            });
    for (File f : files) {
        BinaryFileBuffer bfb = new BinaryFileBuffer(f, cs);
        pq.add(bfb);
    }
    BufferedWriter fbw = new BufferedWriter(new OutputStreamWriter(fileOutput, cs));
    int rowcounter = 0;
    try {
        while (!pq.isEmpty()) {
            BinaryFileBuffer bfb = pq.poll();
            String r = bfb.pop();
            fbw.write(r);
            fbw.newLine();
            ++rowcounter;
            if (bfb.empty()) {
                bfb.getBufferReader().close();
                // we don't need you anymore
                boolean isDeleted = bfb.getOriginalfile().delete();
                if (!isDeleted) {
                    // nop
                }
            } else {
                // add it back
                pq.add(bfb);
            }
        }
    } finally {
        fbw.flush();
        fbw.close();
        for (BinaryFileBuffer bfb : pq) {
            bfb.close();
        }
    }
    return rowcounter;
}

From source file:com.evolveum.midpoint.prism.PrismPropertyValue.java

public static boolean containsValue(Collection<PrismPropertyValue> collection, PrismPropertyValue value,
        Comparator comparator) {
    for (PrismPropertyValue<?> colVal : collection) {
        if (comparator.compare(colVal, value) == 0) {
            return true;
        }/*from ww  w  .j av  a 2  s .co m*/
    }
    return false;
}

From source file:at.gridtec.lambda4j.operator.binary.ByteBinaryOperator.java

/**
 * Returns a {@link ByteBinaryOperator} which returns the lesser of two elements according to the specified {@code
 * Comparator}.//w ww.  j a  v a2  s. c o  m
 *
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ByteBinaryOperator} which returns the lesser of its operands, according to the supplied {@code
 * Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#minBy(Comparator)
 */
@Nonnull
static ByteBinaryOperator minBy(@Nonnull final Comparator<Byte> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ByteBinaryOperator.java

/**
 * Returns a {@link ByteBinaryOperator} which returns the greater of two elements according to the specified {@code
 * Comparator}./*  ww  w .j  a v a2 s. c o m*/
 *
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ByteBinaryOperator} which returns the greater of its operands, according to the supplied {@code
 * Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#maxBy(Comparator)
 */
@Nonnull
static ByteBinaryOperator maxBy(@Nonnull final Comparator<Byte> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.FloatBinaryOperator.java

/**
 * Returns a {@link FloatBinaryOperator} which returns the lesser of two elements according to the specified {@code
 * Comparator}./*from www  .j  av  a2s .  c om*/
 *
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code FloatBinaryOperator} which returns the lesser of its operands, according to the supplied {@code
 * Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#minBy(Comparator)
 */
@Nonnull
static FloatBinaryOperator minBy(@Nonnull final Comparator<Float> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.FloatBinaryOperator.java

/**
 * Returns a {@link FloatBinaryOperator} which returns the greater of two elements according to the specified {@code
 * Comparator}.//from  w w  w. jav  a2 s .c  o m
 *
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code FloatBinaryOperator} which returns the greater of its operands, according to the supplied {@code
 * Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#maxBy(Comparator)
 */
@Nonnull
static FloatBinaryOperator maxBy(@Nonnull final Comparator<Float> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2;
}