Example usage for java.util SortedSet first

List of usage examples for java.util SortedSet first

Introduction

In this page you can find the example usage for java.util SortedSet first.

Prototype

E first();

Source Link

Document

Returns the first (lowest) element currently in this set.

Usage

From source file:com.joliciel.jochre.search.CoordinateStorageImpl.java

public List<Rectangle> getNearestRectangles(int offset) {
    int nearestOffset = -1;
    SortedSet<Integer> tailSet = wordOffsets.tailSet(offset);
    if (tailSet.size() > 0) {
        nearestOffset = tailSet.first();
    } else {/* w w  w.  j  a va  2  s .co  m*/
        SortedSet<Integer> headSet = wordOffsets.headSet(offset);
        nearestOffset = headSet.last();
    }
    if (nearestOffset >= 0) {
        return this.getRectangles(nearestOffset);
    }
    return null;
}

From source file:com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterTest.java

@Test
public void testCustomMessageWithParameters() throws Exception {
    DefaultConfiguration config = createCheckConfig(emptyCheck.getClass());
    config.addMessage("msgKey", "This is a custom message with {0}.");
    emptyCheck.configure(config);//from www.  java 2 s  .  c om

    LocalizedMessages collector = new LocalizedMessages();
    emptyCheck.setMessages(collector);

    emptyCheck.log(0, "msgKey", "TestParam");

    SortedSet<LocalizedMessage> messages = collector.getMessages();
    assertEquals(1, messages.size());

    assertEquals("This is a custom message with TestParam.", messages.first().getMessage());
}

From source file:ca.travelagency.invoice.InvoicePersistenceTest.java

@Test
public void testAddDestination() throws Exception {
    // setup/*from w w w.  ja  va  2 s  .c o m*/
    Invoice invoice = InvoiceHelper.makeInvoiceWithoutId(customer, systemUser);
    InvoiceDestination expected = InvoiceHelper.makeDestinationWithoutId();
    // execute
    invoice.addInvoiceDestination(expected);
    persist(invoice);
    // validate
    validateRelationship(expected);
    Invoice result = find(Invoice.class, invoice.getId());
    // validate
    SortedSet<InvoiceDestination> invoiceDestinations = result.getInvoiceDestinations();
    InvoiceDestination actual = invoiceDestinations.first();
    Assert.assertEquals(expected, actual);
    Assert.assertNotSame(expected, actual);
}

From source file:org.powertac.common.OrderbookTests.java

@Test
public void testGetBids() {
    Orderbook ob = new Orderbook(timeslot, 20.1, now);
    ob.addBid(new OrderbookOrder(3.3, -20.0)).addBid(new OrderbookOrder(2.1, -18.2))
            .addBid(new OrderbookOrder(5.6, -19.4)).addBid(new OrderbookOrder(6.2, null));
    SortedSet<OrderbookOrder> bids = ob.getBids();
    assertEquals("correct number", 4, bids.size());
    assertEquals("correct first item price", null, bids.first().getLimitPrice());
    assertEquals("correct last item price", -18.2, bids.last().getLimitPrice(), 1e-6);
    SortedSet<OrderbookOrder> asks = ob.getAsks();
    assertEquals("no asks", 0, asks.size());
}

From source file:org.apache.hadoop.hbase.zookeeper.lock.ZKInterProcessReadLock.java

/**
 * {@inheritDoc}/*from   www.j  a  v  a2 s  .  c o m*/
 */
@Override
protected String getLockPath(String createdZNode, List<String> children) throws IOException {
    TreeSet<String> writeChildren = new TreeSet<String>(ZNodeComparator.COMPARATOR);
    for (String child : children) {
        if (isChildWriteLock(child)) {
            writeChildren.add(child);
        }
    }
    if (writeChildren.isEmpty()) {
        return null;
    }
    SortedSet<String> lowerChildren = writeChildren.headSet(createdZNode);
    if (lowerChildren.isEmpty()) {
        return null;
    }
    String pathToWatch = lowerChildren.last();
    String nodeHoldingLock = lowerChildren.first();
    String znode = ZKUtil.joinZNode(parentLockNode, nodeHoldingLock);
    handleLockMetadata(znode);

    return pathToWatch;
}

From source file:org.apache.hadoop.hbase.zookeeper.lock.HReadLockImpl.java

/**
 * {@inheritDoc}/*  w  ww.  jav a 2s .  c  om*/
 */
