Example usage for java.util.stream Collectors toSet

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

Introduction

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

Prototype

public static <T> Collector<T, ?, Set<T>> toSet() 

Source Link

Document

Returns a Collector that accumulates the input elements into a new Set .

Usage

From source file:com.netflix.genie.web.data.services.jpa.JpaServiceUtils.java

/**
 * Convert a command entity to a DTO for external exposure.
 *
 * @param commandEntity The entity to convert
 * @return The immutable DTO representation of the entity data
 *//*w w  w .  j a va  2s. c  om*/
static Command toCommandDto(final CommandEntity commandEntity) {
    final Command.Builder builder = new Command.Builder(commandEntity.getName(), commandEntity.getUser(),
            commandEntity.getVersion(), commandEntity.getStatus(),
            StringUtils.join(commandEntity.getExecutable(), ' '), commandEntity.getCheckDelay())
                    .withId(commandEntity.getUniqueId()).withCreated(commandEntity.getCreated())
                    .withUpdated(commandEntity.getUpdated())
                    .withTags(
                            commandEntity.getTags().stream().map(TagEntity::getTag).collect(Collectors.toSet()))
                    .withConfigs(commandEntity.getConfigs().stream().map(FileEntity::getFile)
                            .collect(Collectors.toSet()))
                    .withDependencies(commandEntity.getDependencies().stream().map(FileEntity::getFile)
                            .collect(Collectors.toSet()));

    commandEntity.getMemory().ifPresent(builder::withMemory);
    commandEntity.getDescription().ifPresent(builder::withDescription);
    commandEntity.getSetupFile().ifPresent(setupFileEntity -> builder.withSetupFile(setupFileEntity.getFile()));
    setDtoMetadata(builder, commandEntity);

    return builder.build();
}

From source file:uk.ac.kcl.it.DeIdentificationPKPartitionWithoutScheduling.java

