Example usage for java.util SortedMap containsKey

List of usage examples for java.util SortedMap containsKey

Introduction

In this page you can find the example usage for java.util SortedMap containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:co.rsk.remasc.RemascStorageProviderTest.java

@Test
public void setSaveRetrieveAndGetManySiblings() throws IOException {
    String accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();

    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);

    Block genesis = BlockGenerator.getGenesisBlock();
    Block block1 = BlockGenerator.createChildBlock(genesis);
    Block block2 = BlockGenerator.createChildBlock(block1);
    Block block3 = BlockGenerator.createChildBlock(block2);
    Block block4 = BlockGenerator.createChildBlock(block3);
    Block block5 = BlockGenerator.createChildBlock(block4);

    Sibling sibling1 = new Sibling(genesis.getHeader(), genesis.getCoinbase(), 1);
    Sibling sibling2 = new Sibling(block1.getHeader(), block1.getCoinbase(), 2);
    Sibling sibling3 = new Sibling(block2.getHeader(), block2.getCoinbase(), 3);
    Sibling sibling4 = new Sibling(block3.getHeader(), block3.getCoinbase(), 4);
    Sibling sibling5 = new Sibling(block4.getHeader(), block4.getCoinbase(), 5);
    Sibling sibling6 = new Sibling(block5.getHeader(), block5.getCoinbase(), 6);

    List<Sibling> siblings0 = new ArrayList<>();
    List<Sibling> siblings1 = new ArrayList<>();
    List<Sibling> siblings2 = new ArrayList<>();

    siblings0.add(sibling1);/*from  w  ww  . j av  a 2s. c  om*/
    siblings0.add(sibling2);

    siblings1.add(sibling3);
    siblings1.add(sibling4);

    siblings2.add(sibling5);
    siblings2.add(sibling6);

    provider.getSiblings().put(Long.valueOf(0), siblings0);
    provider.getSiblings().put(Long.valueOf(1), siblings1);
    provider.getSiblings().put(Long.valueOf(2), siblings2);

    provider.save();

    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);

    SortedMap<Long, List<Sibling>> map = newProvider.getSiblings();

    Assert.assertNotNull(map);
    Assert.assertFalse(map.isEmpty());

    Assert.assertTrue(map.containsKey(Long.valueOf(0)));
    Assert.assertTrue(map.containsKey(Long.valueOf(1)));
    Assert.assertTrue(map.containsKey(Long.valueOf(2)));

    Assert.assertEquals(2, map.get(Long.valueOf(0)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(1)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(2)).size());

    List<Sibling> list0 = map.get(Long.valueOf(0));
    List<Sibling> list1 = map.get(Long.valueOf(1));
    List<Sibling> list2 = map.get(Long.valueOf(2));

    Assert.assertEquals(1, list0.get(0).getIncludedHeight());
    Assert.assertArrayEquals(genesis.getHeader().getHash(), list0.get(0).getHash());
    Assert.assertEquals(2, list0.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block1.getHeader().getHash(), list0.get(1).getHash());

    Assert.assertEquals(3, list1.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block2.getHeader().getHash(), list1.get(0).getHash());
    Assert.assertEquals(4, list1.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block3.getHeader().getHash(), list1.get(1).getHash());

    Assert.assertEquals(5, list2.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block4.getHeader().getHash(), list2.get(0).getHash());
    Assert.assertEquals(6, list2.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block5.getHeader().getHash(), list2.get(1).getHash());
}

From source file:com.streamsets.pipeline.stage.processor.statsaggregation.TestMetricAggregationProcessor.java

