Example usage for java.util SortedMap entrySet

List of usage examples for java.util SortedMap entrySet

Introduction

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

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:com.blacklocus.metrics.CloudWatchReporter.java

public void realReport(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    try {/*  w w w.  ja v a 2s .c o m*/
        // Just an estimate to reduce resizing.
        List<MetricDatum> data = new ArrayList<MetricDatum>(
                gauges.size() + counters.size() + meters.size() + 2 * histograms.size() + 2 * timers.size());

        // Translate various metric classes to MetricDatum
        for (Map.Entry<String, Gauge> gaugeEntry : gauges.entrySet()) {
            reportGauge(gaugeEntry, typeDimValGauge, data);
        }
        for (Map.Entry<String, Counter> counterEntry : counters.entrySet()) {
            reportCounter(counterEntry, typeDimValCounterCount, data);
        }
        for (Map.Entry<String, Meter> meterEntry : meters.entrySet()) {
            reportCounter(meterEntry, typeDimValMeterCount, data);
        }
        for (Map.Entry<String, Histogram> histogramEntry : histograms.entrySet()) {
            reportCounter(histogramEntry, typeDimValHistoSamples, data);
            reportSampling(histogramEntry, typeDimValHistoStats, 1.0, data);
        }
        for (Map.Entry<String, Timer> timerEntry : timers.entrySet()) {
            reportCounter(timerEntry, typeDimValTimerSamples, data);
            reportSampling(timerEntry, typeDimValTimerStats, 0.000001, data); // nanos -> millis
        }

        // Filter out unreportable entries.
        Collection<MetricDatum> nonEmptyData = Collections2.filter(data, new Predicate<MetricDatum>() {
            @Override
            public boolean apply(MetricDatum input) {
                if (input == null) {
                    return false;
                } else if (input.getStatisticValues() != null) {
                    // CloudWatch rejects any Statistic Sets with sample count == 0, which it probably should reject.
                    return input.getStatisticValues().getSampleCount() > 0;
                }
                return true;
            }
        });

        // Whether to use local "now" (true, new Date()) or cloudwatch service "now" (false, leave null).
        if (timestampLocal) {
            Date now = new Date();
            for (MetricDatum datum : nonEmptyData) {
                datum.withTimestamp(now);
            }
        }

        // Finally, apply any user-level filter.
        Collection<MetricDatum> filtered = Collections2.filter(nonEmptyData, reporterFilter);

        // Each CloudWatch API request may contain at maximum 20 datums. Break into partitions of 20.
        Iterable<List<MetricDatum>> dataPartitions = Iterables.partition(filtered, 20);
        List<Future<?>> cloudWatchFutures = Lists.newArrayListWithExpectedSize(filtered.size());

        // Submit asynchronously with threads.
        for (List<MetricDatum> dataSubset : dataPartitions) {
            cloudWatchFutures.add(cloudWatch.putMetricDataAsync(
                    new PutMetricDataRequest().withNamespace(metricNamespace).withMetricData(dataSubset)));
        }

        // Wait for CloudWatch putMetricData futures to be fulfilled.
        for (Future<?> cloudWatchFuture : cloudWatchFutures) {
            // We can't let an exception leak out of here, or else the reporter will cease running as described in
            // java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit unit)
            try {
                // See what happened in case of an error.
                cloudWatchFuture.get();
            } catch (Exception e) {
                LOG.error("Exception reporting metrics to CloudWatch. The data in this CloudWatch API request "
                        + "may have been discarded, did not make it to CloudWatch.", e);
            }
        }

        LOG.debug("Sent {} metric data to CloudWatch. namespace: {}", filtered.size(), metricNamespace);

    } catch (RuntimeException e) {
        LOG.error("Error marshalling CloudWatch metrics.", e);
    }
}

From source file:org.lmnl.xml.XMLImportHandlerTest.java

