Example usage for java.util Map forEach

List of usage examples for java.util Map forEach

Introduction

In this page you can find the example usage for java.util Map forEach.

Prototype

default void forEach(BiConsumer<? super K, ? super V> action) 

Source Link

Document

Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

Usage

From source file:com.streamsets.pipeline.stage.origin.restservice.RestServiceReceiver.java

private Record createEmptyPayloadRecord(HttpServletRequest req) {
    Map<String, String> customHeaderAttributes = getCustomHeaderAttributes(req);
    Record placeholderRecord = getContext().createRecord("emptyPayload");
    placeholderRecord.set(Field.createListMap(new LinkedHashMap<>()));
    customHeaderAttributes.forEach((key, value) -> placeholderRecord.getHeader().setAttribute(key, value));
    placeholderRecord.getHeader().setAttribute(EMPTY_PAYLOAD_RECORD_HEADER_ATTR_NAME, "true");
    return placeholderRecord;
}

From source file:org.talend.dataprep.transformation.actions.date.DateParser.java

/**
 * Guess the pattern from the given value.
 *
 * @param value the value to get the date time from.
 * @param column the column metadata//w w w . j av a2  s.  co  m
 * @return the wanted parsed date time. For date only value, time is set to 00:00:00.
 */
DatePattern guessPattern(String value, ColumnMetadata column) {
    if (StringUtils.isEmpty(value)) {
        throw new DateTimeException("No pattern can be found out of '" + value + "'");
    }
    // call DQ on the given value
    try (Analyzer<Analyzers.Result> analyzer = analyzerService.build(column,
            AnalyzerService.Analysis.PATTERNS)) {
        analyzer.analyze(value);
        analyzer.end();

        // only one value --> only one result
        final Analyzers.Result result = analyzer.getResult().get(0);
        if (result.exist(PatternFrequencyStatistics.class)) {
            final PatternFrequencyStatistics patternFrequencyStatistics = result
                    .get(PatternFrequencyStatistics.class);
            final Map<String, Long> topTerms = patternFrequencyStatistics.getTopK(1);
            List<PatternFrequency> patterns = new ArrayList<>(1);
            topTerms.forEach((s, o) -> patterns.add(new PatternFrequency(s, o)));

            // get & check the results
            final List<DatePattern> results = getPatterns(patterns);
            if (results.isEmpty()) {
                throw new DateTimeException("DQ did not find any pattern for '" + value + "'");
            }

            // as Christopher L. said : "there can be only one" :-)
            return getPatterns(patterns).get(0);
        } else {
            throw new DateTimeException("DQ did not find any pattern for '" + value + "'");
        }
    } catch (Exception e) {
        throw new DateTimeException("Unable to close analyzer after analyzing value '" + value + "'", e);
    }
}

From source file:org.apache.tinkerpop.gremlin.console.groovy.plugin.DriverRemoteAcceptor.java

@Override
public Object configure(final List<String> args) throws RemoteException {
    final String option = args.size() == 0 ? "" : args.get(0);
    if (!POSSIBLE_TOKENS.contains(option))
        throw new RemoteException(String.format("The 'config' option expects one of ['%s'] as an argument",
                String.join(",", POSSIBLE_TOKENS)));

    final List<String> arguments = args.subList(1, args.size());

    if (option.equals(TOKEN_TIMEOUT)) {
        final String errorMessage = "The timeout option expects a positive integer representing milliseconds or 'max' as an argument";
        if (arguments.size() != 1)
            throw new RemoteException(errorMessage);
        try {/*from w  w  w.  ja v  a 2 s  .  c  o m*/
            final int to = arguments.get(0).equals(TOKEN_MAX) ? Integer.MAX_VALUE
                    : Integer.parseInt(arguments.get(0));
            if (to <= 0)
                throw new RemoteException(errorMessage);
            this.timeout = to;
            return "Set remote timeout to " + to + "ms";
        } catch (Exception ignored) {
            throw new RemoteException(errorMessage);
        }
    } else if (option.equals(TOKEN_ALIAS)) {
        if (arguments.size() == 1 && arguments.get(0).equals(TOKEN_RESET)) {
            aliases.clear();
            return "Aliases cleared";
        }

        if (arguments.size() == 1 && arguments.get(0).equals(TOKEN_SHOW)) {
            return aliases;
        }

        if (arguments.size() % 2 != 0)
            throw new RemoteException(
                    "Arguments to alias must be 'reset' to clear any existing alias settings or key/value alias/binding pairs");

        final Map<String, Object> aliasMap = ElementHelper.asMap(arguments.toArray());
        aliases.clear();
        aliasMap.forEach((k, v) -> aliases.put(k, v.toString()));
        return aliases;
    }

    return this.toString();
}

From source file:at.grahsl.kafka.connect.mongodb.MongoDbSinkTask.java

