Example usage for java.util Set size

List of usage examples for java.util Set size

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this set (its cardinality).

Usage

From source file:net.firejack.platform.core.utils.db.DBUtils.java

private static String populateInsertQuery(TablesMapping mapping) {
    Map<Column, Column> columnMapping = mapping.getColumnMapping();
    StringBuilder insertQuery = new StringBuilder("insert into ");
    insertQuery.append(mapping.getTargetTable().getName()).append('(');
    Set<Map.Entry<Column, Column>> columnEntries = columnMapping.entrySet();
    for (Map.Entry<Column, Column> entry : columnEntries) {
        Column targetColumn = entry.getValue();
        insertQuery.append(targetColumn.getName()).append(',');
    }//w w  w.  j  av  a  2 s  . c o  m
    if (!columnMapping.isEmpty()) {
        insertQuery.replace(insertQuery.length() - 1, insertQuery.length(), "");
    }
    insertQuery.append(") values (");
    for (int i = 0; i < columnEntries.size(); i++) {
        insertQuery.append(i == 0 ? '?' : ", ?");
    }
    insertQuery.append(')');
    return insertQuery.toString();
}

From source file:edu.stanford.muse.xword.Crossword.java

/** removes the terms that are bad as answers. Caps to MAX_ANSWER_WORDS */
public static List<String> removeBadCandidatesAndCap(List<String> terms, Set<String> tabooTokens) {
    log.info("removing bad candidates with " + tabooTokens.size() + " taboo words");

    // create list of candidates words 
    List<String> result = new ArrayList<String>();
    int count = 0;

    next_term: for (String term : terms) {
        // any token in term matches any taboo token, and the entire term is nuked
        Set<String> tokens = cleanAndTokenize(term);
        for (String token : tokens) {
            if (tabooTokens.contains(token))
                continue next_term;
            if (DictUtils.hasOnlyCommonDictWords(token)) // hasOnlyCommonDictWords is robust for case
                continue next_term;
            if (DictUtils.fullDictWords.contains(token))
                continue next_term;
            //   if (IndexUtils.topNames.contains(token))
            //      continue next_term;
        }/*from  w  ww  .  java2s.c om*/

        result.add(term);
        count++;
        if (count >= MAX_ANSWER_WORDS) {
            log.info("reached limit of " + MAX_ANSWER_WORDS + " dropping the rest");
            break;
        }
    }
    log.info("after removing taboo candidates, answer terms: " + result.size() + " original terms: "
            + terms.size());
    return result;
}

From source file:io.seldon.importer.articles.ItemAttributesImporter.java