@Test
public void showTextContents() throws IOException {
    final String resource = "george-algabal-tei.xml";
    //final String resource = "homer-iliad-tei.xml";
    final Text source = source(resource);
    final Text document = document(resource);

    final int textLength = textRepository.length(document);
    assertTrue(textLength > 0);/*from  w w w  .j  av a2 s  . c  o m*/

    if (LOG.isDebugEnabled()) {
        final SortedMap<String, Annotation> annotations = Maps.newTreeMap();
        for (Annotation annotation : annotationRepository.find(document)) {
            final Object data = annotation.getData();
            if (data == null || !Map.class.isAssignableFrom(data.getClass())) {
                LOG.debug(annotation + " has no attributes");
                continue;
            }
            @SuppressWarnings("unchecked")
            final Map<QName, String> attrs = (Map<QName, String>) data;
            final String nodePath = attrs.get(XMLParser.NODE_PATH_NAME);
            if (nodePath == null) {
                LOG.debug(annotation + " has no XML node path");
                continue;
            }
            if (annotations.containsKey(nodePath)) {
                LOG.debug(nodePath + " already assigned to " + annotations.get(nodePath));
            }
            annotations.put(nodePath, annotation);
        }
        for (Map.Entry<String, Annotation> annotation : annotations.entrySet()) {
            LOG.debug(annotation.getKey() + " ==> " + annotation.getValue());
        }

        if (LOG.isDebugEnabled()) {
            textRepository.read(document, new TextContentReader() {

                public void read(Reader content, int contentLength) throws IOException {
                    LOG.debug(CharStreams.toString(content));
                }
            });
        }

        final List<Range> textRanges = Lists.newArrayList();
        final List<Range> sourceRanges = Lists.newArrayList();

        for (Annotation offset : annotationRepository.find(document, OFFSET_DELTA_NAME)) {
            textRanges.add(offset.getRange());
            sourceRanges.add((Range) offset.getData());
        }

        final SortedMap<Range, String> texts = textRepository.bulkRead(document, Sets.newTreeSet(textRanges));
        final SortedMap<Range, String> sources = textRepository.bulkRead(source, Sets.newTreeSet(sourceRanges));

        final Iterator<Range> sourceRangesIt = sourceRanges.iterator();
        for (Range textRange : textRanges) {
            if (!sourceRangesIt.hasNext()) {
                break;
            }
            final Range sourceRange = sourceRangesIt.next();
            //LOG.debug(textRange + " ==> " + sourceRange);
            LOG.debug(texts.get(textRange) + " ==> " + sources.get(sourceRange));
        }
    }

}

From source file:com.tripit.auth.OAuthCredential.java

@SuppressWarnings("unchecked")
private SortedMap<String, String> generateOAuthParameters(String url, SortedMap<String, String> params)
        throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException {
    TreeMap<String, String> oauthParameters = new TreeMap<String, String>();
    oauthParameters.put("oauth_consumer_key", consumerKey);
    oauthParameters.put("oauth_nonce", generateNonce());
    oauthParameters.put("oauth_timestamp", generateTimestamp());
    oauthParameters.put("oauth_signature_method", OAUTH_SIGNATURE_METHOD);
    oauthParameters.put("oauth_version", OAUTH_VERSION);

    if (userKey != null) {
        oauthParameters.put("oauth_token", userKey);
    }//from   w  w  w .  j  a  v  a 2s  .c  o m

    if (requestorId != null) {
        oauthParameters.put("xoauth_requestor_id", requestorId);
    }

    SortedMap<String, String> oauthParametersForBaseString = (SortedMap<String, String>) oauthParameters
            .clone();
    if (params != null) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            oauthParametersForBaseString.put(param.getKey(), param.getValue());
        }
    }

    oauthParameters.put("oauth_signature", generateSignature(url, oauthParametersForBaseString));

    return oauthParameters;
}

From source file:org.apache.james.mailbox.maildir.MaildirFolder.java

public SortedMap<Long, MaildirMessageName> getUidMap(MailboxSession session, FilenameFilter filter, long from,
        long to) throws MailboxException {
    SortedMap<Long, MaildirMessageName> allUids = getUidMap(session, from, to);
    SortedMap<Long, MaildirMessageName> filteredUids = new TreeMap<Long, MaildirMessageName>();
    for (Entry<Long, MaildirMessageName> entry : allUids.entrySet()) {
        if (filter.accept(null, entry.getValue().getFullName()))
            filteredUids.put(entry.getKey(), entry.getValue());
    }/*from ww  w .  j a v a  2 s  .c  o m*/
    return filteredUids;
}

From source file:org.apache.accumulo.server.master.balancer.HostRegexTableLoadBalancer.java