@Test
public void testCountersForMetricRecord() throws StageException {
    MetricsRuleDefinition metricsRuleDefinition1 = TestHelper.createTestMetricRuleDefinition("a < b", true,
            System.currentTimeMillis());
    Record metricRuleChangeRecord = AggregatorUtil.createMetricRuleChangeRecord(metricsRuleDefinition1);
    Record metricRecord = createTestMetricRecord();
    runner.runProcess(Arrays.asList(metricRuleChangeRecord, metricRecord));

    MetricRegistry metrics = metricAggregationProcessor.getMetrics();

    SortedMap<String, Counter> counters = metrics.getCounters();
    Assert.assertTrue(counters.containsKey(
            "stage.com_streamsets_pipeline_stage_origin_spooldir_SpoolDirDSource_1:com_streamsets_pipeline_stage_origin_spooldir_SpoolDirDSource_1OutputLane14578524215930.outputRecords.counter"));
    Assert.assertEquals(1000, counters.get(
            "stage.com_streamsets_pipeline_stage_origin_spooldir_SpoolDirDSource_1:com_streamsets_pipeline_stage_origin_spooldir_SpoolDirDSource_1OutputLane14578524215930.outputRecords.counter")
            .getCount());//from w  ww .  j av  a  2 s.  c  om
    Assert.assertTrue(counters.containsKey(
            "stage.com_streamsets_pipeline_stage_processor_fieldhasher_FieldHasherDProcessor_1:com_streamsets_pipeline_stage_processor_fieldhasher_FieldHasherDProcessor_1OutputLane14578524269500.outputRecords.counter"));
    Assert.assertEquals(750, counters.get(
            "stage.com_streamsets_pipeline_stage_processor_fieldhasher_FieldHasherDProcessor_1:com_streamsets_pipeline_stage_processor_fieldhasher_FieldHasherDProcessor_1OutputLane14578524269500.outputRecords.counter")
            .getCount());
    Assert.assertTrue(counters.containsKey(
            "stage.com_streamsets_pipeline_stage_processor_expression_ExpressionDProcessor_1:com_streamsets_pipeline_stage_processor_expression_ExpressionDProcessor_1OutputLane14578524390500.outputRecords.counter"));
    Assert.assertEquals(500, counters.get(
            "stage.com_streamsets_pipeline_stage_processor_expression_ExpressionDProcessor_1:com_streamsets_pipeline_stage_processor_expression_ExpressionDProcessor_1OutputLane14578524390500.outputRecords.counter")
            .getCount());
}

From source file:org.codetrack.database.file.FileProject.java

/**
 * Remove an ProjectItem instance of project
 *
 * @param projectItem to remove/*from   w w w  . j a  v a 2s .  c  om*/
 * @return this project
 */
//@Override
public Project remove(ProjectItem projectItem) {

    SortedMap<String, ProjectItem> map = itemsMap.get(projectItem.getClass());

    if (map == null) {
        map = Maps.newTreeMap();
        itemsMap.put(projectItem.getClass(), map);
        return this;
    }

    if (map.containsKey(projectItem.getId()))
        map.remove(projectItem.getId());

    return this;

}

From source file:org.ambraproject.article.service.BrowseServiceSolrTest.java

@Test(dataProvider = "articleCategoryCount")
public void testGetArticleCountsByCategory(String journalKey, Map<String, Long> expectedCounts) {
    SortedMap<String, Long> results = browseService.getSubjectsForJournal(journalKey);
    assertNotNull(results, "returned null results");
    assertEquals(results.size(), expectedCounts.size(), "returned incorrect number of categories");
    for (String category : expectedCounts.keySet()) {
        assertTrue(results.containsKey(category), "didn't return expected category " + category);
        assertEquals(results.get(category), expectedCounts.get(category),
                "Returned incorrect count for category " + category);
    }//from  w  w  w  .  ja v  a2 s  .co  m
}

From source file:com.streamsets.pipeline.stage.processor.statsaggregation.TestMetricAggregationProcessor.java