public static Map<String, String> getAttributes(String url, String existingCategory) {
    ItemProcessResult itemProcessResult = new ItemProcessResult();
    itemProcessResult.client_item_id = url;
    itemProcessResult.extraction_status = "EXTRACTION_FAILED";

    logger.info("Trying to get attributes for " + url);
    Map<String, String> attributes = null;
    String title = "";
    String category = "";
    String subCategory = "";
    String img_url = "";
    String description = "";
    String tags = "";
    String leadtext = "";
    String link = "";
    String publishDate = "";
    String domain = "";
    try {//from   w  ww  .  j a  va2 s .c  o  m
        long now = System.currentTimeMillis();
        long timeSinceLastRequest = now - lastUrlFetchTime;
        if (timeSinceLastRequest < minFetchGapMsecs) {
            long timeToSleep = minFetchGapMsecs - timeSinceLastRequest;
            logger.info(
                    "Sleeping " + timeToSleep + "msecs as time since last fetch is " + timeSinceLastRequest);
            Thread.sleep(timeToSleep);
        }
        Document articleDoc = Jsoup.connect(url).userAgent("SeldonBot/1.0").timeout(httpGetTimeout).get();
        lastUrlFetchTime = System.currentTimeMillis();
        //get IMAGE URL
        if (StringUtils.isNotBlank(imageCssSelector)) {
            Element imageElement = articleDoc.select(imageCssSelector).first();
            if (imageElement != null && imageElement.attr("content") != null) {
                img_url = imageElement.attr("content");
            }
            if (imageElement != null && StringUtils.isBlank(img_url)) {
                img_url = imageElement.attr("src");
            }
            if (imageElement != null && StringUtils.isBlank(img_url)) {
                img_url = imageElement.attr("href");
            }

        }

        if (StringUtils.isBlank(img_url) && StringUtils.isNotBlank(defImageUrl)) {
            logger.info("Setting image to default: " + defImageUrl);
            img_url = defImageUrl;
        }
        img_url = StringUtils.strip(img_url);

        //get TITLE
        if (StringUtils.isNotBlank(titleCssSelector)) {
            Element titleElement = articleDoc.select(titleCssSelector).first();
            if ((titleElement != null) && (titleElement.attr("content") != null)) {
                title = titleElement.attr("content");
            }

            // if still blank get from text instead
            if (StringUtils.isBlank(title) && (titleElement != null)) {
                title = titleElement.text();
            }
        }

        //get LEAD TEXT
        if (StringUtils.isNotBlank(leadTextCssSelector)) {
            Element leadElement = articleDoc.select(leadTextCssSelector).first();
            if (leadElement != null && leadElement.attr("content") != null) {
                leadtext = leadElement.attr("content");
            }
        }

        //get publish date
        if (StringUtils.isNotBlank(publishDateCssSelector)) {
            //2013-01-21T10:40:55Z
            Element pubElement = articleDoc.select(publishDateCssSelector).first();
            if (pubElement != null && pubElement.attr("content") != null) {
                String pubtext = pubElement.attr("content");
                SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
                Date result = null;
                try {
                    result = df.parse(pubtext);
                } catch (ParseException e) {
                    logger.info("Failed to parse date withUTC format " + pubtext);
                }
                //try a simpler format
                df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
                try {
                    result = df.parse(pubtext);
                } catch (ParseException e) {
                    logger.info("Failed to parse date " + pubtext);
                }

                if (result != null)
                    publishDate = dateFormatter.format(result);
                else
                    logger.error("Failed to parse date " + pubtext);
            }
        }

        //get Link
        if (StringUtils.isNotBlank(linkCssSelector)) {
            Element linkElement = articleDoc.select(linkCssSelector).first();
            if (linkElement != null && linkElement.attr("content") != null) {
                link = linkElement.attr("content");
            }
        }

        //get CONTENT
        if (StringUtils.isNotBlank(textCssSelector)) {
            Element descriptionElement = articleDoc.select(textCssSelector).first();
            if (descriptionElement != null)
                description = Jsoup.parse(descriptionElement.html()).text();
        }

        //get TAGS
        Set<String> tagSet = AttributesImporterUtils.getTags(articleDoc, tagsCssSelector, title);

        if (tagSet.size() > 0)
            tags = CollectionTools.join(tagSet, ",");

        //get CATEGORY - client specific
        if (StringUtils.isNotBlank(categoryCssSelector)) {
            Element categoryElement = articleDoc.select(categoryCssSelector).first();
            if (categoryElement != null && categoryElement.attr("content") != null) {
                category = categoryElement.attr("content");
                if (StringUtils.isNotBlank(category))
                    category = category.toUpperCase();
            }
        } else if (StringUtils.isNotBlank(categoryClassPrefix)) {
            String className = "io.seldon.importer.articles.category." + categoryClassPrefix
                    + "CategoryExtractor";
            Class<?> clazz = Class.forName(className);
            Constructor<?> ctor = clazz.getConstructor();
            CategoryExtractor extractor = (CategoryExtractor) ctor.newInstance();
            category = extractor.getCategory(url, articleDoc);
        }

        //get Sub CATEGORY - client specific
        if (StringUtils.isNotBlank(subCategoryCssSelector)) {
            Element subCategoryElement = articleDoc.select(subCategoryCssSelector).first();
            if (subCategoryElement != null && subCategoryElement.attr("content") != null) {
                subCategory = subCategoryElement.attr("content");
                if (StringUtils.isNotBlank(subCategory))
                    subCategory = category.toUpperCase();
            }
        } else if (StringUtils.isNotBlank(subCategoryClassPrefix)) {
            String className = "io.seldon.importer.articles.category." + subCategoryClassPrefix
                    + "SubCategoryExtractor";
            Class<?> clazz = Class.forName(className);
            Constructor<?> ctor = clazz.getConstructor();
            CategoryExtractor extractor = (CategoryExtractor) ctor.newInstance();
            subCategory = extractor.getCategory(url, articleDoc);
        }

        // Get domain
        if (domainIsNeeded) {
            domain = getDomain(url);
        }

        if ((StringUtils.isNotBlank(title) && (imageNotNeeded || StringUtils.isNotBlank(img_url))
                && (categoryNotNeeded || StringUtils.isNotBlank(category))
                && (!domainIsNeeded || StringUtils.isNotBlank(domain)))) {
            attributes = new HashMap<String, String>();
            attributes.put(TITLE, title);
            if (StringUtils.isNotBlank(category))
                attributes.put(CATEGORY, category);
            if (StringUtils.isNotBlank(subCategory))
                attributes.put(SUBCATEGORY, subCategory);
            if (StringUtils.isNotBlank(link))
                attributes.put(LINK, link);
            if (StringUtils.isNotBlank(leadtext))
                attributes.put(LEAD_TEXT, leadtext);
            if (StringUtils.isNotBlank(img_url))
                attributes.put(IMG_URL, img_url);
            if (StringUtils.isNotBlank(tags))
                attributes.put(TAGS, tags);
            attributes.put(CONTENT_TYPE, VERIFIED_CONTENT_TYPE);
            if (StringUtils.isNotBlank(description))
                attributes.put(DESCRIPTION, description);
            if (StringUtils.isNotBlank(publishDate))
                attributes.put(PUBLISH_DATE, publishDate);
            if (StringUtils.isNotBlank(domain))
                attributes.put(DOMAIN, domain);
            System.out.println("Item: " + url + "; Category: " + category + " SubCategory: " + subCategory);
            itemProcessResult.extraction_status = "EXTRACTION_SUCCEEDED";
        } else {
            logger.warn("Failed to get needed attributes for article " + url);
            logger.warn("[title=" + title + ", img_url=" + img_url + ", category=" + category + ", domain="
                    + domain + "]");
        }

        { // check for failures for the log result
            if (StringUtils.isBlank(title)) {
                itemProcessResult.attrib_failure_list = itemProcessResult.attrib_failure_list
                        + ((StringUtils.isBlank(itemProcessResult.attrib_failure_list)) ? "" : ",") + "title";
            }
            if (!imageNotNeeded && StringUtils.isBlank(img_url)) {
                itemProcessResult.attrib_failure_list = itemProcessResult.attrib_failure_list
                        + ((StringUtils.isBlank(itemProcessResult.attrib_failure_list)) ? "" : ",") + "img_url";
            }
            if (!categoryNotNeeded && StringUtils.isBlank(category)) {
                itemProcessResult.attrib_failure_list = itemProcessResult.attrib_failure_list
                        + ((StringUtils.isBlank(itemProcessResult.attrib_failure_list)) ? "" : ",")
                        + "category";
            }
        }
    } catch (Exception e) {
        logger.error("Article: " + url + ". Attributes import FAILED", e);
        itemProcessResult.error = e.toString();
    }

    AttributesImporterUtils.logResult(logger, itemProcessResult);

    return attributes;
}

