Example usage for java.util.stream Collectors toMap

List of usage examples for java.util.stream Collectors toMap

Introduction

In this page you can find the example usage for java.util.stream Collectors toMap.

Prototype

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) 

Source Link

Document

Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.

Usage

From source file:com.vmware.admiral.compute.container.volume.VolumeUtil.java

/**
 * Creates additional affinity rules between container descriptions which share
 * local volumes. Each container group should be deployed on a single host.
 */// www.j  a  v a 2 s.com
public static void applyLocalNamedVolumeConstraints(Collection<ComponentDescription> componentDescriptions) {

    Map<String, ContainerVolumeDescription> volumes = filterDescriptions(ContainerVolumeDescription.class,
            componentDescriptions);

    List<String> localVolumes = volumes.values().stream().filter(v -> DEFAULT_VOLUME_DRIVER.equals(v.driver))
            .map(v -> v.name).collect(Collectors.toList());

    if (localVolumes.isEmpty()) {
        return;
    }

    Map<String, ContainerDescription> containers = filterDescriptions(ContainerDescription.class,
            componentDescriptions);

    // sort containers by local volume: each set is a group of container names
    // that share a particular local volume
    List<Set<String>> localVolumeContainers = localVolumes.stream()
            .map(v -> filterByVolume(v, containers.values())).filter(s -> !s.isEmpty())
            .collect(Collectors.toList());

    if (localVolumeContainers.isEmpty()) {
        return;
    }

    /** Merge sets of containers sharing local volumes
     *
     *  C1  C2  C3  C4  C5  C6
     *   \  /\  /   |    \  /
     *    L1  L2    L3    L4
     *
     *    Input: [C1, C2], [C2, C3], [C4], [C5, C6]
     *    Output: [C1, C2, C3], [C4], [C5, C6]
     */
    localVolumeContainers = mergeSets(localVolumeContainers);

    Map<String, List<ContainerVolumeDescription>> containerToVolumes = containers.values().stream()
            .collect(Collectors.toMap(cd -> cd.name, cd -> filterVolumes(cd, volumes.values())));

    Map<String, Integer> containerToDriverCount = containerToVolumes.entrySet().stream()
            .collect(Collectors.toMap(e -> e.getKey(),
                    e -> e.getValue().stream().map(vd -> vd.driver).collect(Collectors.toSet()).size()));

    for (Set<String> s : localVolumeContainers) {
        if (s.size() > 1) {
            // find the container with highest number of required drivers
            int max = s.stream().map(cn -> containerToDriverCount.get(cn))
                    .max((vc1, vc2) -> Integer.compare(vc1, vc2)).get();
            Set<String> maxDrivers = s.stream().filter(cn -> containerToDriverCount.get(cn) == max)
                    .collect(Collectors.toSet());

            String maxCont = maxDrivers.iterator().next();
            s.remove(maxCont);
            s.stream().forEach(cn -> addAffinity(maxCont, containers.get(cn)));
        }
    }
}

From source file:com.epam.ta.reportportal.events.handler.TicketActivitySubscriber.java

@EventListener
public void onTicketAttached(TicketAttachedEvent event) {
    List<Activity> activities = new ArrayList<>();
    String separator = ",";
    Iterable<TestItem> testItems = event.getBefore();
    Map<String, Activity.FieldValues> results = StreamSupport.stream(testItems.spliterator(), false)
            .filter(item -> null != item.getIssue())
            .collect(Collectors.toMap(TestItem::getId, item -> Activity.FieldValues.newOne()
                    .withOldValue(issuesIdsToString(item.getIssue().getExternalSystemIssues(), separator))));

    Iterable<TestItem> updated = event.getAfter();

    for (TestItem testItem : updated) {
        if (null == testItem.getIssue())
            continue;
        Activity.FieldValues fieldValues = results.get(testItem.getId());
        fieldValues.withNewValue(issuesIdsToString(testItem.getIssue().getExternalSystemIssues(), separator));

        Activity activity = activityBuilder.get().addProjectRef(event.getProject()).addActionType(ATTACH_ISSUE)
                .addLoggedObjectRef(testItem.getId()).addObjectType(TestItem.TEST_ITEM)
                .addUserRef(event.getPostedBy()).addHistory(ImmutableMap.<String, Activity.FieldValues>builder()
                        .put(TICKET_ID, fieldValues).build())
                .build();/*from  w w  w .  ja va2s  .  c o  m*/
        activities.add(activity);
    }
    activityRepository.save(activities);
}

From source file:io.github.retz.scheduler.Launcher.java