@Test
public void deidentificationPerformanceTest() {
    dbmsTestUtils.createBasicInputTable();
    dbmsTestUtils.createBasicOutputTable();
    dbmsTestUtils.createDeIdInputTable();
    List<Mutant> mutants = testUtils.insertTestDataForDeidentification(env.getProperty("tblIdentifiers"),
            env.getProperty("tblInputDocs"), mutatortype);
    int totalTruePositives = 0;
    int totalFalsePositives = 0;
    int totalFalseNegatives = 0;

    for (Mutant mutant : mutants) {
        Set<Pattern> mutatedPatterns = new HashSet<>();
        mutant.setDeidentifiedString(elasticGazetteerService.deIdentifyString(mutant.getFinalText(),
                String.valueOf(mutant.getDocumentid())));
        Set<String> set = new HashSet<>(mutant.getOutputTokens());
        mutatedPatterns.addAll(//w ww .j  a  v  a  2  s  .c  om
                set.stream().map(string -> Pattern.compile(Pattern.quote(string), Pattern.CASE_INSENSITIVE))
                        .collect(Collectors.toSet()));
        List<MatchResult> results = new ArrayList<>();
        for (Pattern pattern : mutatedPatterns) {
            Matcher matcher = pattern.matcher(mutant.getFinalText());
            while (matcher.find()) {
                results.add(matcher.toMatchResult());
            }
        }

        int truePositives = getTruePositiveTokenCount(mutant);
        int falsePositives = getFalsePositiveTokenCount(mutant);
        int falseNegatives = getFalseNegativeTokenCount(mutant);

        System.out.println("Doc ID " + mutant.getDocumentid() + " has " + falseNegatives
                + " unmasked identifiers from a total of " + (falseNegatives + truePositives));
        System.out.println("Doc ID " + mutant.getDocumentid() + " has " + falsePositives
                + " inaccurately masked tokens from a total of " + (falsePositives + truePositives));
        System.out.println("TP: " + truePositives + " FP: " + falsePositives + " FN: " + falseNegatives);
        System.out.println("Doc ID precision " + calcPrecision(falsePositives, truePositives));
        System.out.println("Doc ID recall " + calcRecall(falseNegatives, truePositives));
        System.out.println(mutant.getDeidentifiedString());
        System.out.println(mutant.getFinalText());
        System.out.println(mutant.getInputTokens());
        System.out.println(mutant.getOutputTokens());
        System.out.println();
        if (env.getProperty("elasticgazetteerTestOutput") != null) {
            try {
                try (BufferedWriter bw = new BufferedWriter(
                        new FileWriter(new File(env.getProperty("elasticgazetteerTestOutput") + File.separator
                                + mutant.getDocumentid())))) {
                    bw.write("Doc ID " + mutant.getDocumentid() + " has " + falseNegatives
                            + " unmasked identifiers from a total of " + (falseNegatives + truePositives));
                    bw.newLine();
                    bw.write("Doc ID " + mutant.getDocumentid() + " has " + falsePositives
                            + " inaccurately masked tokens from a total of "
                            + (falsePositives + truePositives));
                    bw.newLine();
                    bw.write("TP: " + truePositives + " FP: " + falsePositives + " FN: " + falseNegatives);
                    bw.newLine();
                    bw.write("Doc ID precision " + calcPrecision(falsePositives, truePositives));
                    bw.newLine();
                    bw.write("Doc ID recall " + calcRecall(falseNegatives, truePositives));
                    bw.newLine();
                    bw.write(mutant.getDeidentifiedString());
                    bw.newLine();
                    bw.write(mutant.getFinalText());
                    bw.newLine();
                    bw.write(mutant.getInputTokens().toString());
                    bw.newLine();
                    bw.write(mutant.getOutputTokens().toString());

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        totalTruePositives += truePositives;
        totalFalsePositives += falsePositives;
        totalFalseNegatives += falseNegatives;
    }
    System.out.println();
    System.out.println();
    System.out.println("THIS RUN TP: " + totalTruePositives + " FP: " + totalFalsePositives + " FN: "
            + totalFalseNegatives);
    System.out.println("Doc ID precision " + calcPrecision(totalFalsePositives, totalTruePositives));
    System.out.println("Doc ID recall " + calcRecall(totalFalseNegatives, totalTruePositives));
    if (env.getProperty("elasticgazetteerTestOutput") != null) {
        try {
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                    new File(env.getProperty("elasticgazetteerTestOutput") + File.separator + "summary")))) {
                bw.write("THIS RUN TP: " + totalTruePositives + " FP: " + totalFalsePositives + " FN: "
                        + totalFalseNegatives);
                bw.newLine();
                bw.write("Doc ID precision " + calcPrecision(totalFalsePositives, totalTruePositives));
                bw.newLine();
                bw.write("Doc ID recall " + calcRecall(totalFalseNegatives, totalTruePositives));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:at.ac.tuwien.qse.sepm.gui.controller.impl.PhotoInspectorImpl.java

@FXML
private void initialize() {
    addToSlideshowButton.setOnAction(this::handleAddToSlideshow);
    slideshowsCombobox.setConverter(new SlideshowStringConverter());
    slideshowsCombobox.setItems(slideshowView.getSlideshows());

    ratingPicker.setRatingChangeHandler(r -> getEntities().forEach(this::saveRating));

    mapScene.setClickCallback((position) -> {
        if (!changeCoordinatesButton.isSelected()) {
            return;
        }// ww w .  j  av  a  2  s. com

        changeCoordinatesButton.setSelected(false);

        for (Photo photo : getEntities()) {
            photo.getData().setLatitude(position.getLatitude());
            photo.getData().setLongitude(position.getLongitude());
            savePhoto(photo);
        }

        updateMap(getEntities());
    });

    tagPicker.setOnUpdate(() -> getEntities().forEach(this::saveTags));

    tagField.setOnAction(() -> {
        String value = tagField.getText();
        if (value == null || value.isEmpty())
            return;
        for (Photo photo : getEntities()) {
            tagField.setText(null);
            photo.getData().getTags().add(new Tag(null, value));
            savePhoto(photo);
        }
        onUpdate();
    });

    photographerField.setOnAction(() -> {
        String value = photographerField.getText();
        if (value == null || value.isEmpty())
            return;
        for (Photo photo : getEntities()) {
            photographerField.setText(null);
            photo.getData().setPhotographer(new Photographer(null, value));
            savePhoto(photo);
        }
        onUpdate();
    });

    // TODO: reload these when tags / photographers are added
    try {
        LOGGER.debug("loading photographer suggestions");
        photographerField.getSuggestions().setAll(
                photographerService.readAll().stream().map(Photographer::getName).collect(Collectors.toSet()));
    } catch (ServiceException ex) {
        LOGGER.warn("failed loading photographer suggestions");
        LOGGER.error("", ex);
    }
    try {
        LOGGER.debug("loading tag suggestions");
        tagField.getSuggestions()
                .setAll(tagService.getAllTags().stream().map(Tag::getName).collect(Collectors.toSet()));
    } catch (ServiceException ex) {
        LOGGER.warn("failed loading tag suggestions");
        LOGGER.error("", ex);
    }
}

From source file:me.rkfg.xmpp.bot.plugins.FaggotOfTheDayPlugin.java

private void calculateFaggot() {
    occupants.removeIf(occupant -> occupant.getNick().equals(getBotNick()));

    if (occupants.isEmpty()) {
        log.info("No contenders for Faggot of the Day today.");
        if (faggot != null) {
            log.info("{} remains Faggot of the Day!", faggot.getNick());
        }/* w w  w .  ja va  2  s .c  om*/
        return;
    }

    final Set<String> uniqueJids = occupants.stream().map(Occupant::getJid).map(XmppStringUtils::parseBareJid)
            .collect(Collectors.toSet());
    log.info("Contenders for todays Faggot of the Day title: {}", uniqueJids);

    final int i = random.nextInt(uniqueJids.size());
    final String faggotJid = uniqueJids.stream().skip(i).findFirst().get();
    faggot = occupants.stream()
            .filter(occupant -> faggotJid.equals(XmppStringUtils.parseBareJid(occupant.getJid()))).findFirst()
            .get();
    log.info("{} becomes Faggot of the Day!", faggot.getNick());

    occupants.clear();
}

From source file:com.hortonworks.registries.schemaregistry.avro.SchemaBranchLifeCycleTest.java

@Test
public void getAllBranches() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException,
        SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);

    SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata,
            "/device.avsc");
    SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata,
            masterSchemaIdVersion1.getSchemaVersionId());
    SchemaIdVersion masterSchemaIdVersion2 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata,
            "/device-incompat.avsc");
    SchemaBranch schemaBranch2 = addSchemaBranch("BRANCH2", schemaMetadata,
            masterSchemaIdVersion2.getSchemaVersionId());

    Set<String> actualSchemaBranches = schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).stream()
            .map(branch -> branch.getName()).collect(Collectors.toSet());
    Set<String> expectedSchemaBranches = new HashSet<>(
            Lists.newArrayList("MASTER", schemaBranch1.getName(), schemaBranch2.getName()));

    Assert.assertTrue(SetUtils.isEqualSet(actualSchemaBranches, expectedSchemaBranches));
}