From source file:org.eclipse.virgo.ide.runtime.core.provisioning.RepositoryUtils.java

/**
 * Downloads the given <code>artifacts</code>.
 *//* w  w w. ja  va2s .  co m*/
public static void downloadArifacts(final Set<Artefact> artifacts, final IProject project, final Shell shell,
        boolean resolveDependencies) {

    if (resolveDependencies) {
        artifacts.addAll(resolveDependencies(artifacts, false));
    }

    final Set<IRuntime> runtimes = new HashSet<IRuntime>();

    ServerRuntimeUtils.execute(project, new ServerRuntimeUtils.ServerRuntimeCallback() {

        public boolean doWithRuntime(VirgoServerRuntime runtime) {
            runtimes.add(runtime.getRuntime());
            return true;
        }
    });

    if (runtimes.size() > 0) {

        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                RepositoryProvisioningJob job = new RepositoryProvisioningJob(runtimes, artifacts, true);
                job.run(monitor);
            }
        };

        try {
            IRunnableContext context = new ProgressMonitorDialog(shell);
            context.run(true, true, runnable);
        } catch (InvocationTargetException e1) {
        } catch (InterruptedException e2) {
        }
    }
}

From source file:edu.kit.dama.dataworkflow.util.DataWorkflowHelper.java