@Override
public long balance(SortedMap<TServerInstance, TabletServerStatus> current, Set<KeyExtent> migrations,
        List<TabletMigration> migrationsOut) {
    long minBalanceTime = 5 * 1000;
    // Iterate over the tables and balance each of them
    TableOperations t = getTableOperations();
    if (t == null)
        return minBalanceTime;

    Map<String, SortedMap<TServerInstance, TabletServerStatus>> currentGrouped = splitCurrentByRegex(current);
    if ((System.currentTimeMillis() - this.lastOOBCheck) > this.oobCheckMillis) {
        try {//from  www .  j av a  2  s.  com
            // Check to see if a tablet is assigned outside the bounds of the pool. If so, migrate it.
            for (Entry<TServerInstance, TabletServerStatus> e : current.entrySet()) {
                for (String table : poolNameToRegexPattern.keySet()) {
                    // pool names are the same as table names, except in the DEFAULT case.
                    // If this table is assigned to a pool for this host, then move on.
                    if (getPoolNamesForHost(e.getKey().host()).contains(table)) {
                        continue;
                    }
                    String tid = t.tableIdMap().get(table);
                    if (null == tid) {
                        LOG.warn(
                                "Unable to check for out of bounds tablets for table {}, it may have been deleted or renamed.",
                                table);
                        continue;
                    }
                    try {
                        List<TabletStats> outOfBoundsTablets = getOnlineTabletsForTable(e.getKey(), tid);
                        if (null == outOfBoundsTablets) {
                            continue;
                        }
                        Random random = new Random();
                        for (TabletStats ts : outOfBoundsTablets) {
                            KeyExtent ke = new KeyExtent(ts.getExtent());
                            if (migrations.contains(ke)) {
                                LOG.debug("Migration for  out of bounds tablet {} has already been requested",
                                        ke);
                                continue;
                            }
                            String poolName = getPoolNameForTable(table);
                            SortedMap<TServerInstance, TabletServerStatus> currentView = currentGrouped
                                    .get(poolName);
                            if (null != currentView) {
                                int skip = random.nextInt(currentView.size());
                                Iterator<TServerInstance> iter = currentView.keySet().iterator();
                                for (int i = 0; i < skip; i++) {
                                    iter.next();
                                }
                                TServerInstance nextTS = iter.next();
                                LOG.info(
                                        "Tablet {} is currently outside the bounds of the regex, migrating from {} to {}",
                                        ke, e.getKey(), nextTS);
                                migrationsOut.add(new TabletMigration(ke, e.getKey(), nextTS));
                                if (migrationsOut.size() > this.maxTServerMigrations) {
                                    break;
                                }
                            } else {
                                LOG.warn(
                                        "No tablet servers online for pool {}, unable to migrate out of bounds tablets",
                                        poolName);
                            }
                        }
                    } catch (TException e1) {
                        LOG.error("Error in OOB check getting tablets for table {} from server {}", tid,
                                e.getKey().host(), e);
                    }
                }
            }
        } finally {
            this.lastOOBCheck = System.currentTimeMillis();
        }
    }

    if (migrationsOut.size() > 0) {
        LOG.warn("Not balancing tables due to moving {} out of bounds tablets", migrationsOut.size());
        return minBalanceTime;
    }

    if (migrations != null && migrations.size() > 0) {
        LOG.warn("Not balancing tables due to {} outstanding migrations", migrations.size());
        return minBalanceTime;
    }

    for (String s : t.tableIdMap().values()) {
        String tableName = tableIdToTableName.get(s);
        String regexTableName = getPoolNameForTable(tableName);
        SortedMap<TServerInstance, TabletServerStatus> currentView = currentGrouped.get(regexTableName);
        if (null == currentView) {
            LOG.warn(
                    "Skipping balance for table {} as no tablet servers are online, will recheck for online tservers at {} ms intervals",
                    tableName, this.poolRecheckMillis);
            continue;
        }
        ArrayList<TabletMigration> newMigrations = new ArrayList<TabletMigration>();
        long tableBalanceTime = getBalancerForTable(s).balance(currentView, migrations, newMigrations);
        if (tableBalanceTime < minBalanceTime) {
            minBalanceTime = tableBalanceTime;
        }
        migrationsOut.addAll(newMigrations);
        if (migrationsOut.size() > this.maxTServerMigrations) {
            break;
        }
    }

    return minBalanceTime;
}