private static void maybeRequeueRunningJobs(String master, String frameworkId, List<Job> running) {
    LOG.info("{} jobs found in DB 'STARTING' or 'STARTED' state. Requeuing...", running.size());
    int offset = 0;
    int limit = 128;
    Map<String, Job> runningMap = running.stream().collect(Collectors.toMap(job -> job.taskId(), job -> job));
    List<Job> recoveredJobs = new LinkedList<>();
    while (true) {
        try {//from w ww  .  ja  va2 s .co  m
            List<Map<String, Object>> tasks = MesosHTTPFetcher.fetchTasks(master, frameworkId, offset, limit);
            if (tasks.isEmpty()) {
                break;
            }

            for (Map<String, Object> task : tasks) {
                String state = (String) task.get("state");
                // Get TaskId
                String taskId = (String) task.get("id");
                if (runningMap.containsKey(taskId)) {
                    Job job = runningMap.remove(taskId);
                    recoveredJobs.add(JobQueue.updateJobStatus(job, state));
                } else {
                    LOG.warn("Unknown job!");
                }
            }
            offset = offset + tasks.size();
        } catch (MalformedURLException e) {
            LOG.error(e.toString());
            throw new RuntimeException(e.toString());
        }
    }
    Database.getInstance().updateJobs(recoveredJobs);
    LOG.info("{} jobs rescheduled, {} jobs didn't need change.", recoveredJobs.size(), runningMap.size());
}

From source file:edu.zipcloud.cloudstreetmarket.api.services.StockProductServiceOnlineImpl.java

private void updateStocksAndQuotesFromYahoo(Set<StockProduct> askedContent) {
    if (askedContent.isEmpty()) {
        return;/*from   ww  w .  j a  va 2  s.co m*/
    }

    Set<StockProduct> recentlyUpdated = askedContent.stream()
            .filter(t -> t.getLastUpdate() != null && DateUtil.isRecent(t.getLastUpdate(), 1))
            .collect(Collectors.toSet());

    if (askedContent.size() != recentlyUpdated.size()) {

        String guid = AuthenticationUtil.getPrincipal().getUsername();
        SocialUser socialUser = usersConnectionRepository.getRegisteredSocialUser(guid);
        if (socialUser == null) {
            return;
        }
        String token = socialUser.getAccessToken();
        Connection<Yahoo2> connection = connectionRepository.getPrimaryConnection(Yahoo2.class);

        if (connection != null) {
            askedContent.removeAll(recentlyUpdated);

            Map<String, StockProduct> updatableTickers = askedContent.stream()
                    .collect(Collectors.toMap(StockProduct::getId, Function.identity()));

            List<YahooQuote> yahooQuotes = connection.getApi().financialOperations()
                    .getYahooQuotes(new ArrayList<String>(updatableTickers.keySet()), token);

            Set<StockProduct> updatableProducts = yahooQuotes.stream()
                    .filter(yq -> StringUtils.isNotBlank(yq.getExchange()))
                    .filter(yq -> updatableTickers.get(yq.getId()) != null).map(yq -> {
                        StockQuote sq = new StockQuote(yq, updatableTickers.get((yq.getId())));
                        return syncProduct(updatableTickers.get((yq.getId())), sq);
                    }).collect(Collectors.toSet());

            if (!updatableProducts.isEmpty()) {
                stockProductRepository.save(updatableProducts);
            }

            //This job below should decrease with the time
            Set<StockProduct> removableProducts = yahooQuotes.stream()
                    .filter(yq -> StringUtils.isBlank(yq.getExchange())).map(yq -> {
                        StockQuote sq = new StockQuote(yq, updatableTickers.get((yq.getId())));
                        return syncProduct(updatableTickers.get((yq.getId())), sq);
                    }).collect(Collectors.toSet());

            if (!removableProducts.isEmpty()) {
                stockProductRepository.delete(removableProducts);
            }
        }
    }
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.TaskDataFetcher.java

@Override
protected Stream<Task> dataMapping(Stream<TaskData> dataStream) {
    // TODO Task progress not accessible from DB. It implies to establish a connection with
    // the SchedulerFrontend active object to get the value that is in the Scheduler memory

    return dataStream.map(taskData -> {
        TaskData.DBTaskId id = taskData.getId();

        return Task.builder().additionalClasspath(taskData.getAdditionalClasspath())
                .description(taskData.getDescription()).executionDuration(taskData.getExecutionDuration())
                .executionHostname(taskData.getExecutionHostName()).finishedTime(taskData.getFinishedTime())
                .genericInformation(taskData.getGenericInformation()).id(id.getTaskId())
                .inErrorTime(taskData.getInErrorTime()).javaHome(taskData.getJavaHome()).jobId(id.getJobId())
                .jvmArguments(taskData.getJvmArguments())
                .maxNumberOfExecution(taskData.getMaxNumberOfExecution()).name(taskData.getTaskName())
                .numberOfExecutionLeft(taskData.getNumberOfExecutionLeft())
                .numberOfExecutionOnFailureLeft(taskData.getNumberOfExecutionOnFailureLeft())
                .onTaskError(/*from www. j  a  v  a  2  s  . c  o m*/
                        CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, taskData.getOnTaskErrorString()))
                .preciousLogs(taskData.isPreciousLogs()).preciousResult(taskData.isPreciousResult())
                .restartMode(RestartMode.getMode(taskData.getRestartModeId()).getDescription().toUpperCase())
                .resultPreview(taskData.getResultPreview()).runAsMe(taskData.isRunAsMe())
                .scheduledTime(taskData.getScheduledTime()).startTime(taskData.getStartTime())
                .status(taskData.getTaskStatus().name()).tag(taskData.getTag())
                .variables(taskData.getVariables().values().stream()
                        .map(taskDataVariable -> Maps.immutableEntry(taskDataVariable.getName(),
                                taskDataVariable.getValue()))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
                .workingDir(taskData.getWorkingDir()).walltime(taskData.getWallTime()).build();
    });
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.JobDataFetcher.java

@Override
protected Stream<Job> dataMapping(Stream<JobData> dataStream) {
    return dataStream.parallel()
            .map(jobData -> Job.builder()
                    .dataManagement(DataManagement.builder().globalSpaceUrl(jobData.getGlobalSpace())
                            .inputSpaceUrl(jobData.getInputSpace()).outputSpaceUrl(jobData.getOutputSpace())
                            .userSpaceUrl(jobData.getUserSpace()).build())
                    .description(jobData.getDescription()).finishedTime(jobData.getFinishedTime())
                    .genericInformation(jobData.getGenericInformation()).id(jobData.getId())
                    .inErrorTime(jobData.getInErrorTime()).lastUpdatedTime(jobData.getLastUpdatedTime())
                    .maxNumberOfExecution(jobData.getMaxNumberOfExecution()).name(jobData.getJobName())
                    .numberOfFailedTasks(jobData.getNumberOfFailedTasks())
                    .numberOfFaultyTasks(jobData.getNumberOfFaultyTasks())
                    .numberOfFinishedTasks(jobData.getNumberOfFinishedTasks())
                    .numberOfInErrorTasks(jobData.getNumberOfInErrorTasks())
                    .numberOfPendingTasks(jobData.getNumberOfPendingTasks())
                    .numberOfRunningTasks(jobData.getNumberOfRunningTasks())
                    .onTaskError(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE,
                            jobData.getOnTaskErrorString()))
                    .owner(jobData.getOwner()).priority(jobData.getPriority().name())
                    .projectName(jobData.getProjectName()).removedTime(jobData.getRemovedTime())
                    .status(jobData.getStatus().name()).startTime(jobData.getStartTime())
                    .submittedTime(jobData.getSubmittedTime())
                    .totalNumberOfTasks(jobData.getTotalNumberOfTasks())
                    // TODO Currently map the JobVariable object to a simple string (its value). Need to map the whole object later
                    .variables(jobData.getVariables() == null ? ImmutableMap.of()
                            : jobData.getVariables().entrySet().stream()
                                    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getValue())))
                    .build());// ww  w  .ja va2 s .  c o  m
}

