Example usage for java.util Set forEach

List of usage examples for java.util Set forEach

Introduction

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

Prototype

default void forEach(Consumer<? super T> action) 

Source Link

Document

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Usage

From source file:org.apdplat.superword.tools.SentenceScorer.java

public static TreeMap<Float, Map<String, List<String>>> score(String path, int limit) {
    //?  /*  ww  w  . jav  a2s .  c  om*/
    Set<String> fileNames = TextAnalyzer.getFileNames(path);
    //?
    Map<String, AtomicInteger> frequency = TextAnalyzer.frequency(fileNames);
    //?
    TreeMap<Float, Map<String, List<String>>> sentences = new TreeMap<>();
    //??????
    Set<Integer> hashes = new HashSet<>();
    Set<String> repeat = new HashSet<>();
    //?????????
    int count = 0;
    for (String fileName : fileNames) {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new BufferedInputStream(new FileInputStream(fileName))))) {
            String book = Paths.get(fileName).toFile().getName().replace(".txt", "");
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                int hc = line.hashCode();
                if (hashes.contains(hc)) {
                    repeat.add(line);
                    continue;
                }
                hashes.add(hc);
                //
                float score = score(line, frequency);
                if (score > 0) {
                    if (count >= limit) {
                        LOGGER.debug("?????" + limit + "?");
                        return sentences;
                    }
                    count++;
                    sentences.putIfAbsent(score, new HashMap<>());
                    sentences.get(score).putIfAbsent(book, new ArrayList<>());
                    sentences.get(score).get(book).add(line);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    LOGGER.debug("????" + repeat.size());
    AtomicInteger i = new AtomicInteger();
    repeat.forEach(r -> {
        LOGGER.debug("\t" + i.incrementAndGet() + "?" + r);
    });
    LOGGER.debug("???" + count);
    return sentences;
}

From source file:org.jamocha.dn.compiler.pathblocks.PathBlocks.java

protected static List<PathRule> createOutput(final List<Either<Rule, ExistentialProxy>> rules,
        final PathBlockSet resultBlockSet) {
    final Function<? super Block, ? extends Integer> characteristicNumber = block -> block
            .getFlatFilterInstances().size() / block.getRulesOrProxies().size();
    final TreeMap<Integer, CursorableLinkedList<Block>> blockMap = resultBlockSet.getBlocks().stream()
            .collect(groupingBy(characteristicNumber, TreeMap::new, toCollection(CursorableLinkedList::new)));
    // iterate over all the filter proxies ever used
    for (final FilterProxy filterProxy : FilterProxy.getFilterProxies()) {
        final Set<ExistentialProxy> existentialProxies = filterProxy.getProxies();
        // determine the largest characteristic number of the blocks containing filter instances
        // of one of the existential proxies (choice is arbitrary, since the filters and the
        // conflicts are identical if they belong to the same filter).
        final OptionalInt optMax = resultBlockSet.getRuleInstanceToBlocks()
                .computeIfAbsent(Either.right(existentialProxies.iterator().next()), newHashSet()).stream()
                .mapToInt(composeToInt(characteristicNumber, Integer::intValue)).max();
        if (!optMax.isPresent())
            continue;
        final int eCN = optMax.getAsInt();
        // get the list to append the blocks using the existential closure filter INSTANCE to
        final CursorableLinkedList<Block> targetList = blockMap.get(eCN);
        // for every existential part
        for (final ExistentialProxy existentialProxy : existentialProxies) {
            final FilterInstance exClosure = existentialProxy.getExistentialClosure();
            // create a list storing the blocks to move
            final List<Block> toMove = new ArrayList<>();
            for (final CursorableLinkedList<Block> blockList : blockMap.headMap(eCN, true).values()) {
                // iterate over the blocks in the current list
                for (final ListIterator<Block> iterator = blockList.listIterator(); iterator.hasNext();) {
                    final Block current = iterator.next();
                    // if the current block uses the current existential closure filter
                    // INSTANCE, it has to be moved
                    if (current.getFlatFilterInstances().contains(exClosure)) {
                        iterator.remove();
                        toMove.add(current);
                    }//from  w w w .  j  av  a  2s.  c  o  m
                }
            }
            // append the blocks to be moved (they were only removed so far)
            targetList.addAll(toMove);
        }
    }
    final Set<FilterInstance> constructedFIs = new HashSet<>();
    final Map<Either<Rule, ExistentialProxy>, Map<FilterInstance, Set<FilterInstance>>> ruleToJoinedWith = new HashMap<>();
    final Map<Set<FilterInstance>, PathFilterList> joinedWithToComponent = new HashMap<>();
    // at this point, the network can be constructed
    for (final CursorableLinkedList<Block> blockList : blockMap.values()) {
        for (final Block block : blockList) {
            final List<Either<Rule, ExistentialProxy>> blockRules = Lists
                    .newArrayList(block.getRulesOrProxies());
            final Set<List<FilterInstance>> filterInstanceColumns = Block
                    .getFilterInstanceColumns(block.getFilters(), block.getRuleToFilterToRow(), blockRules);
            // since we are considering blocks, it is either the case that all filter
            // instances of the column have been constructed or none of them have
            final PathSharedListWrapper sharedListWrapper = new PathSharedListWrapper(blockRules.size());
            final Map<Either<Rule, ExistentialProxy>, PathSharedList> ruleToSharedList = IntStream
                    .range(0, blockRules.size()).boxed()
                    .collect(toMap(blockRules::get, sharedListWrapper.getSharedSiblings()::get));
            final List<List<FilterInstance>> columnsToConstruct, columnsAlreadyConstructed;
            {
                final Map<Boolean, List<List<FilterInstance>>> partition = filterInstanceColumns.stream()
                        .collect(partitioningBy(column -> Collections.disjoint(column, constructedFIs)));
                columnsAlreadyConstructed = partition.get(Boolean.FALSE);
                columnsToConstruct = partition.get(Boolean.TRUE);
            }

            if (!columnsAlreadyConstructed.isEmpty()) {
                final Map<PathSharedList, LinkedHashSet<PathFilterList>> sharedPart = new HashMap<>();
                for (final List<FilterInstance> column : columnsAlreadyConstructed) {
                    for (final FilterInstance fi : column) {
                        sharedPart
                                .computeIfAbsent(ruleToSharedList.get(fi.getRuleOrProxy()), newLinkedHashSet())
                                .add(joinedWithToComponent
                                        .get(ruleToJoinedWith.get(fi.getRuleOrProxy()).get(fi)));
                    }
                }
                sharedListWrapper.addSharedColumns(sharedPart);
            }

            for (final List<FilterInstance> column : columnsToConstruct) {
                sharedListWrapper.addSharedColumn(column.stream().collect(
                        toMap(fi -> ruleToSharedList.get(fi.getRuleOrProxy()), FilterInstance::convert)));
            }
            constructedFIs.addAll(block.getFlatFilterInstances());
            for (final Entry<Either<Rule, ExistentialProxy>, Map<Filter, FilterInstancesSideBySide>> entry : block
                    .getRuleToFilterToRow().entrySet()) {
                final Either<Rule, ExistentialProxy> rule = entry.getKey();
                final Set<FilterInstance> joined = entry.getValue().values().stream()
                        .flatMap(sbs -> sbs.getInstances().stream()).collect(toSet());
                final Map<FilterInstance, Set<FilterInstance>> joinedWithMapForThisRule = ruleToJoinedWith
                        .computeIfAbsent(rule, newHashMap());
                joined.forEach(fi -> joinedWithMapForThisRule.put(fi, joined));
                joinedWithToComponent.put(joined, ruleToSharedList.get(rule));
            }
        }
    }
    final List<PathRule> pathRules = new ArrayList<>();
    for (final Either<Rule, ExistentialProxy> either : rules) {
        if (either.isRight()) {
            continue;
        }
        final List<PathFilterList> pathFilterLists = Stream
                .concat(either.left().get().existentialProxies.values().stream().map(p -> Either.right(p)),
                        Stream.of(either))
                .flatMap(e -> ruleToJoinedWith.getOrDefault(e, Collections.emptyMap()).values().stream()
                        .distinct())
                .map(joinedWithToComponent::get).collect(toList());
        pathRules.add(either.left().get().getOriginal().toPathRule(PathFilterList.toSimpleList(pathFilterLists),
                pathFilterLists.size() > 1 ? InitialFactPathsFinder.gather(pathFilterLists)
                        : Collections.emptySet()));
    }
    return pathRules;
}

From source file:org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.java

private void scanPackage(SpringPersistenceUnitInfo scannedUnit, String pkg) {
    if (this.componentsIndex != null) {
        Set<String> candidates = new HashSet<>();
        for (AnnotationTypeFilter filter : entityTypeFilters) {
            candidates// ww  w  . jav  a2  s  . c  o  m
                    .addAll(this.componentsIndex.getCandidateTypes(pkg, filter.getAnnotationType().getName()));
        }
        candidates.forEach(scannedUnit::addManagedClassName);
        Set<String> managedPackages = this.componentsIndex.getCandidateTypes(pkg, "package-info");
        managedPackages.forEach(scannedUnit::addManagedPackage);
        return;
    }

    try {
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                + ClassUtils.convertClassNameToResourcePath(pkg) + CLASS_RESOURCE_PATTERN;
        Resource[] resources = this.resourcePatternResolver.getResources(pattern);
        MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
        for (Resource resource : resources) {
            if (resource.isReadable()) {
                MetadataReader reader = readerFactory.getMetadataReader(resource);
                String className = reader.getClassMetadata().getClassName();
                if (matchesFilter(reader, readerFactory)) {
                    scannedUnit.addManagedClassName(className);
                    if (scannedUnit.getPersistenceUnitRootUrl() == null) {
                        URL url = resource.getURL();
                        if (ResourceUtils.isJarURL(url)) {
                            scannedUnit.setPersistenceUnitRootUrl(ResourceUtils.extractJarFileURL(url));
                        }
                    }
                } else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
                    scannedUnit.addManagedPackage(
                            className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
                }
            }
        }
    } catch (IOException ex) {
        throw new PersistenceException("Failed to scan classpath for unlisted entity classes", ex);
    }
}

From source file:org.libreplan.web.orders.OrderElementTreeController.java

public void expandAll() {
    Set<Treeitem> childrenSet = new HashSet<>();
    Treechildren children = tree.getTreechildren();

    if (children != null) {
        childrenSet.addAll(children.getItems());
    }//from w ww .j  a va 2s. co m
    childrenSet.forEach(this::expandAll);
}

From source file:org.artifactory.ui.rest.service.admin.configuration.repositories.replication.ReplicationConfigService.java

private void updateAddedAndRemovedReplications(Set<LocalReplicationDescriptor> replications, String repoKey,
        MutableCentralConfigDescriptor configDescriptor) {
    log.info("Updating replication configurations for repo {}", repoKey);
    // Remove all replication configs for this repo and re-add all newly received ones and do cleanup for
    // descriptors that had their url changed
    Set<String> newUrls = replications.stream().map(LocalReplicationDescriptor::getUrl)
            .collect(Collectors.toSet());
    List<LocalReplicationDescriptor> currentLocalReplications = configDescriptor
            .getMultiLocalReplications(repoKey);
    cleanupLocalReplications(currentLocalReplications.stream()
            .filter(replication -> !newUrls.contains(replication.getUrl())).collect(Collectors.toList()));
    currentLocalReplications.forEach(configDescriptor::removeLocalReplication);
    replications.forEach(configDescriptor::addLocalReplication);
}

From source file:org.libreplan.web.orders.OrderElementTreeController.java

private void expandAll(Treeitem item) {
    item.setOpen(true);/*from w  ww  .  j a  v  a  2 s  . co  m*/

    Set<Treeitem> childrenSet = new HashSet<>();
    Treechildren children = item.getTreechildren();

    if (children != null) {
        childrenSet.addAll(children.getItems());
    }

    childrenSet.forEach(this::expandAll);
}

From source file:org.onosproject.newoptical.OpticalPathProvisioner.java

private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency,
        Set<PacketLinkRealizedByOptical> links) {
    OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement());
    OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency, links,
            Collections.emptySet());

    links.forEach(l -> linkPathMap.put(l, connectivity));

    // store connectivity information
    connectivityMap.put(connectivity.id(), connectivity);

    return connectivity;
}

