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:edu.umn.cs.spatialHadoop.core.RectangleNN.java

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

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

    long t1 = System.currentTimeMillis();
    LOG.info("Joining arrays " + R.length + " with " + S.length);
    Arrays.sort(R, comparator);
    Arrays.sort(S, comparator);

    int i = 0, j = 0;

    try {
        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++;
                    if (reporter != null)
                        reporter.progress();
                }
                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++;
                    if (reporter != null)
                        reporter.progress();
                }
                j++;
            }
            if (reporter != null)
                reporter.progress();
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    long t2 = System.currentTimeMillis();
    LOG.info("Finished plane sweep in " + (t2 - t1) + " millis and found " + count + " pairs");
    return count;
}

From source file:edu.umn.cs.spatialHadoop.core.RectangleNN.java

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

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

    long t1 = System.currentTimeMillis();
    LOG.info("Joining arrays " + R.length + " with " + S.length);
    Arrays.sort(R, comparator);
    Arrays.sort(S, comparator);

    int i = 0, j = 0;

    try {
        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.getMBR().isIntersected(s.getMBR())) {
                        if (output != null)
                            output.collect(r, s);
                        count++;
                    }
                    jj++;

                    if (reporter != null)
                        reporter.progress();
                }
                i++;
            } else {
                s = S[j];
                int ii = i;

                while ((ii < R.length) && ((r = R[ii]).getMBR().x1 <= s.getMBR().x2)) {
                    if (r.getMBR().isIntersected(s.getMBR())) {
                        if (output != null)
                            output.collect(r, s);
                        count++;
                    }
                    ii++;
                }
                j++;
                if (reporter != null)
                    reporter.progress();
            }
            if (reporter != null)
                reporter.progress();
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    long t2 = System.currentTimeMillis();
    LOG.info("Finished plane sweep filter only in " + (t2 - t1) + " millis and found " + count + " pairs");
    return count;
}

From source file:edu.umn.cs.spatialHadoop.core.RectangleNN.java

/**
 * @param R// w  w  w .ja  v  a 2  s. c o  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, Reporter reporter) throws IOException {
    int count = 0;

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

    long t1 = System.currentTimeMillis();
    LOG.info("Joining lists " + R.size() + " with " + S.size());
    Collections.sort(R, comparator);
    Collections.sort(S, comparator);

    int i = 0, j = 0;

    try {
        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)) {
                    // Check if r and s are overlapping but not the same object
                    // for self join
                    if (r.isIntersected(s) && !r.equals(s)) {
                        if (output != null)
                            output.collect(r, s);
                        count++;
                    }
                    jj++;
                    if (reporter != null)
                        reporter.progress();
                }
                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) && !r.equals(s)) {
                        if (output != null)
                            output.collect(r, s);
                        count++;
                    }
                    ii++;
                    if (reporter != null)
                        reporter.progress();
                }
                j++;
            }
            if (reporter != null)
                reporter.progress();
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    long t2 = System.currentTimeMillis();
    LOG.info("Finished plane sweep in " + (t2 - t1) + " millis and found " + count + " pairs");
    return count;
}

From source file:ru.novoscan.trkpd.snmp.spring.TrackSpringSnmpAgent.java

private int[] nextOID(int oidDigits[]) {
    Iterator<?> keysIterator = mibEntries.keySet().iterator();
    // Fix: Added parameters to avoid compilation warnings
    Comparator<int[]> comparator = (TrackOidComparator) mibEntries.comparator();
    while (keysIterator.hasNext()) {
        int curKey[] = (int[]) (int[]) keysIterator.next();
        if (comparator.compare(curKey, oidDigits) > 0)
            return curKey;
    }/* w w w . j a  v a 2  s . c o m*/
    return null;
}

From source file:org.localmatters.lesscss4j.compile.AbstractLessCssCompilerTest.java