From source file:com.github.horrorho.inflatabledonkey.chunk.engine.standard.StandardChunkEngine.java

Optional<Map<ChunkServer.ChunkReference, Chunk>> chunks(int container, List<Optional<Chunk>> chunkList) {
    if (chunkList.contains(Optional.<Chunk>empty())) {
        return Optional.empty();
    }/*  www  . j a v  a 2s  . c  o m*/

    Map<ChunkServer.ChunkReference, Chunk> chunks = IntStream.range(0, chunkList.size())
            .filter(i -> chunkList.get(i).isPresent()).mapToObj(Integer::valueOf).collect(Collectors
                    .toMap(i -> ChunkReferences.chunkReference(container, i), i -> chunkList.get(i).get()));

    return Optional.of(chunks);
}

From source file:com.orange.servicebroker.staticcreds.stories.get_syslog_drain_url.CreateLogDrainServiceBindingTest.java

@Test
public void create_service_binding_with_static_syslog_drain_url_set_at_service_plan_level() throws Exception {
    given().syslog_drain_url_set_in_catalog(
            service_broker_properties_with_syslog_drain_url_at_service_plan_level_with_requires_field_set());
    when().cloud_controller_requests_to_create_a_service_instance_binding_for_plan_id("dev-id");
    then().it_should_be_returned_with_syslog_drain_url(
            new CreateServiceInstanceAppBindingResponse().withSyslogDrainUrl("syslog://log.dev.com:5000")
                    .withCredentials(Collections.unmodifiableMap(
                            Stream.of(new AbstractMap.SimpleEntry<>("URI", "http://my-api.org"))
                                    .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())))));
}

From source file:com.netflix.spinnaker.fiat.roles.UserRolesSyncer.java

private Map<String, UserPermission> getServiceAccountsAsMap() {
    return serviceAccountProvider.getAll().stream().map(ServiceAccount::toUserPermission)
            .collect(Collectors.toMap(UserPermission::getId, Functions.identity()));
}

From source file:com.github.horrorho.inflatabledonkey.BackupAssistant.java

public Map<Device, List<Snapshot>> deviceSnapshots(HttpClient httpClient, Collection<Device> devices)
        throws IOException {

    Map<SnapshotID, Device> snapshotDevice = devices.stream().distinct()
            .flatMap(d -> d.snapshotIDs().stream().map(s -> new SimpleImmutableEntry<>(s, d)))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    List<SnapshotID> snapshotIDs = devices.stream().map(Device::snapshotIDs).flatMap(Collection::stream)
            .collect(Collectors.toList());

    return snapshots(httpClient, snapshotIDs).stream()
            .collect(Collectors.groupingBy(s -> snapshotDevice.get(s.snapshotID())));
}