From source file:org.apache.samza.execution.ExecutionPlanner.java

/**
 * Creates the physical graph from {@link ApplicationDescriptorImpl}
 *//*from   w w  w  .j  a  va2  s .  co m*/
/* package private */
JobGraph createJobGraph(ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc) {
    JobGraph jobGraph = new JobGraph(config, appDesc);
    // Source streams contain both input and intermediate streams.
    Set<StreamSpec> sourceStreams = getStreamSpecs(appDesc.getInputStreamIds(), streamConfig);
    // Sink streams contain both output and intermediate streams.
    Set<StreamSpec> sinkStreams = getStreamSpecs(appDesc.getOutputStreamIds(), streamConfig);

    Set<StreamSpec> intermediateStreams = Sets.intersection(sourceStreams, sinkStreams);
    Set<StreamSpec> inputStreams = Sets.difference(sourceStreams, intermediateStreams);
    Set<StreamSpec> outputStreams = Sets.difference(sinkStreams, intermediateStreams);

    Set<TableDescriptor> tables = appDesc.getTableDescriptors();

    // Generate job.id and job.name configs from app.id and app.name if defined
    MapConfig generatedJobConfigs = JobPlanner.generateSingleJobConfig(config);
    String jobName = generatedJobConfigs.get(JobConfig.JOB_NAME());
    String jobId = generatedJobConfigs.get(JobConfig.JOB_ID(), "1");

    // For this phase, we have a single job node for the whole DAG
    JobNode node = jobGraph.getOrCreateJobNode(jobName, jobId);

    // Add input streams
    inputStreams.forEach(spec -> jobGraph.addInputStream(spec, node));

    // Add output streams
    outputStreams.forEach(spec -> jobGraph.addOutputStream(spec, node));

    // Add intermediate streams
    intermediateStreams.forEach(spec -> jobGraph.addIntermediateStream(spec, node, node));

    // Add tables
    for (TableDescriptor table : tables) {
        jobGraph.addTable(table, node);
        // Add side-input streams (if any)
        if (table instanceof LocalTableDescriptor) {
            LocalTableDescriptor localTable = (LocalTableDescriptor) table;
            Iterable<String> sideInputs = ListUtils.emptyIfNull(localTable.getSideInputs());
            for (String sideInput : sideInputs) {
                jobGraph.addSideInputStream(getStreamSpec(sideInput, streamConfig));
            }
        }
    }

    if (!LegacyTaskApplication.class.isAssignableFrom(appDesc.getAppClass())) {
        // skip the validation when input streamIds are empty. This is only possible for LegacyTaskApplication
        jobGraph.validate();
    }

    return jobGraph;
}