From source file:com.netflix.spinnaker.fiat.controllers.AuthorizeController.java

@RequestMapping(value = "/{userId:.+}/serviceAccounts", method = RequestMethod.GET)
public Set<ServiceAccount.View> getServiceAccounts(@PathVariable String userId) {
    return permissionsRepository.get(ControllerSupport.convert(userId)).orElseThrow(NotFoundException::new)
            .getView().getServiceAccounts().stream().collect(Collectors.toSet());
}

From source file:org.openlmis.fulfillment.web.util.BasicOrderDtoBuilder.java

private Map<UUID, ProgramDto> getPrograms(List<Order> orders) {
    Set<UUID> programIds = orders.stream().map(Order::getProgramId).collect(Collectors.toSet());
    return programReferenceDataService.findByIds(programIds).stream()
            .collect(Collectors.toMap(BaseDto::getId, Function.identity()));
}

From source file:jp.classmethod.aws.brian.web.TriggerController.java

/**
 * Delete specified triggerGroup (belonging triggers).
 * /*from   w w w .  j a  va  2  s .c  om*/
 * @param triggerGroupName trigger group name
 * @return wherther the trigger is removed
 * @throws SchedulerException
 */
@ResponseBody
@RequestMapping(value = "/triggers/{triggerGroupName}/", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTriggerGroup(@PathVariable("triggerGroupName") String triggerGroupName)
        throws SchedulerException {
    logger.info("deleteTriggerGroup {}", triggerGroupName);

    List<String> triggers = listTriggers(triggerGroupName);
    Set<String> failed = triggers.stream().filter(triggerName -> {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
        try {
            return scheduler.unscheduleJob(triggerKey) == false;
        } catch (SchedulerException e) {
            logger.error("unexpected", e);
        }
        return false;
    }).collect(Collectors.toSet());

    if (failed.isEmpty()) {
        return ResponseEntity.ok(triggers);
    }
    Map<String, Object> map = new HashMap<>();
    map.put("failed", failed);
    map.put("deleted", triggers.stream().filter(t -> failed.contains(t) == false).collect(Collectors.toList()));
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(new BrianResponse<>(false, "failed to unschedule", map));
}

From source file:eu.itesla_project.online.tools.OnlineWorkflowTool.java

@Override
public void run(CommandLine line) throws Exception {

    OnlineWorkflowStartParameters startconfig = OnlineWorkflowStartParameters.loadDefault();

    String host = line.getOptionValue(OnlineWorkflowCommand.HOST);
    String port = line.getOptionValue(OnlineWorkflowCommand.PORT);
    String threads = line.getOptionValue(OnlineWorkflowCommand.THREADS);
    if (host != null)
        startconfig.setJmxHost(host);/*  ww  w.ja v  a 2 s . c o m*/
    if (port != null)
        startconfig.setJmxPort(Integer.valueOf(port));
    if (threads != null)
        startconfig.setThreads(Integer.valueOf(threads));

    Set<DateTime> baseCasesSet = null;

    OnlineWorkflowParameters params = OnlineWorkflowParameters.loadDefault();
    boolean atLeastOneBaseCaseLineParam = line.hasOption(OnlineWorkflowCommand.CASE_TYPE)
            || line.hasOption(OnlineWorkflowCommand.COUNTRIES)
            || line.hasOption(OnlineWorkflowCommand.BASE_CASE)
            || line.hasOption(OnlineWorkflowCommand.BASECASES_INTERVAL);
    boolean allNeededBaseCaseLineParams = line.hasOption(OnlineWorkflowCommand.CASE_TYPE)
            && line.hasOption(OnlineWorkflowCommand.COUNTRIES)
            && (line.hasOption(OnlineWorkflowCommand.BASE_CASE)
                    || line.hasOption(OnlineWorkflowCommand.BASECASES_INTERVAL));

    if (line.hasOption(OnlineWorkflowCommand.CASE_FILE)) {
        if (atLeastOneBaseCaseLineParam) {
            showHelp("parameter " + OnlineWorkflowCommand.CASE_FILE
                    + " cannot be used together with parameters: " + OnlineWorkflowCommand.CASE_TYPE + ", "
                    + OnlineWorkflowCommand.COUNTRIES + ", " + OnlineWorkflowCommand.BASE_CASE + ", "
                    + OnlineWorkflowCommand.BASECASES_INTERVAL);
            return;
        }
        params.setCaseFile(line.getOptionValue(OnlineWorkflowCommand.CASE_FILE));
    } else {
        if (params.getCaseFile() != null) {
            if (atLeastOneBaseCaseLineParam) {
                if (!allNeededBaseCaseLineParams) {
                    showHelp("to override default parameter " + OnlineWorkflowCommand.CASE_FILE
                            + ", all these parameters must be specified: " + OnlineWorkflowCommand.CASE_TYPE
                            + ", " + OnlineWorkflowCommand.COUNTRIES + ", " + OnlineWorkflowCommand.BASE_CASE
                            + " or " + OnlineWorkflowCommand.BASECASES_INTERVAL);
                    return;
                }
                params.setCaseFile(null);
            }
        }
        if (line.hasOption(OnlineWorkflowCommand.CASE_TYPE))
            params.setCaseType(CaseType.valueOf(line.getOptionValue(OnlineWorkflowCommand.CASE_TYPE)));
        if (line.hasOption(OnlineWorkflowCommand.COUNTRIES)) {
            params.setCountries(Arrays.stream(line.getOptionValue(OnlineWorkflowCommand.COUNTRIES).split(","))
                    .map(Country::valueOf).collect(Collectors.toSet()));
        }
        if (line.hasOption(OnlineWorkflowCommand.BASECASES_INTERVAL)) {
            Interval basecasesInterval = Interval
                    .parse(line.getOptionValue(OnlineWorkflowCommand.BASECASES_INTERVAL));
            OnlineConfig oConfig = OnlineConfig.load();
            CaseRepository caseRepo = oConfig.getCaseRepositoryFactoryClass().newInstance()
                    .create(new LocalComputationManager());
            baseCasesSet = caseRepo.dataAvailable(params.getCaseType(), params.getCountries(),
                    basecasesInterval);
            System.out.println("Base cases available for interval " + basecasesInterval.toString());
            baseCasesSet.forEach(x -> {
                System.out.println(" " + x);
            });
        }
        if (baseCasesSet == null) {
            baseCasesSet = new HashSet<>();
            String base = line.getOptionValue(OnlineWorkflowCommand.BASE_CASE);
            if (base != null) {
                baseCasesSet.add(DateTime.parse(base));
            } else {
                baseCasesSet.add(params.getBaseCaseDate());
            }
        }
    }

    String histo = line.getOptionValue(OnlineWorkflowCommand.HISTODB_INTERVAL);
    if (histo != null)
        params.setHistoInterval(Interval.parse(histo));

    String states = line.getOptionValue(OnlineWorkflowCommand.STATES);
    if (states != null)
        params.setStates(Integer.parseInt(states));

    String timeHorizon = line.getOptionValue(OnlineWorkflowCommand.TIME_HORIZON);
    if (timeHorizon != null)
        params.setTimeHorizon(TimeHorizon.fromName(timeHorizon));

    String workflowid = line.getOptionValue(OnlineWorkflowCommand.WORKFLOW_ID);
    if (workflowid != null)
        params.setOfflineWorkflowId(workflowid);

    String feAnalysisId = line.getOptionValue(OnlineWorkflowCommand.FEANALYSIS_ID);
    if (feAnalysisId != null)
        params.setFeAnalysisId(feAnalysisId);

    String rulesPurity = line.getOptionValue(OnlineWorkflowCommand.RULES_PURITY);
    if (rulesPurity != null)
        params.setRulesPurityThreshold(Double.parseDouble(rulesPurity));

    if (line.hasOption(OnlineWorkflowCommand.STORE_STATES))
        params.setStoreStates(true);

    if (line.hasOption(OnlineWorkflowCommand.ANALYSE_BASECASE))
        params.setAnalyseBasecase(true);

    if (line.hasOption(OnlineWorkflowCommand.VALIDATION)) {
        params.setValidation(true);
        params.setStoreStates(true); // if validation then store states
        params.setAnalyseBasecase(true); // if validation then analyze base case
    }

    Set<SecurityIndexType> securityIndexes = null;
    if (line.hasOption(OnlineWorkflowCommand.SECURITY_INDEXES)) {
        if (!"ALL".equals(line.getOptionValue(OnlineWorkflowCommand.SECURITY_INDEXES)))
            securityIndexes = Arrays
                    .stream(line.getOptionValue(OnlineWorkflowCommand.SECURITY_INDEXES).split(","))
                    .map(SecurityIndexType::valueOf).collect(Collectors.toSet());
        params.setSecurityIndexes(securityIndexes);
    }

    if (line.hasOption(OnlineWorkflowCommand.MERGE_OPTIMIZED))
        params.setMergeOptimized(true);

    String limitReduction = line.getOptionValue(OnlineWorkflowCommand.LIMIT_REDUCTION);
    if (limitReduction != null)
        params.setLimitReduction(Float.parseFloat(limitReduction));

    if (line.hasOption(OnlineWorkflowCommand.HANDLE_VIOLATION_IN_N)) {
        params.setHandleViolationsInN(true);
        params.setAnalyseBasecase(true); // if I need to handle violations in N, I need to analyze base case
    }

    String constraintMargin = line.getOptionValue(OnlineWorkflowCommand.CONSTRAINT_MARGIN);
    if (constraintMargin != null)
        params.setConstraintMargin(Float.parseFloat(constraintMargin));

    String urlString = "service:jmx:rmi:///jndi/rmi://" + startconfig.getJmxHost() + ":"
            + startconfig.getJmxPort() + "/jmxrmi";

    JMXServiceURL serviceURL = new JMXServiceURL(urlString);
    Map<String, String> jmxEnv = new HashMap<>();
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, jmxEnv);
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();

    ObjectName name = new ObjectName(LocalOnlineApplicationMBean.BEAN_NAME);
    LocalOnlineApplicationMBean application = MBeanServerInvocationHandler.newProxyInstance(mbsc, name,
            LocalOnlineApplicationMBean.class, false);

    if (line.hasOption(OnlineWorkflowCommand.START_CMD)) {
        if (params.getCaseFile() != null) {
            System.out.println("starting Online Workflow, caseFile " + params.getCaseFile());
            String workflowId = application.startWorkflow(startconfig, params);
            System.out.println("workflowId=" + workflowId);

        } else {
            for (DateTime basecase : baseCasesSet) {
                params.setBaseCaseDate(basecase);
                System.out.println("starting Online Workflow, basecase " + basecase.toString());
                String workflowId = application.startWorkflow(startconfig, params);
                System.out.println("workflowId=" + workflowId);
            }
        }
    } else if (line.hasOption(OnlineWorkflowCommand.SHUTDOWN_CMD)) {
        application.shutdown();
    } else {
        showHelp("");
    }

}

From source file:com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions.java

@Override
public boolean enableOnly(Principal principal, AllowedActions actions) {
    return enableOnly(principal, actions.getAvailableActions().stream().flatMap(avail -> avail.stream())
            .collect(Collectors.toSet()));
}