From source file:org.apache.nifi.processors.standard.HashAttribute.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();//from   w ww. ja  v a2  s .c  om
    if (flowFile == null) {
        return;
    }

    final Map<String, Pattern> patterns = regexMapRef.get();
    final ComponentLog logger = getLogger();

    final SortedMap<String, String> attributes = getRelevantAttributes(flowFile, patterns);
    if (attributes.size() != patterns.size()) {
        final Set<String> wantedKeys = patterns.keySet();
        final Set<String> foundKeys = attributes.keySet();
        final StringBuilder missingKeys = new StringBuilder();
        for (final String wantedKey : wantedKeys) {
            if (!foundKeys.contains(wantedKey)) {
                missingKeys.append(wantedKey).append(" ");
            }
        }

        logger.error("routing {} to 'failure' because of missing attributes: {}",
                new Object[] { flowFile, missingKeys.toString() });
        session.transfer(flowFile, REL_FAILURE);
    } else {
        // create single string of attribute key/value pairs to use for group ID hash
        final StringBuilder hashableValue = new StringBuilder();
        for (final Map.Entry<String, String> entry : attributes.entrySet()) {
            hashableValue.append(entry.getKey());
            if (StringUtils.isBlank(entry.getValue())) {
                hashableValue.append("EMPTY");
            } else {
                hashableValue.append(entry.getValue());
            }
        }

        // create group ID
        final String hashValue = DigestUtils.md5Hex(hashableValue.toString());

        logger.info("adding Hash Value {} to attributes for {} and routing to success",
                new Object[] { hashValue, flowFile });
        flowFile = session.putAttribute(flowFile, context.getProperty(HASH_VALUE_ATTRIBUTE).getValue(),
                hashValue);
        session.getProvenanceReporter().modifyAttributes(flowFile);
        session.transfer(flowFile, REL_SUCCESS);
    }
}

From source file:org.mc4j.ems.impl.jmx.connection.DConnection.java

@SuppressWarnings({ "unchecked" })
public synchronized void loadSynchronous(boolean deep) {

    if (!connectionProvider.isConnected())
        connectionProvider.connect();/*  w  ww  . j  a  v a 2  s . c om*/

    log.info("Querying MBeanServer for all MBeans...");

    MBeanServer mBeanServer = connectionProvider.getMBeanServer();
    Set<ObjectName> objectNames = null;
    try {
        objectNames = (Set<ObjectName>) mBeanServer.queryNames(new ObjectName("*:*"), null);
    } catch (MalformedObjectNameException e) {
        /* Should never happen */ }

    SortedMap<ObjectName, DMBean> retrievedBeansMap = new TreeMap<ObjectName, DMBean>(
            new ObjectNameComparator());

    if (!loaded) {
        log.info("Found " + objectNames.size() + " MBeans - starting load...");
    }

    Set<DObjectName> currentKeys = new HashSet<DObjectName>(this.beanMap.keySet());

    for (ObjectName objectName : objectNames) {

        // TODO: We're loading the beans on every run here i think... only load it if its not in the beanMap

        DMBean bean = mapBean(objectName, deep);
        retrievedBeansMap.put(objectName, bean);
    }

    Set<EmsBean> newBeans = new HashSet<EmsBean>();
    Set<EmsBean> removedBeans = new HashSet<EmsBean>();

    for (Map.Entry<ObjectName, DMBean> entry : retrievedBeansMap.entrySet()) {
        if (!currentKeys.contains(entry.getKey())) {
            newBeans.add(entry.getValue());
        }
    }

    for (DObjectName name : currentKeys) {
        if (!retrievedBeansMap.containsKey(name.getObjectName())) {
            removedBeans.add(beanMap.remove(name));
        }
    }

    if (loaded && log.isDebugEnabled()) {
        log.debug("Added " + newBeans.size() + " and removed " + removedBeans.size() + " since previous load.");
    }

    loaded = true;
    fireRegistrationEvent(newBeans, removedBeans);
}

From source file:org.apache.falcon.resource.proxy.ExtensionManagerProxy.java

private void updateEntities(String extensionName, String jobName, SortedMap<EntityType, List<Entity>> entityMap,
        InputStream configStream, HttpServletRequest request)
        throws FalconException, IOException, JAXBException {
    List<Entity> feeds = entityMap.get(EntityType.FEED);
    List<Entity> processes = entityMap.get(EntityType.PROCESS);
    validateFeeds(feeds);/*from   www .  j av  a2 s .  c o m*/
    validateProcesses(processes);
    List<String> feedNames = new ArrayList<>();
    List<String> processNames = new ArrayList<>();

    for (Map.Entry<EntityType, List<Entity>> entry : entityMap.entrySet()) {
        for (final Entity entity : entry.getValue()) {
            final String entityType = entity.getEntityType().toString();
            final String entityName = entity.getName();
            final HttpServletRequest bufferedRequest = getEntityStream(entity, entity.getEntityType(), request);
            entityProxyUtil.proxyUpdate(entityType, entityName, Boolean.FALSE, bufferedRequest, entity);
            if (!embeddedMode) {
                super.update(bufferedRequest, entity.getEntityType().toString(), entity.getName(), currentColo,
                        Boolean.FALSE);
            }
            if (entity.getEntityType().equals(EntityType.FEED)) {
                feedNames.add(entity.getName());
            } else {
                processNames.add(entity.getName());
            }
        }
    }

    ExtensionMetaStore metaStore = ExtensionStore.getMetaStore();
    byte[] configBytes = null;
    if (configStream != null) {
        configBytes = IOUtils.toByteArray(configStream);
    }
    metaStore.updateExtensionJob(jobName, extensionName, feedNames, processNames, configBytes);
}