/**
 * Schedule the staging process(es) for the data needed by the provided
 * task. The process contains the following points:
 * <ul>/*from   w w  w  . j  a va  2  s  . c o m*/
 * <li>Creation of the task base path including data_in, data_out, working
 * and temp directories.</li>
 * <li>Obtaining selected data organization views of digital objects to
 * stage.</li>
 * <li>Schedule downloads for the content of each digital object.</li>
 * <li>Create symbolic links of the first-level content (all nodes directly
 * below 'root') of each view into 'data_in'. Existing files will be
 * skipped.</li>
 * </ul>
 * As soon as the staging for all digital objects is done, the symbolic
 * links in the data_in directory should point the valid data located inside
 * the different staging locations.
 *
 * @param pTask The task for which the staging should be scheduled.
 *
 * @return A properties object containing the object-transferId mapping.
 * This mapping should be stored in the DataWorkflowTask in order to be able
 * to check the data staging process.
 *
 * @throws StagingPreparationException if anything fails.
 */
public static Properties scheduleStaging(DataWorkflowTask pTask) throws StagingPreparationException {
    Properties objectDownloadMap = new Properties();
    IAuthorizationContext ctx = getTaskContext(pTask);
    File taskBasePath;
    try {
        taskBasePath = getStagingBasePath(pTask);
    } catch (IOException ex) {
        throw new StagingPreparationException("Staging preparation failed.", ex);
    }
    LOGGER.debug("Checking task base path {}", taskBasePath);
    if (taskBasePath.exists()) {
        LOGGER.debug("Task base path at " + taskBasePath + " already exists.");
    } else {
        LOGGER.debug("Task base path does not exist. Creating directory strucute {}", taskBasePath);
        if (!taskBasePath.mkdirs()) {
            throw new StagingPreparationException("Failed to create task base path at " + taskBasePath);
        }
        LOGGER.debug("Task base path structure successfully created.");
    }

    File inputDir = getTaskInputDirectory(taskBasePath);
    File outputDir = getTaskOutputDirectory(taskBasePath);
    File tempDir = getTaskTempDirectory(taskBasePath);
    File workingDir = getTaskWorkingDirectory(taskBasePath);
    LOGGER.debug("Creating directories:");
    LOGGER.debug(" - Input: {}", inputDir);
    LOGGER.debug(" - Output: {}", outputDir);
    LOGGER.debug(" - Working: {}", workingDir);
    LOGGER.debug(" - Temp: {}", tempDir);

    LOGGER.debug("Obtaining object-view list for DataWorkflow task {}", pTask.getId());
    Properties objectViewMap = null;

    try {
        objectViewMap = pTask.getObjectViewMapAsObject();
    } catch (IOException ex) {
        throw new StagingPreparationException(
                "Failed to deserialize object-view list from task " + pTask.getId());
    }

    try {
        TransferClientProperties props = new TransferClientProperties();
        String accessPointId = pTask.getExecutionEnvironment().getStagingAccessPointId();
        AbstractStagingAccessPoint accessPoint = StagingConfigurationManager.getSingleton()
                .getAccessPointById(accessPointId);
        LOGGER.debug("Adding staging acccess point id {} to TransferClientProperties.", accessPointId);
        props.setStagingAccessPointId(accessPointId);
        String mail = getContact(pTask).getEmail();
        LOGGER.debug("Adding contact mail {} to TransferClientProperties.", mail);
        props.setReceiverMail(mail);
        Set<Entry<Object, Object>> entries = objectViewMap.entrySet();

        LOGGER.debug("Scheduling download for {} objects in object-view map", entries.size());
        for (Entry<Object, Object> entry : entries) {
            String objectId = (String) entry.getKey();
            String viewId = (String) entry.getValue();
            DigitalObjectId doid = new DigitalObjectId(objectId);
            IFileTree tree = DataOrganizationServiceLocal.getSingleton().loadFileTree(doid, viewId, ctx);
            LOGGER.debug("Scheduling download for object {} and view {}", objectId, viewId);
            DownloadInformation downloadInfo = DownloadInformationServiceLocal.getSingleton()
                    .scheduleDownload(doid, tree, props, ctx);
            LOGGER.debug("Putting transfer id {} for object {} to object-transfer list.", downloadInfo.getId(),
                    objectId);
            objectDownloadMap.put(doid.getStringRepresentation(), Long.toString(downloadInfo.getId()));

            List<? extends IDataOrganizationNode> firstLevelNodes = tree.getRootNode().getChildren();
            LOGGER.debug("Creating links for {} first level data organization nodes", firstLevelNodes.size());
            for (IDataOrganizationNode node : firstLevelNodes) {
                File linkedFile = new File(inputDir + File.separator + node.getName());
                LOGGER.debug("Creating link for file {}", linkedFile);
                if (linkedFile.exists()) {
                    LOGGER.error("File link " + linkedFile
                            + " already exists. Skipping link creation but processing might fail.");
                } else {
                    LOGGER.debug("Obtaining data path from download information");
                    File dataPath = accessPoint.getLocalPathForUrl(downloadInfo.getDataFolderUrl(), ctx);
                    LOGGER.debug("Obtained data path is '{}'. Creating symbolic link to input dir at '{}'",
                            dataPath, linkedFile);
                    SystemUtils.createSymbolicLink(new File(dataPath, node.getName()), linkedFile);
                    LOGGER.debug("Link successfully created.");
                }
            }
            LOGGER.debug("Staging of object {} for task {} successfully scheduled.", objectId, pTask.getId());
        }
        LOGGER.debug("Scheduling of all objects for task {} successfully finished.", pTask.getId());
    } catch (IOException | EntityNotFoundException | TransferPreparationException ex) {
        //Failed to create link/view not found/transfer preparation has failed
        throw new StagingPreparationException(
                "Failed to prepare task directory structure for task " + pTask.getId(), ex);
    }
    return objectDownloadMap;
}