From source file:com.netflix.spinnaker.halyard.deploy.provider.v1.kubernetes.KubernetesProviderInterface.java

private void upsertSecret(AccountDeploymentDetails<KubernetesAccount> details, Set<String> files,
        String secretName, String namespace) {
    KubernetesClient client = getClient(details.getAccount());
    createNamespace(client, namespace);//from ww w  . jav a  2s . c  om

    if (client.secrets().inNamespace(namespace).withName(secretName).get() != null) {
        client.secrets().inNamespace(namespace).withName(secretName).delete();
    }

    Map<String, String> secretContents = new HashMap<>();

    files.forEach(s -> {
        try {
            File f = new File(s);
            String name = f.getName();
            String data = new String(
                    Base64.getEncoder().encode(IOUtils.toString(new FileInputStream(f)).getBytes()));
            secretContents.putIfAbsent(name, data);
        } catch (IOException e) {
            throw new HalException(
                    new ConfigProblemBuilder(Severity.ERROR, "Unable to read contents of \"" + s + "\": " + e)
                            .build());
        }
    });

    SecretBuilder secretBuilder = new SecretBuilder();
    secretBuilder = secretBuilder.withNewMetadata().withName(secretName).withNamespace(namespace).endMetadata()
            .withData(secretContents);

    log.info("Staging secret " + secretName + " in namespace " + namespace + " with contents " + files);

    client.secrets().inNamespace(namespace).create(secretBuilder.build());
}