@Override
public void put(Collection<SinkRecord> records) {

    if (records.isEmpty()) {
        LOGGER.debug("no sink records to process for current poll operation");
        return;//from w  ww  . jav  a2s .c om
    }

    Map<String, MongoDbSinkRecordBatches> batchMapping = createSinkRecordBatchesPerTopic(records);

    batchMapping.forEach((namespace, batches) -> {

        String collection = StringUtils.substringAfter(namespace,
                MongoDbSinkConnectorConfig.MONGODB_NAMESPACE_SEPARATOR);

        batches.getBufferedBatches().forEach(batch -> {
            processSinkRecords(cachedCollections.get(namespace), batch);
            MongoDbSinkConnectorConfig.RateLimitSettings rls = rateLimitSettings.getOrDefault(collection,
                    rateLimitSettings.get(MongoDbSinkConnectorConfig.TOPIC_AGNOSTIC_KEY_NAME));
            if (rls.isTriggered()) {
                LOGGER.debug(
                        "rate limit settings triggering {}ms defer timeout"
                                + " after processing {} further batches for collection {}",
                        rls.getTimeoutMs(), rls.getEveryN(), collection);
                try {
                    Thread.sleep(rls.getTimeoutMs());
                } catch (InterruptedException e) {
                    LOGGER.error(e.getMessage());
                }
            }
        });
    });

}

From source file:org.commonjava.cartographer.discover.indy.CartoAliasingStartupAction.java

@Override
public void start() throws AppLifecycleException {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.info("STARTUP/begin: Alias initialization");

    String baseUrl = config.getIndyUrl();
    if (!isEmpty(baseUrl)) {
        logger.info("Retrieving endpoints from Indy at: {} in order to auto-alias...", baseUrl);
        try {/* w  ww.  j  a  va 2s  .  com*/
            Indy indy = new Indy(baseUrl).connect();
            EndpointViewListing endpoints = indy.stats().getAllEndpoints();
            for (EndpointView epv : endpoints.getItems()) {
                logger.info("Alias Indy '{}' => {}", epv.getKey(), epv.getResourceUri());
                sourceManager.addSourceAlias(epv.getKey(), epv.getResourceUri());
            }
        } catch (IndyClientException e) {
            throw new AppLifecycleException("Failed to read repositories from Indy at: %s. Reason: %s", e,
                    baseUrl, e.getMessage());
        } catch (CartoException e) {
            throw new AppLifecycleException("Failed to add alias from Indy at: %s. Reason: %s", e, baseUrl,
                    e.getMessage());
        }
    } else {
        logger.info("No Indy server configured. Skipping auto-aliasing step.");
    }

    Map<String, String> explicitAliases = config.getExplicitAliases();
    List<String> errors = new ArrayList<>();
    if (explicitAliases != null) {
        logger.info("Adding explicit aliases from configuration...");
        explicitAliases.forEach((alias, url) -> {
            logger.info("Alias '{}' => {}", alias, url);
            try {
                sourceManager.addSourceAlias(alias, url);
            } catch (CartoException e) {
                errors.add(String.format("%s -> %s (Reason: %s)", e, alias, url, e.getMessage()));
            }
        });
    }

    if (!errors.isEmpty()) {
        throw new AppLifecycleException("Failed to add aliases:\n  %s", StringUtils.join(errors, "\n  "));
    }

    logger.info("STARTUP/done: Alias initialization complete.");
}

From source file:com.adobe.ags.curly.controller.BatchRunner.java