From source file:modula.parser.io.ModelUpdater.java

/**
 * transition/*from   w  w w.  java2s . c o m*/
 */
private static void updateTransition(final SimpleTransition transition,
        final Map<String, TransitionTarget> targets) throws ModelException {
    String next = transition.getNext();
    if (next == null) { // stay transition
        return;
    }
    Set<TransitionTarget> tts = transition.getTargets();
    if (tts.isEmpty()) {
        // 'next' is a space separated list of transition target IDs
        StringTokenizer ids = new StringTokenizer(next);
        while (ids.hasMoreTokens()) {
            String id = ids.nextToken();
            TransitionTarget tt = targets.get(id);
            if (tt == null) {
                logAndThrowModelError(ERR_TARGET_NOT_FOUND, new Object[] { id });
            }
            tts.add(tt);
        }
        if (tts.size() > 1) {
            boolean legal = verifyTransitionTargets(tts);
            if (!legal) {
                logAndThrowModelError(ERR_ILLEGAL_TARGETS, new Object[] { next });
            }
        }
    }
}

From source file:Os.java

private static OsFamily[] determineAllFamilies() {
    // Determine all families the current OS belongs to
    Set allFamilies = new HashSet();
    if (OS_FAMILY != null) {
        List queue = new ArrayList();
        queue.add(OS_FAMILY);// w  w w.  j av a 2 s.  c  o m
        while (queue.size() > 0) {
            final OsFamily family = (OsFamily) queue.remove(0);
            allFamilies.add(family);
            final OsFamily[] families = family.getFamilies();
            for (int i = 0; i < families.length; i++) {
                OsFamily parent = families[i];
                queue.add(parent);
            }
        }
    }
    return (OsFamily[]) allFamilies.toArray(new OsFamily[allFamilies.size()]);
}

