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:eu.stratosphere.pact.runtime.sort.ExternalSortITCase.java

@Test
public void testSpillingSort() throws Exception {
    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.CONSTANT, VAL);//from   w w w  . j  ava  2 s  . c om
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, 16 * 1024 * 1024, 64, 0.7f);

    // emit data
    LOG.debug("Reading and sorting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsEmitted = 1;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsEmitted++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());

        rec2 = tmp;
    }
    Assert.assertTrue(NUM_PAIRS == pairsEmitted);

    merger.close();
}

From source file:org.kuali.rice.kns.util.BeanPropertyComparator.java

/**
 * Compare two JavaBeans by the properties given to the constructor. If no propertues
 * /*from  w w  w  .  j  a  v  a  2  s.c om*/
 * @param o1 Object The first bean to get data from to compare against
 * @param o2 Object The second bean to get data from to compare
 * @return int negative or positive based on order
 */
public int compare(Object o1, Object o2) {
    int compared = 0;

    try {
        for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) {
            String currentProperty = i.next().toString();

            // choose appropriate comparator
            Comparator currentComparator = null;
            try {
                PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1,
                        currentProperty);
                Class propertyClass = propertyDescriptor.getPropertyType();
                if (propertyClass.equals(String.class)) {
                    currentComparator = this.stringComparator;
                } else if (TypeUtils.isBooleanClass(propertyClass)) {
                    currentComparator = this.booleanComparator;
                } else {
                    currentComparator = this.genericComparator;
                }
            } catch (NullPointerException e) {
                throw new BeanComparisonException(
                        "unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e);
            }

            // compare the values
            Object value1 = PropertyUtils.getProperty(o1, currentProperty);
            Object value2 = PropertyUtils.getProperty(o2, currentProperty);
            /* Begin Null Support Fix */
            if (value1 == null && value2 == null)
                return 0;
            else if (value1 == null)
                return -1;
            else if (value2 == null)
                return 1;
            /* End Null Support Fix*/
            compared = currentComparator.compare(value1, value2);
        }
    } catch (IllegalAccessException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    } catch (NoSuchMethodException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    } catch (InvocationTargetException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    }

    return compared;
}

From source file:org.apache.flink.runtime.operators.sort.ExternalSortITCase.java

@Test
public void testInMemorySort() throws Exception {
    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.CONSTANT, VAL);/* www .  j a v a  2  s.  c om*/
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 64 / 78, 2, 0.9f);

    // emit data
    LOG.debug("Reading and sorting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsEmitted = 1;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsEmitted++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());

        rec2 = tmp;
    }
    Assert.assertTrue(NUM_PAIRS == pairsEmitted);

    merger.close();
}

From source file:org.apache.flink.runtime.operators.sort.ExternalSortITCase.java

@Test
public void testSpillingSort() throws Exception {
    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.CONSTANT, VAL);//from w  w  w. j  a v a 2  s  .  c  o  m
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 16 / 78, 64, 0.7f);

    // emit data
    LOG.debug("Reading and sorting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsEmitted = 1;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsEmitted++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());

        rec2 = tmp;
    }
    Assert.assertTrue(NUM_PAIRS == pairsEmitted);

    merger.close();
}

From source file:org.tinygroup.springmvc.handlermapping.DefaultCOCUrlHandlerMapping.java

protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {
    // ???// www. j av a 2 s.c om

    // Direct match?
    Object handler = getHandlerMap().get(urlPath);
    if (handler != null) {
        // Bean name or resolved handler?
        if (handler instanceof String) {
            String handlerName = (String) handler;
            handler = getApplicationContext().getBean(handlerName);
        }
        validateHandler(handler, request);
        return buildPathExposingHandler(handler, urlPath, urlPath, null);
    }
    // +++ convention match ??match
    String noExtPath = WebUtil.getUriWithoutExtension(urlPath);
    boolean hasExtension = !StringUtils.equals(urlPath, noExtPath);

    // for Pattern match?
    List<String> matchingPatterns = new ArrayList<String>();

    if (hasExtension) {
        // ???
        handler = getHandlerMap().get(noExtPath);
        if (handler != null) {
            // Bean name or resolved handler?
            if (handler instanceof String) {
                String handlerName = (String) handler;
                handler = getApplicationContext().getBean(handlerName);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Convention Matching  for request [" + urlPath + "] are " + noExtPath);
            }
            validateHandler(handler, request);
            return buildPathExposingHandler(handler, urlPath, urlPath, null);
        }

        // pattern match
        for (String registeredPattern : getHandlerMap().keySet()) {
            if (getPathMatcher().match(registeredPattern, urlPath)) {
                // ? users/1.htm match
                // users/{id}????
                if (!conventionHelper.isConventional(registeredPattern)) {
                    matchingPatterns.add(registeredPattern);
                    continue;
                }
            }
            // condition: convention && request path has extension
            if ((hasExtension && getPathMatcher().match(registeredPattern, noExtPath))) {
                // if request path has extension ,then add the extension to
                // pattern
                matchingPatterns.add(registeredPattern);
            }
        }
    } else {// ?
        // pattern match
        for (String registeredPattern : getHandlerMap().keySet()) {
            if (getPathMatcher().match(registeredPattern, urlPath)) {
                matchingPatterns.add(registeredPattern);
            }
        }
    }
    String bestPatternMatch = null;
    Comparator<String> patternComparator = getPathMatcher().getPatternComparator(urlPath);
    if (!matchingPatterns.isEmpty()) {
        Collections.sort(matchingPatterns, patternComparator);
        if (logger.isDebugEnabled()) {
            logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns);
        }
        bestPatternMatch = matchingPatterns.get(0);
    }
    if (bestPatternMatch != null) {
        handler = getHandlerMap().get(bestPatternMatch);
        // Bean name or resolved handler?
        if (handler instanceof String) {
            String handlerName = (String) handler;
            handler = getApplicationContext().getBean(handlerName);
        }
        validateHandler(handler, request);

        if (conventionHelper.isConventional(bestPatternMatch) && hasExtension) {
            urlPath = noExtPath;
        }

        String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPatternMatch, urlPath);

        // There might be multiple 'best patterns', let's make sure we have
        // the correct URI template variables
        // for all of them
        Map<String, String> uriTemplateVariables = new LinkedHashMap<String, String>();
        for (String matchingPattern : matchingPatterns) {
            if (patternComparator.compare(bestPatternMatch, matchingPattern) == 0) {
                uriTemplateVariables
                        .putAll(getPathMatcher().extractUriTemplateVariables(matchingPattern, urlPath));
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("URI Template variables for request [" + urlPath + "] are " + uriTemplateVariables);
        }
        return buildPathExposingHandler(handler, bestPatternMatch, pathWithinMapping, uriTemplateVariables);
    }
    // No handler found...
    return null;
}