private void buildTasks(List<Action> actions, List<Map<String, String>> batchData,
        Map<String, StringProperty> defaultValues, Set<String> displayColumns) {
    int row = 0;/*from w  ww  . ja  v  a  2  s .  com*/
    for (Map<String, String> data : batchData) {
        row++;
        try {
            Map<String, String> values = new HashMap<>(data);
            defaultValues.forEach((key, value) -> {
                if (values.get(key) == null || values.get(key).isEmpty()) {
                    values.put(key, value.get());
                }
            });
            ActionGroupRunner runner = new ActionGroupRunner("Row " + row, this::getConnection, actions, values,
                    displayColumns);
            result.addDetail(runner.results);
            executor.execute(runner);
        } catch (ParseException ex) {
            Logger.getLogger(BatchRunner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:io.syndesis.rest.v1.handler.connection.ConnectionActionHandler.java

@POST
@Path(value = "/{id}")
@Produces(MediaType.APPLICATION_JSON)/*from ww w  .j av  a 2 s . c  om*/
@ApiOperation("Retrieves enriched action definition, that is an action definition that has input/output data shapes and property enums defined with respect to the given action properties")
@ApiResponses(@ApiResponse(code = 200, response = ActionDefinition.class, message = "A map of zero or more action property suggestions keyed by the property name"))
public ActionDefinition enrichWithMetadata(
        @PathParam("id") @ApiParam(required = true, example = "io.syndesis:salesforce-create-or-update:latest") final String id,
        final Map<String, String> properties) {

    final Action action = actions.stream().filter(a -> a.idEquals(id)).findAny()
            .orElseThrow(() -> new EntityNotFoundException("Action with id: " + id));

    final ActionDefinition defaultDefinition = action.getDefinition();

    if (!action.getTags().contains("dynamic")) {
        return defaultDefinition;
    }

    final String connectorId = connector.getId().get();

    final Map<String, String> parameters = new HashMap<>(
            Optional.ofNullable(properties).orElseGet(HashMap::new));
    // put all action parameters with `null` values
    defaultDefinition.getPropertyDefinitionSteps()
            .forEach(step -> step.getProperties().forEach((k, v) -> parameters.putIfAbsent(k, null)));

    // lastly put all connection properties
    parameters.putAll(connection.getConfiguredProperties());

    final Client client = createClient();
    final WebTarget target = client.target(
            String.format("http://%s/api/v1/connectors/%s/actions/%s", config.getService(), connectorId, id));

    final ActionDefinition.Builder enriched = new ActionDefinition.Builder().createFrom(defaultDefinition);
    final DynamicActionMetadata dynamicActionMetadata = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(parameters, MediaType.APPLICATION_JSON), DynamicActionMetadata.class);

    final Map<String, List<DynamicActionMetadata.ActionPropertySuggestion>> actionPropertySuggestions = dynamicActionMetadata
            .properties();
    actionPropertySuggestions.forEach((k, vals) -> enriched.replaceConfigurationProperty(k, b -> b.addAllEnum(
            vals.stream().map(s -> ConfigurationProperty.PropertyValue.Builder.from(s))::iterator)));

    //Setting the defaultValue as suggested by the metadata
    for (Entry<String, List<ActionPropertySuggestion>> suggestions : actionPropertySuggestions.entrySet()) {
        if (suggestions.getValue().size() == 1) {
            for (DynamicActionMetadata.ActionPropertySuggestion suggestion : suggestions.getValue()) {
                enriched.replaceConfigurationProperty(suggestion.displayValue(),
                        v -> v.defaultValue(suggestion.value()));
            }
        }
    }

    final Object input = dynamicActionMetadata.inputSchema();
    if (shouldEnrichDataShape(defaultDefinition.getInputDataShape(), input)) {
        enriched.inputDataShape(new DataShape.Builder().type(typeFrom(input)).kind("json-schema")
                .specification(specificationFrom(input)).build());
    }

    final Object output = dynamicActionMetadata.outputSchema();
    if (shouldEnrichDataShape(defaultDefinition.getOutputDataShape(), output)) {
        enriched.outputDataShape(new DataShape.Builder().type(typeFrom(output)).kind("json-schema")
                .specification(specificationFrom(output)).build());
    }

    return enriched.build();
}

From source file:com.qwazr.scheduler.SchedulerManager.java

TreeMap<String, String> getSchedulers() {
    etcTracker.check();/*from   w w  w  . j  a  v a  2 s  .co m*/
    final TreeMap<String, String> map = new TreeMap<String, String>();
    final Map<String, SchedulerDefinition> scMap = schedulerMap;
    if (scMap == null)
        return map;
    scMap.forEach(
            (name, schedulerDef) -> map.put(name, ClusterManager.INSTANCE.myAddress + "/schedulers/" + name));
    return map;
}

From source file:org.apache.samza.system.kinesis.consumer.KinesisSystemConsumer.java

@Override
public void afterCheckpoint(Map<SystemStreamPartition, String> sspOffsets) {
    LOG.info("afterCheckpoint called with sspOffsets {}", sspOffsets);
    sspOffsets.forEach((ssp, offset) -> {
        KinesisRecordProcessor processor = processors.get(ssp);
        KinesisSystemConsumerOffset kinesisOffset = KinesisSystemConsumerOffset.parse(offset);
        if (processor == null) {
            LOG.info(//  w ww . jav a2 s.  co  m
                    "Kinesis Processor is not alive for ssp {}. This could be the result of rebalance. Hence dropping the"
                            + " checkpoint {}.",
                    ssp, offset);
        } else if (!kinesisOffset.getShardId().equals(processor.getShardId())) {
            LOG.info(
                    "KinesisProcessor for ssp {} currently owns shard {} while the checkpoint is for shard {}. This could"
                            + " be the result of rebalance. Hence dropping the checkpoint {}.",
                    ssp, processor.getShardId(), kinesisOffset.getShardId(), offset);
        } else {
            processor.checkpoint(kinesisOffset.getSeqNumber());
        }
    });
}

From source file:com.github.blindpirate.gogradle.task.go.GoTest.java

private List<TestClassResult> doTest() {
    List<TestClassResult> ret = new ArrayList<>();

    Map<File, List<File>> parentDirToTestFiles = determineTestPattern();

    parentDirToTestFiles.forEach((parentDir, testFiles) -> {
        String packageImportPath = dirToImportPath(parentDir);
        PackageTestResult result = doSingleTest(packageImportPath, testFiles);

        List<TestClassResult> resultOfSinglePackage = extractor.extractTestResult(result);
        logResult(packageImportPath, resultOfSinglePackage);
        ret.addAll(resultOfSinglePackage);
    });/*from  ww  w.j av a  2 s. c  om*/
    return ret;
}