From source file:com.aurel.track.report.dashboard.AverageTimeToCloseItem.java

private SortedMap<Integer, SortedMap<Integer, Double>> createYearToIntervalToAvgTimeMap(Integer timeInterval,
        Set<Integer> finalStates, int selectedTimeFormat) {

    SortedMap<Integer, SortedMap<Integer, ArrayList<ReportBeanWithHistory>>> yearToIntervalToReportBeanList = createYearToIntervalToReportBeanListMap(
            timeInterval, finalStates);/*from  w ww . j  a v  a  2s  .  c  o  m*/
    SortedMap<Integer, SortedMap<Integer, Double>> yearToIntervalToAverageValue = new TreeMap<Integer, SortedMap<Integer, Double>>();

    for (Map.Entry<Integer, SortedMap<Integer, ArrayList<ReportBeanWithHistory>>> yearToIntervalEntry : yearToIntervalToReportBeanList
            .entrySet()) {
        Integer year = yearToIntervalEntry.getKey();
        SortedMap<Integer, ArrayList<ReportBeanWithHistory>> intervalToReportBeanList = yearToIntervalEntry
                .getValue();
        yearToIntervalToAverageValue.put(year, new TreeMap<Integer, Double>());
        for (Entry<Integer, ArrayList<ReportBeanWithHistory>> intervalToWorkItemsListEntry : intervalToReportBeanList
                .entrySet()) {
            Integer interval = intervalToWorkItemsListEntry.getKey();
            ArrayList<ReportBeanWithHistory> reportBeanLists = intervalToWorkItemsListEntry.getValue();
            int count = 0;
            int sumOfDaysOrWorkingHours = 0;
            for (ReportBeanWithHistory reportBean : reportBeanLists) {
                Integer stateID = getReportBeanStateID(reportBean);
                Date lastStateChangeDate = getReportBeanLastStateChange(reportBean);
                if (stateID == null || lastStateChangeDate == null) {
                    continue;
                }
                count++;
                int valueToAdd = getNumberOfDaysBetweenDates(reportBean.getWorkItemBean().getCreated(),
                        lastStateChangeDate);
                //It was closed on same day when it was created => 1 day
                if (valueToAdd == 0) {
                    valueToAdd = 1;
                }
                if (selectedTimeFormat == TIME_FORMAT.WORKING_HOURS) {
                    Double workingDaysHourse = hoursPerWorkingDayMap
                            .get(reportBean.getWorkItemBean().getProjectID());
                    if (workingDaysHourse == null) {
                        workingDaysHourse = 8.0;
                    }
                    valueToAdd *= workingDaysHourse;
                }
                sumOfDaysOrWorkingHours += valueToAdd;
            }
            Double avg = 0.0;
            if (count > 0) {
                avg = (double) sumOfDaysOrWorkingHours / count;
            }
            yearToIntervalToAverageValue.get(year).put(interval, avg);
        }
    }
    return yearToIntervalToAverageValue;
}

From source file:org.apache.cassandra.index.sasi.disk.TokenTreeTest.java

public void buildSerializeAndIterate(TokenTreeBuilder builder, SortedMap<Long, LongSet> tokenMap)
        throws Exception {

    builder.finish();//  w  w  w.j av  a 2  s  .  co m
    final File treeFile = File.createTempFile("token-tree-iterate-test1", "tt");
    treeFile.deleteOnExit();

    try (SequentialWriter writer = new SequentialWriter(treeFile, 4096, BufferType.ON_HEAP)) {
        builder.write(writer);
        writer.sync();
    }

    final RandomAccessReader reader = RandomAccessReader.open(treeFile);
    final TokenTree tokenTree = new TokenTree(new MappedBuffer(reader));

    final Iterator<Token> tokenIterator = tokenTree.iterator(KEY_CONVERTER);
    final Iterator<Map.Entry<Long, LongSet>> listIterator = tokenMap.entrySet().iterator();
    while (tokenIterator.hasNext() && listIterator.hasNext()) {
        Token treeNext = tokenIterator.next();
        Map.Entry<Long, LongSet> listNext = listIterator.next();

        Assert.assertEquals(listNext.getKey(), treeNext.get());
        Assert.assertEquals(convert(listNext.getValue()), convert(treeNext));
    }

    Assert.assertFalse("token iterator not finished", tokenIterator.hasNext());
    Assert.assertFalse("list iterator not finished", listIterator.hasNext());

    reader.close();
}