From source file:eu.stratosphere.pact.runtime.sort.UnilateralSortMergerITCase.java

public void testSpillingSortWithIntermediateMerge() throws Exception {
    // amount of pairs
    final int PAIRS = 10000000;

    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.FIX_LENGTH);/*  w w  w. j  a  v a2  s .com*/
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, 64 * 1024 * 1024, 16, 0.7f);

    // emit data
    LOG.debug("Emitting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsRead = 1;
    int nextStep = PAIRS / 20;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsRead++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());
        rec2 = tmp;

        // log
        if (pairsRead == nextStep) {
            nextStep += PAIRS / 20;
        }

    }
    Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead);
    merger.close();
}

From source file:eu.stratosphere.pact.runtime.sort.ExternalSortITCase.java

@Test
public void testSpillingSortWithIntermediateMerge() throws Exception {
    // amount of pairs
    final int PAIRS = 10000000;

    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.FIX_LENGTH);//from   w  w w.j  a  v a2  s .  c o  m
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, 64 * 1024 * 1024, 16, 0.7f);

    // emit data
    LOG.debug("Emitting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsRead = 1;
    int nextStep = PAIRS / 20;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsRead++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());
        rec2 = tmp;

        // log
        if (pairsRead == nextStep) {
            nextStep += PAIRS / 20;
        }

    }
    Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead);
    merger.close();
}

From source file:eu.stratosphere.pact.runtime.sort.ExternalSortITCase.java

@Test
public void testInMemorySortUsing10Buffers() throws Exception {
    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.CONSTANT, VAL);//from  w w w  .  j av a  2 s  .c  o m
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, 64 * 1024 * 1024, 10, 2,
            0.9f);

    // emit data
    LOG.debug("Reading and sorting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsEmitted = 1;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsEmitted++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());

        rec2 = tmp;
    }
    Assert.assertTrue(NUM_PAIRS == pairsEmitted);

    merger.close();
}

From source file:org.apache.flink.runtime.operators.sort.ExternalSortITCase.java

public void testSpillingSortWithIntermediateMerge() throws Exception {
    // amount of pairs
    final int PAIRS = 10000000;

    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.FIX_LENGTH);/* w w  w .  ja v  a 2s.  co  m*/
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 64 / 78, 16, 0.7f);

    // emit data
    LOG.debug("Emitting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsRead = 1;
    int nextStep = PAIRS / 20;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsRead++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());
        rec2 = tmp;

        // log
        if (pairsRead == nextStep) {
            nextStep += PAIRS / 20;
        }

    }
    Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead);
    merger.close();
}

From source file:org.apache.flink.runtime.operators.sort.ExternalSortITCase.java

@Test
public void testInMemorySortUsing10Buffers() throws Exception {
    // comparator
    final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator();

    final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM,
            ValueMode.CONSTANT, VAL);/*from   w ww  .j av a  2 s. c  o m*/
    final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS);

    // merge iterator
    LOG.debug("Initializing sortmerger...");

    Sorter<Record> merger = new UnilateralSortMerger<Record>(this.memoryManager, this.ioManager, source,
            this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 64 / 78, 10, 2,
            0.9f);

    // emit data
    LOG.debug("Reading and sorting data...");

    // check order
    MutableObjectIterator<Record> iterator = merger.getIterator();

    LOG.debug("Checking results...");
    int pairsEmitted = 1;

    Record rec1 = new Record();
    Record rec2 = new Record();

    Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
    while ((rec2 = iterator.next(rec2)) != null) {
        final Key k1 = rec1.getField(0, TestData.Key.class);
        final Key k2 = rec2.getField(0, TestData.Key.class);
        pairsEmitted++;

        Assert.assertTrue(keyComparator.compare(k1, k2) <= 0);

        Record tmp = rec1;
        rec1 = rec2;
        k1.setKey(k2.getKey());

        rec2 = tmp;
    }
    Assert.assertTrue(NUM_PAIRS == pairsEmitted);

    merger.close();
}