@Test
public void testEvaluateDataRuleRecord() throws StageException, InterruptedException {
    Record dataRuleChangeRecord = AggregatorUtil.createDataRuleChangeRecord(
            TestHelper.createTestDataRuleDefinition("a < b", true, System.currentTimeMillis()));
    Thread.sleep(2); // Sleep for 2 seconds that the data rule records get a later timestamp
    List<Record> testDataRuleRecords = TestHelper.createTestDataRuleRecords();
    List<Record> batch = new ArrayList<>();
    batch.add(dataRuleChangeRecord);// w  ww  .jav a  2 s  .c o  m
    batch.addAll(testDataRuleRecords);
    runner.runProcess(batch);
    MetricRegistry metrics = metricAggregationProcessor.getMetrics();

    SortedMap<String, Counter> counters = metrics.getCounters();
    Assert.assertTrue(counters.containsKey("user.x.matched.counter"));
    Assert.assertEquals(400, counters.get("user.x.matched.counter").getCount());

    Assert.assertTrue(counters.containsKey("user.x.evaluated.counter"));
    Assert.assertEquals(2000, counters.get("user.x.evaluated.counter").getCount());

    // Alert expected as threshold type is count and threshold value is 100
    SortedMap<String, Gauge> gauges = metrics.getGauges();
    Assert.assertTrue(gauges.containsKey("alert.x.gauge"));
    Map<String, Object> gaugeValue = (Map<String, Object>) gauges.get("alert.x.gauge").getValue();
    Assert.assertEquals(400L, gaugeValue.get("currentValue"));

    List<String> alertTexts = (List<String>) gaugeValue.get("alertTexts");
    Assert.assertEquals(1, alertTexts.size());
    Assert.assertEquals("Alert!!", alertTexts.get(0));
}

From source file:com.cafbit.netlib.dns.DNSMessage.java

public String toString() {
    StringBuilder sb = new StringBuilder();

    // questions/* w  w  w  .ja  v  a 2 s  .co m*/
    for (DNSQuestion q : questions) {
        sb.append("\nQuestion: " + q.toString() + "\n");
    }

    // group answers by name
    SortedMap<String, List<DNSAnswer>> answersByName = new TreeMap<String, List<DNSAnswer>>();

    for (DNSAnswer a : answers) {
        List<DNSAnswer> list;
        if (answersByName.containsKey(a.name)) {
            list = answersByName.get(a.name);
        } else {
            list = new LinkedList<DNSAnswer>();
            answersByName.put(a.name, list);
        }
        list.add(a);
    }

    for (DNSAnswer a : authorities) {
        List<DNSAnswer> list;
        if (answersByName.containsKey(a.name)) {
            list = answersByName.get(a.name);
        } else {
            list = new LinkedList<DNSAnswer>();
            answersByName.put(a.name, list);
        }
        list.add(a);
    }

    for (DNSAnswer a : addrecords) {
        List<DNSAnswer> list;
        if (answersByName.containsKey(a.name)) {
            list = answersByName.get(a.name);
        } else {
            list = new LinkedList<DNSAnswer>();
            answersByName.put(a.name, list);
        }
        list.add(a);
    }

    for (Map.Entry<String, List<DNSAnswer>> entry : answersByName.entrySet()) {
        sb.append(entry.getKey() + "\n");
        for (DNSAnswer a : entry.getValue()) {
            sb.append("  " + a.type.toString() + " " + a.getRdataString() + "\n");
        }
    }

    return sb.toString();
}

From source file:org.kuali.kpme.tklm.leave.block.web.LeaveBlockDisplayAction.java

private List<LeaveBlockDisplay> getLeaveEntries(String principalId, LocalDate serviceDate, LocalDate beginDate,
        LocalDate endDate, List<AccrualCategory> accrualCategories) {
    List<LeaveBlockDisplay> leaveEntries = new ArrayList<LeaveBlockDisplay>();

    List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId,
            beginDate, endDate);//from www. ja v a  2 s .c  om

    for (LeaveBlock leaveBlock : leaveBlocks) {
        if (!leaveBlock.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER)) {
            leaveEntries.add(new LeaveBlockDisplay(leaveBlock));
        }
    }
    Collections.sort(leaveEntries, new Comparator<LeaveBlockDisplay>() {
        @Override
        public int compare(LeaveBlockDisplay o1, LeaveBlockDisplay o2) {
            return ObjectUtils.compare(o1.getLeaveDate(), o2.getLeaveDate());
        }
    });

    SortedMap<String, BigDecimal> accrualBalances = getPreviousAccrualBalances(principalId, serviceDate,
            beginDate, accrualCategories);

    for (LeaveBlockDisplay leaveEntry : leaveEntries) {
        for (AccrualCategory accrualCategory : accrualCategories) {
            if (!accrualBalances.containsKey(accrualCategory.getAccrualCategory())) {
                accrualBalances.put(accrualCategory.getAccrualCategory(), BigDecimal.ZERO);
            }
            BigDecimal currentAccrualBalance = accrualBalances.get(accrualCategory.getAccrualCategory());

            if (StringUtils.equals(leaveEntry.getAccrualCategory(), accrualCategory.getAccrualCategory())) {
                BigDecimal accruedBalance = currentAccrualBalance.add(leaveEntry.getLeaveAmount());
                accrualBalances.put(accrualCategory.getAccrualCategory(), accruedBalance);
            }

            leaveEntry.setAccrualBalance(accrualCategory.getAccrualCategory(),
                    accrualBalances.get(accrualCategory.getAccrualCategory()));
        }
    }

    return leaveEntries;
}