From source file:com.hortonworks.streamline.streams.security.service.SecurityCatalogResource.java

@PUT
@Path("/roles/{parentRoleName}/children")
@Timed//w w  w . j  a va2  s. c o  m
public Response addOrUpdateChildRoles(@PathParam("parentRoleName") String parentRoleName,
        Set<String> childRoleNames, @Context SecurityContext securityContext) throws Exception {
    SecurityUtil.checkRole(authorizer, securityContext, ROLE_SECURITY_ADMIN);
    Long parentId = getIdFromRoleName(parentRoleName);
    Set<Long> currentChildIds = new HashSet<>();
    catalogService.getChildRoles(parentId).forEach(role -> currentChildIds.add(role.getId()));
    Set<Long> updatedChildIds = new HashSet<>();
    childRoleNames.forEach(childRoleName -> {
        if (childRoleName.equals(parentRoleName)) {
            throw new IllegalArgumentException("Child role(s) contain parent role");
        }
        updatedChildIds.add(getIdFromRoleName(childRoleName));
    });
    Set<Long> childIdsToAdd = Sets.difference(updatedChildIds, currentChildIds);
    Set<Long> childIdsToRemove = Sets.difference(currentChildIds, updatedChildIds);
    childIdsToRemove.forEach(childId -> catalogService.removeChildRole(parentId, childId));
    Set<RoleHierarchy> res = new HashSet<>();
    Sets.intersection(currentChildIds, updatedChildIds)
            .forEach(childId -> res.add(new RoleHierarchy(parentId, childId)));
    childIdsToAdd.forEach(childId -> res.add(catalogService.addChildRole(parentId, childId)));
    return WSUtils.respondEntities(res, OK);
}