protected void compileAndValidate(String lessFile, String cssFile, Comparator<String> comparator)
        throws IOException {
    URL url = getClass().getClassLoader().getResource(lessFile);
    assertNotNull("Unable to open " + lessFile, url);

    ByteArrayOutputStream output = new ByteArrayOutputStream();

    _compiler.compile(new UrlStyleSheetResource(url), output, _errorHandler);

    output.close();//w  w  w  .j a va 2 s.  c o  m

    if (_errorHandler == null || _errorHandler.getErrorCount() == 0) {
        String expected = readCss(cssFile);
        String actual = output.toString(ENCODING);
        if (comparator == null) {
            assertEquals(expected, actual);
        } else {
            comparator.compare(expected, actual);
        }
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FSLeafQueue.java

@Override
public RMContainer preemptContainer() {
    RMContainer toBePreempted = null;/*  w  w  w. j av a2  s .  c  o m*/

    // If this queue is not over its fair share, reject
    if (!canBePreempted()) {
        return toBePreempted;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Queue " + getName() + " is going to preempt a container " + "from its applications.");
    }

    // Choose the app that is most over fair share
    Comparator<Schedulable> comparator = policy.getComparator();
    FSAppAttempt candidateSched = null;
    readLock.lock();
    try {
        for (FSAppAttempt sched : runnableApps) {
            if (candidateSched == null || comparator.compare(sched, candidateSched) > 0) {
                candidateSched = sched;
            }
        }
    } finally {
        readLock.unlock();
    }

    // Preempt from the selected app
    if (candidateSched != null) {
        toBePreempted = candidateSched.preemptContainer();
    }
    return toBePreempted;
}

From source file:org.marketcetera.marketdata.yahoo.YahooFeedEventTranslator.java

/**
 * Determines if the given event should be sent to the client or not. 
 *
 * <p>This method requires external synchronization.
 *
 * @param inEvent an <code>Event</code> value
 * @return a <code>boolean</code> value
 */// w w w  .  ja  v a2s  . com
private boolean shouldSendEvent(Event inEvent) {
    Event cachedEvent = eventCache.get(inEvent.getClass());
    if (cachedEvent == null) {
        eventCache.put(inEvent.getClass(), inEvent);
        return true;
    }
    // compare just the salient parts (e.g., the timestamp will be different, but that won't matter to us)
    Comparator<Event> comparator = getComparator(inEvent);
    if (comparator == null) {
        throw new UnsupportedOperationException(Messages.NO_COMPARATOR.getText(inEvent.getClass()));
    }
    if (comparator.compare(cachedEvent, inEvent) == 0) {
        // event compares to the cachedEvent, do nothing
        return false;
    }
    // event is not the same as the cachedEvent
    eventCache.put(inEvent.getClass(), inEvent);
    return true;
}

From source file:org.marketcetera.photon.internal.marketdata.ui.MarketDepthView.java

private void setSortColumn(TableSupport tableSupport, final Comparator<MDQuote> comparator, int column,
        int dir) {
    TableViewer viewer = tableSupport.getTableViewer();
    Table table = viewer.getTable();//from w  ww  . j a  va  2  s  .c om
    table.setSortColumn(table.getColumn(column));
    table.setSortDirection(dir);
    viewer.setComparator(new ViewerComparator() {
        @Override
        public int compare(Viewer viewer, Object e1, Object e2) {
            MDQuote q1 = (MDQuote) e1;
            MDQuote q2 = (MDQuote) e2;
            return comparator.compare(q1, q2);
        }
    });

}

From source file:org.briljantframework.array.base.BaseArrayRoutines.java

@Override
public <T> T min(Array<T> x, Comparator<T> cmp) {
    if (x.size() < 1) {
        return null;
    }/*from w ww  . ja  va  2  s  .c  o  m*/
    return x.reduce(x.get(0), (o, n) -> {
        if (cmp.compare(o, n) < 0) {
            return o;
        } else {
            return n;
        }
    });
}

From source file:org.briljantframework.array.base.BaseArrayRoutines.java

@Override
public <T> T max(Array<T> x, Comparator<T> cmp) {
    if (x.size() < 1) {
        return null;
    }/*from w  ww . j a v  a 2 s.co m*/
    return x.reduce(x.get(0), (o, n) -> {
        if (cmp.compare(o, n) > 0) {
            return o;
        } else {
            return n;
        }
    });
}