From source file:org.apache.hadoop.hbase.regionserver.wal.TestHLogFiltering.java

@Test
public void testFlushedSequenceIdsSentToHMaster() throws IOException, InterruptedException, ServiceException {
    SortedMap<byte[], Long> allFlushedSequenceIds = new TreeMap<byte[], Long>(Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < NUM_RS; ++i) {
        flushAllRegions(i);/*from w w  w .  java 2 s  .c  o  m*/
    }
    Thread.sleep(10000);
    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
    for (int i = 0; i < NUM_RS; ++i) {
        for (byte[] regionName : getRegionsByServer(i)) {
            if (allFlushedSequenceIds.containsKey(regionName)) {
                GetLastFlushedSequenceIdRequest req = RequestConverter
                        .buildGetLastFlushedSequenceIdRequest(regionName);

                assertEquals((long) allFlushedSequenceIds.get(regionName),
                        master.getLastFlushedSequenceId(null, req).getLastFlushedSequenceId());
            }
        }
    }
}

From source file:org.apache.hadoop.yarn.util.Log4jWarningErrorMetricsAppender.java

private void updateMessageDetails(String message, Long eventTimeSeconds,
        Map<String, SortedMap<Long, Integer>> map, SortedMap<Long, Integer> timestampsCount,
        SortedSet<PurgeElement> purgeInformation) {
    synchronized (lock) {
        if (map.containsKey(message)) {
            SortedMap<Long, Integer> tmp = map.get(message);
            Long lastMessageTime = tmp.lastKey();
            int value = 1;
            if (tmp.containsKey(eventTimeSeconds)) {
                value = tmp.get(eventTimeSeconds) + 1;
            }//from w w w.  j  a  v  a 2 s  .  com
            tmp.put(eventTimeSeconds, value);
            purgeInformation.remove(new PurgeElement(message, lastMessageTime));
        } else {
            SortedMap<Long, Integer> value = new TreeMap<>();
            value.put(eventTimeSeconds, 1);
            map.put(message, value);
            if (map.size() > maxUniqueMessages * 2) {
                cleanupTimer.cancel();
                cleanupTimer = new Timer();
                cleanupTimer.schedule(new ErrorAndWarningsCleanup(), 0);
            }
        }
        purgeInformation.add(new PurgeElement(message, eventTimeSeconds));
        int newValue = 1;
        if (timestampsCount.containsKey(eventTimeSeconds)) {
            newValue = timestampsCount.get(eventTimeSeconds) + 1;
        }
        timestampsCount.put(eventTimeSeconds, newValue);
    }
}

From source file:org.sakaiproject.tool.roster.FilteredParticipantListingBean.java

protected SortedMap<String, Integer> findRoleCounts(Iterable<Participant> participants) {
    SortedMap<String, Integer> roleCountMap = new TreeMap<String, Integer>();
    for (Participant participant : participants) {
        String role = participant.getRoleTitle();
        if (role != null) {
            if (roleCountMap.containsKey(role)) {
                int count = roleCountMap.get(role) + 1;
                roleCountMap.put(role, count);
            } else {
                roleCountMap.put(role, 1);
            }/*from  ww  w.  j  av a 2s .  c  om*/
        } else {
            String partName = "Unknown";
            //This doesn't seem likely either, but just in case!
            if (participant.getUser() != null) {
                partName = participant.getUser().getDisplayId();
            }
            log.info("Role null for participant:" + partName);
        }
    }
    return roleCountMap;
}