@Override
protected String getLockPath(String createdZNode, List<String> children)
        throws IOException, InterruptedException {
    TreeSet<String> writeChildren = new TreeSet<String>(new ZNodeComparator(zkWrapper.getIdentifier()));
    for (String child : children) {
        if (isWriteChild(child)) {
            writeChildren.add(child);
        }
    }
    if (writeChildren.isEmpty()) {
        return null;
    }
    SortedSet<String> lowerChildren = writeChildren.headSet(createdZNode);
    if (lowerChildren.isEmpty()) {
        return null;
    }
    String pathToWatch = lowerChildren.last();
    String nodeHoldingLock = lowerChildren.first();
    try {
        handleLockMetadata(nodeHoldingLock);
    } catch (IOException e) {
        LOG.warn("Error processing lock metadata in " + nodeHoldingLock, e);
    }
    return pathToWatch;
}

From source file:com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterTest.java

@Test(expected = IllegalArgumentException.class)
public void testCustomMessageWithParametersNegative() throws Exception {
    DefaultConfiguration config = createCheckConfig(emptyCheck.getClass());
    config.addMessage("msgKey", "This is a custom message {0.");
    emptyCheck.configure(config);//ww w  .ja v  a2  s  . c  o  m

    LocalizedMessages collector = new LocalizedMessages();
    emptyCheck.setMessages(collector);

    emptyCheck.log(0, "msgKey", "TestParam");

    SortedSet<LocalizedMessage> messages = collector.getMessages();
    assertEquals(1, messages.size());

    //we expect an exception here because of the bogus custom message
    //format
    messages.first().getMessage();
}

From source file:org.powertac.common.OrderbookTests.java

@Test
public void testGetAsks() {
    Orderbook ob = new Orderbook(timeslot, 20.1, now);
    ob.addAsk(new OrderbookOrder(-3.3, 24.0)).addAsk(new OrderbookOrder(-2.1, 20.2))
            .addAsk(new OrderbookOrder(-5.6, 22.4)).addAsk(new OrderbookOrder(-6.2, null));
    SortedSet<OrderbookOrder> asks = ob.getAsks();
    assertEquals("correct number", 4, asks.size());
    assertEquals("no bids", 0, ob.getBids().size());
    assertNull("correct first", asks.first().getLimitPrice());
    assertEquals("correct last", 24.0, asks.last().getLimitPrice(), 1e-6);
}

From source file:eugene.agent.noise.impl.PlaceOrderBehaviour.java

/**
 * Cancels the oldest working order.//from   w w w  . j  a va 2  s .c  om
 */
private void cancel() {
    final SortedSet<OrderReference> orders = session.getOrderReferences();
    if (!orders.isEmpty()) {
        session.send(cancelRequest(orders.first()));
    }
}

From source file:org.cloudata.examples.web.WebKeyRangePartitioner.java

public int getPartition(WritableComparable key, Writable value, int numPartitions) {
    if (confException != null) {
        LOG.error(confException.getMessage(), confException);
        return -1;
    }/*from   w w w .j  av a 2 s. co m*/

    if (numPartitions != tabletInfoSet.size()) {
        LOG.error(
                "tablet count(" + tabletInfoSet.size() + ") not equals numPartitions (" + numPartitions + ")");
        return -1;
    }

    if (tabletInfoSet.size() == 0) {
        LOG.error("tablet partition size is zero");
        return -1;
    }
    int partitionNumber = 0;
    Text tKey = (Text) key;

    Row.Key rowKey;

    int keyIndex = tKey.find("\t");
    if (keyIndex < 0) {
        LOG.error("invalid value:" + tKey);
        rowKey = Row.Key.MAX_KEY;
    } else {
        rowKey = new Row.Key(tKey.getBytes(), 0, keyIndex);
    }

    SortedSet<RowKeyItem> tailSet = tabletInfoSet.tailSet(new RowKeyItem(rowKey, 0));
    RowKeyItem item = null;
    if (tailSet.size() > 0) {
        item = tailSet.first();
        partitionNumber = item.index;
    } else {
        item = tabletInfoSet.last();
        partitionNumber = item.index;
    }

    if (partitionNumber >= numPartitions) {
        LOG.info("Partition Number is : " + partitionNumber + ", numPartitions : " + numPartitions
                + ", Row.Key : " + key.toString());
        partitionNumber = numPartitions - 1;
    }
    //LOG.info("tablet partition num:" + partitionNumber);
    count++;
    if (count % 5000 == 0) {
        try {
            System.out.println("Partitioned:" + new String(rowKey.getBytes(), "EUC-KR") + ","
                    + new String(item.rowKey.getBytes(), "EUC-KR"));
        } catch (UnsupportedEncodingException e) {

        }
    }
    return partitionNumber;
}