From source file:io.cloudslang.lang.tools.build.SlangBuildMain.java

private static void printCoveredExecutables(Set<String> coveredExecutables,
        final LoggingService loggingService) {
    loggingService.logEvent(Level.INFO, "");
    loggingService.logEvent(Level.INFO, "------------------------------------------------------------");
    loggingService.logEvent(Level.INFO, "Following " + coveredExecutables.size() + " executables have tests:");
    for (String executable : coveredExecutables) {
        loggingService.logEvent(Level.INFO, "- " + executable);
    }/*  www.ja  v  a  2  s . co  m*/
}

From source file:ai.susi.mind.SusiSkill.java

/**
 * if no keys are given, we compute them from the given phrases
 * @param phrases//from   w  w  w.j  a va 2  s.c om
 * @return
 */
private static JSONArray computeKeysFromPhrases(List<SusiPhrase> phrases) {
    Set<String> t = new LinkedHashSet<>();

    // create a list of token sets from the phrases
    List<Set<String>> ptl = new ArrayList<>();
    final AtomicBoolean needsCatchall = new AtomicBoolean(false);
    phrases.forEach(phrase -> {
        Set<String> s = new HashSet<>();
        for (String token : SPACE_PATTERN.split(phrase.getPattern().toString())) {
            String m = SusiPhrase.extractMeat(token.toLowerCase());
            if (m.length() > 1)
                s.add(m);
        }
        // if there is no meat inside, it will not be possible to access the skill without the catchall skill, so remember that
        if (s.size() == 0)
            needsCatchall.set(true);

        ptl.add(s);
    });

    // this is a kind of emergency case where we need a catchall skill because otherwise we cannot access one of the phrases
    JSONArray a = new JSONArray();
    if (needsCatchall.get())
        return a.put(CATCHALL_KEY);

    // collect all token
    ptl.forEach(set -> set.forEach(token -> t.add(token)));

    // if no tokens are available, return the catchall key
    if (t.size() == 0)
        return a.put(CATCHALL_KEY);

    // make a copy to make it possible to use the original key set again
    Set<String> tc = new LinkedHashSet<>();
    t.forEach(c -> tc.add(c));

    // remove all token that do not appear in all phrases
    ptl.forEach(set -> {
        Iterator<String> i = t.iterator();
        while (i.hasNext())
            if (!set.contains(i.next()))
                i.remove();
    });

    // if no token is left, use the original tc set and add all keys
    if (t.size() == 0) {
        tc.forEach(c -> a.put(c));
        return a;
    }

    // use only the first token, because that appears in all the phrases
    return new JSONArray().put(t.iterator().next());
}