Example usage for java.util TreeSet size

List of usage examples for java.util TreeSet size

Introduction

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

Prototype

public int size() 

Source Link

Document

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

Usage

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

/**
 * Returns a list of terms in the specified field along with the corresponding count of documents in the set that match that constraint.
 * This method uses the FilterCache to get the intersection count between <code>docs</code> and the DocSet for each term in the filter.
 * //ww  w  . j  a  v a 2 s  . co  m
 * @see FacetParams#FACET_LIMIT
 * @see FacetParams#FACET_ZEROS
 * @see FacetParams#FACET_MISSING
 */
public NamedList<Object> getFacetTermEnumCounts(IndexSearcher searcher, OpenBitSet docs, String field,
        String fieldName, int offset, int limit, int mincount, boolean missing, String sort, String prefix,
        String locale, ExtendedPropertyDefinition epd) throws IOException {

    /*
     * :TODO: potential optimization... cache the Terms with the highest docFreq and try them first don't enum if we get our max from
     * them
     */

    // Minimum term docFreq in order to use the filterCache for that term.
    int minDfFilterCache = params.getFieldInt(field, FacetParams.FACET_ENUM_CACHE_MINDF, 0);

    IndexReader r = searcher.getIndexReader();
    FieldType ft = getType(epd);

    final int maxsize = limit >= 0 ? offset + limit : Integer.MAX_VALUE - 1;
    final TreeSet<SimpleFacets.CountPair<String, Integer>> queue = (sort.equals("count") || sort.equals("true"))
            ? new TreeSet<SimpleFacets.CountPair<String, Integer>>()
            : null;
    final NamedList<Object> res = new NamedList<Object>();

    int min = mincount - 1; // the smallest value in the top 'N' values
    int off = offset;
    int lim = limit >= 0 ? limit : Integer.MAX_VALUE;

    String startTerm = prefix == null ? "" : ft.toInternal(prefix);
    TermEnum te = r.terms(new Term(fieldName, startTerm));
    TermDocs td = r.termDocs();
    SolrIndexSearcher.TermDocsState tdState = new SolrIndexSearcher.TermDocsState();
    tdState.tenum = te;
    tdState.tdocs = td;

    if (docs.size() >= mincount) {
        do {
            Term t = te.term();

            if (null == t || !t.field().equals(fieldName))
                break;

            if (prefix != null && !t.text().startsWith(prefix))
                break;

            int df = te.docFreq();

            // If we are sorting, we can use df>min (rather than >=) since we
            // are going in index order. For certain term distributions this can
            // make a large difference (for example, many terms with df=1).
            if (df > 0 && df > min) {
                int c;

                if (df >= minDfFilterCache) {
                    // use the filter cache
                    // TODO: use the new method ???                        
                    //                        docs.intersectionSize( searcher.getPositiveDocSet(new TermQuery(t), tdState) );
                    c = (int) OpenBitSet.intersectionCount(getDocIdSet(new TermQuery(t), locale), docs);
                } else {
                    // iterate over TermDocs to calculate the intersection
                    td.seek(te);
                    c = 0;
                    while (td.next()) {
                        int doc = td.doc();
                        if (locale != null) {
                            doc = getMainDocIdForTranslations(
                                    searcher.getIndexReader().document(doc, PARENT_AND_TRANSLATION_FIELDS),
                                    locale);
                        }

                        if (docs.fastGet(doc)) {
                            c++;
                        }
                    }
                }

                if (sort.equals("count") || sort.equals("true")) {
                    if (c > min) {
                        queue.add(new SimpleFacets.CountPair<String, Integer>(t.text(), c));
                        if (queue.size() >= maxsize) {
                            break;
                        }
                    }
                } else {
                    if (c >= mincount && --off < 0) {
                        if (--lim < 0)
                            break;
                        res.add(ft.indexedToReadable(t.text()), c);
                    }
                }
            }
        } while (te.next());
    }

    if (sort.equals("count") || sort.equals("true")) {
        for (SimpleFacets.CountPair<String, Integer> p : queue) {
            if (--off >= 0)
                continue;
            if (--lim < 0)
                break;
            res.add(ft.indexedToReadable(p.key), p.val);
        }
    }

    if (missing) {
        res.add(null, getFieldMissingCount(searcher, docs, fieldName, locale));
    }

    te.close();
    td.close();

    return res;
}

From source file:org.lockss.subscription.SubscriptionManager.java

/**
 * Populates the list of weighted repositories, to be used in a round-robin
 * fashion for the subsequent AU configurations.
 * /*from   w  ww.jav a2s  .  c  o m*/
 * @param repositoryMap
 *          A Map<String, PlatformUtil.DF> with the map of all distinct
 *          repositories available.
 * @return a List<String> with the list of weighted repositories.
 */
List<String> populateRepositories(Map<String, PlatformUtil.DF> repositoryMap) {
    final String DEBUG_HEADER = "populateRepositories(): ";
    if (log.isDebug2())
        log.debug2(DEBUG_HEADER + "repositoryMap.size() = " + repositoryMap.size());

    // Initialize the list of available repositories.
    List<String> repos = new ArrayList<String>();

    // Handle an empty repository map.
    if (repositoryMap.size() < 1) {
        if (log.isDebug2())
            log.debug2(DEBUG_HEADER + "repos = " + repos);
        return repos;
    }

    // Get the available repositories sorted by their available space.
    TreeSet<Entry<String, PlatformUtil.DF>> sortedRepos = new TreeSet<Entry<String, PlatformUtil.DF>>(
            DF_BY_AVAIL_COMPARATOR);
    sortedRepos.addAll(repositoryMap.entrySet());
    if (log.isDebug3())
        log.debug3(DEBUG_HEADER + "sortedRepos.size() = " + sortedRepos.size());

    // Handle the case of a single repository.
    if (sortedRepos.size() == 1) {
        repos.add(sortedRepos.first().getKey());
        if (log.isDebug3())
            log.debug3(DEBUG_HEADER + "Added " + sortedRepos.first().getKey());

        if (log.isDebug2())
            log.debug2(DEBUG_HEADER + "repos = " + repos);
        return repos;
    }

    // Get the repository available space threshold from the configuration.
    int repoThreshold = ConfigManager.getCurrentConfig().getInt(PARAM_REPOSITORY_AVAIL_SPACE_THRESHOLD,
            DEFAULT_REPOSITORY_AVAIL_SPACE_THRESHOLD);
    if (log.isDebug3())
        log.debug3(DEBUG_HEADER + "repoThreshold = " + repoThreshold);

    // Get the available space of the repository with the least amount of
    // available space.
    long minAvail = sortedRepos.first().getValue().getAvail();
    if (log.isDebug3())
        log.debug3(DEBUG_HEADER + "minAvail = " + minAvail);

    // Remove repositories that don't have a minimum of space, except the last
    // one.
    while (minAvail < repoThreshold) {
        sortedRepos.remove(sortedRepos.first());
        if (log.isDebug3())
            log.debug3(DEBUG_HEADER + "sortedRepos.size() = " + sortedRepos.size());

        // If there is only one repository left, use it.
        if (sortedRepos.size() == 1) {
            repos.add(sortedRepos.first().getKey());
            if (log.isDebug3())
                log.debug3(DEBUG_HEADER + "Added " + sortedRepos.first().getKey());

            if (log.isDebug2())
                log.debug2(DEBUG_HEADER + "repos = " + repos);
            return repos;
        }

        // Get the available space of the repository with the least amount of
        // available space.
        minAvail = sortedRepos.first().getValue().getAvail();
        if (log.isDebug3())
            log.debug3(DEBUG_HEADER + "minAvail = " + minAvail);
    }

    // Count the remaining repositories.
    int repoCount = sortedRepos.size();
    if (log.isDebug3())
        log.debug3(DEBUG_HEADER + "repoCount = " + repoCount);

    // Initialize the array of repositories and the total available space.
    long totalAvailable = 0l;
    int i = 0;
    Entry<String, PlatformUtil.DF>[] repoArray = new Entry[repoCount];

    for (Entry<String, PlatformUtil.DF> df : sortedRepos) {
        totalAvailable += df.getValue().getAvail();
        if (log.isDebug3())
            log.debug3(DEBUG_HEADER + "totalAvailable = " + totalAvailable);

        repoArray[i++] = df;
    }

    // For each repository, compute the target fraction and initialize the count
    // of appearances in the final list.
    i = 0;
    double[] repoTargetFraction = new double[repoCount];
    int[] repoAppearances = new int[repoCount];

    for (Entry<String, PlatformUtil.DF> df : repoArray) {
        repoTargetFraction[i] = df.getValue().getAvail() / (double) totalAvailable;
        if (log.isDebug3())
            log.debug3(DEBUG_HEADER + "i = " + i + ", repoTargetFraction[i] = " + repoTargetFraction[i]);

        repoAppearances[i++] = 0;
    }

    // The first repository in the list is the one with the largest amount of
    // available space.
    repos.add(repoArray[repoCount - 1].getKey());
    repoAppearances[repoCount - 1]++;

    // An indication of whether the created list matches the target fractions of
    // all the repositories.
    boolean done = false;

    while (!done) {
        // If no differences between the target fractions and the fractions of
        // appearances are found in the process below, the list is complete.
        done = true;

        double difference = 0;
        double maxDifference = 0;
        int nextRepo = -1;

        // Loop through all the repositories.
        for (int j = 0; j < repoCount; j++) {
            if (log.isDebug3())
                log.debug3(DEBUG_HEADER + "j = " + j + ", repoAppearances[j]/(double)repos.size() = "
                        + repoAppearances[j] / (double) repos.size() + ", repoTargetFraction[j] = "
                        + repoTargetFraction[j]);

            // Find the difference between the target fraction and the fraction of
            // appearances.
            difference = repoTargetFraction[j] - repoAppearances[j] / (double) repos.size();
            if (log.isDebug3())
                log.debug3(DEBUG_HEADER + "difference = " + difference);

            // Update the largest difference, if necessary.
            if (maxDifference < difference) {
                maxDifference = difference;
                nextRepo = j;
            }
        }

        // Check whether a repository with the largest difference was found.
        if (nextRepo != -1) {
            // Yes: Add it to the list.
            repos.add(repoArray[nextRepo].getKey());
            if (log.isDebug3())
                log.debug3(DEBUG_HEADER + "Added " + repoArray[nextRepo].getKey());

            // Increment its appearance count.
            repoAppearances[nextRepo]++;

            // Check whether not all the target fractions have been achieved.
            for (int k = 0; k < repoCount; k++) {
                difference = repoAppearances[k] / (double) repos.size() - repoTargetFraction[k];
                if (log.isDebug3())
                    log.debug3(DEBUG_HEADER + "k = " + k + ", difference = " + difference);

                // Within one per cent is a match.
                if (Math.abs(difference) > 0.01) {
                    done = false;
                    break;
                }
            }
        }
    }

    if (log.isDebug2())
        log.debug2(DEBUG_HEADER + "repos = " + repos);
    return repos;
}

From source file:com.ichi2.anki2.DeckPicker.java

private void updateDecksList(TreeSet<Object[]> decks, int eta, int count) {
    if (decks == null) {
        Log.e(AnkiDroidApp.TAG, "updateDecksList: empty decks list");
        return;/*from www  .ja v a  2 s . c  o  m*/
    }
    mDeckList.clear();
    int due = 0;
    for (Object[] d : decks) {
        HashMap<String, String> m = new HashMap<String, String>();
        String[] name = ((String[]) d[0]);
        m.put("name", readableDeckName(name));
        m.put("did", ((Long) d[1]).toString());
        m.put("new", ((Integer) d[2]).toString());
        m.put("lrn", ((Integer) d[3]).toString());
        m.put("rev", ((Integer) d[4]).toString());
        m.put("dyn", ((Boolean) d[5]) ? "d1" : "d0");
        // m.put("complMat", ((Float)d[5]).toString());
        // m.put("complAll", ((Float)d[6]).toString());
        if (name.length == 1) {
            due += Integer.parseInt(m.get("new")) + Integer.parseInt(m.get("lrn"))
                    + Integer.parseInt(m.get("rev"));
            // top position
            m.put("sep", "top");
            // correct previous deck
            if (mDeckList.size() > 0) {
                HashMap<String, String> map = mDeckList.get(mDeckList.size() - 1);
                if (map.get("sep").equals("top")) {
                    map.put("sep", "ful");
                } else {
                    map.put("sep", "bot");
                }
            }
        } else {
            // center position
            m.put("sep", "cen");
        }
        if (mDeckList.size() > 0 && mDeckList.size() == decks.size() - 1) {
            // bottom position
            if (name.length == 1) {
                m.put("sep", "ful");
            } else {
                m.put("sep", "bot");
            }
        }
        mDeckList.add(m);
    }
    mDeckListAdapter.notifyDataSetChanged();

    // set title
    Resources res = getResources();
    if (count != -1) {
        String time = "-";
        if (eta != -1) {
            time = res.getQuantityString(R.plurals.deckpicker_title_minutes, eta, eta);
        }
        AnkiDroidApp.getCompat().setSubtitle(this,
                res.getQuantityString(R.plurals.deckpicker_title, due, due, count, time));
    }
    setTitle(res.getString(R.string.app_name));

    // update widget
    WidgetStatus.update(this, decks);
}

From source file:com.joliciel.talismane.parser.TransitionBasedGlobalLearningParser.java

public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences,
        FeatureWeightVector weightVector, RankingSolution correctSolution) {
    MONITOR.startTask("parseSentence");
    try {/*from   w  ww . j  av a 2 s  .  co  m*/
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, TreeSet<ParseConfiguration>> heaps = new TreeMap<Integer, TreeSet<ParseConfiguration>>();

        TreeSet<ParseConfiguration> heap0 = new TreeSet<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(new SimpleRankingScoringStrategy());
            initialConfiguration.setRankingScore(0.0);
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        TreeSet<ParseConfiguration> backupHeap = null;

        TreeSet<ParseConfiguration> finalHeap = null;
        while (heaps.size() > 0) {
            Entry<Integer, TreeSet<ParseConfiguration>> heapEntry = heaps.firstEntry();
            TreeSet<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new TreeSet<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.first();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            // check if we've gone over alloted time for this sentence
            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            // check if we've enough memory to process this sentence
            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            // check if any of the remaining top-N solutions on any heap can lead to the correct solution
            if (correctSolution != null) {
                boolean canReachCorrectSolution = false;
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    int j = 1;
                    for (ParseConfiguration solution : heap) {
                        if (j > beamWidth)
                            break;
                        if (solution.canReach(correctSolution)) {
                            canReachCorrectSolution = true;
                            break;
                        }
                        j++;
                    }
                    if (canReachCorrectSolution)
                        break;
                }
                if (!canReachCorrectSolution) {
                    LOG.debug("None of the solutions on the heap can reach the gold solution. Exiting.");
                    finished = true;
                }
            }

            if (finished) {
                // combine any remaining heaps
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    if (finalHeap != heap) {
                        finalHeap.addAll(heap);
                    }
                }
                break;
            }

            // remove heap from set of heaps
            heapEntry = heaps.pollFirstEntry();

            // limit the breadth to K
            int maxSolutions = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.pollFirst();
                backupHeap.add(history);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                Set<Transition> transitions = new HashSet<Transition>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.getCondition().getName());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                transitions.add(rule.getTransition());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }

                                if (!rule.getTransition().checkPreconditions(history)) {
                                    LOG.error("Cannot apply rule, preconditions not met.");
                                    ruleApplied = false;
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    transitions = parsingConstrainer.getPossibleTransitions(history);

                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    for (Transition transition : transitions) {
                        if (!transition.checkPreconditions(history)) {
                            eliminatedTransitions.add(transition);
                        }
                    }
                    transitions.removeAll(eliminatedTransitions);

                    // apply the negative rules
                    eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.getCondition().getName());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.add(rule.getTransition());
                                    if (LOG.isTraceEnabled()) {
                                        LOG.debug("Rule applies. Eliminating transition: "
                                                + rule.getTransition().getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() == transitions.size()) {
                                LOG.debug("All transitions eliminated! Restoring original transitions.");
                            } else {
                                transitions.removeAll(eliminatedTransitions);
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                if (transitions.size() == 0) {
                    // just in case the we run out of both heaps and analyses, we build this backup heap
                    backupHeap.add(history);
                    if (LOG.isTraceEnabled())
                        LOG.trace(
                                "No transitions could be applied: not counting this solution as part of the beam");
                } else {
                    // up the counter, since we will count this solution towards the heap
                    j++;
                    // add solutions to the heap, one per valid transition
                    MONITOR.startTask("heap sort");
                    try {
                        Map<Transition, Double> deltaScorePerTransition = new HashMap<Transition, Double>();
                        double absoluteMax = 1;

                        for (Transition transition : transitions) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Applying transition: " + transition.getCode());
                            }
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            transition.apply(configuration);
                            configuration.setRankingScore(history.getRankingScore());
                            configuration.getIncrementalFeatureResults()
                                    .addAll(history.getIncrementalFeatureResults());

                            // test the features on the new configuration
                            double scoreDelta = 0.0;
                            MONITOR.startTask("feature analyse");
                            List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
                            try {
                                for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                                    MONITOR.startTask(feature.getName());
                                    try {
                                        RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                        FeatureResult<?> featureResult = feature.check(configuration, env);
                                        if (featureResult != null) {
                                            featureResults.add(featureResult);
                                            double weight = weightVector.getWeight(featureResult);
                                            scoreDelta += weight;
                                            if (LOG.isTraceEnabled()) {
                                                LOG.trace(featureResult.toString() + " = " + weight);
                                            }
                                        }
                                    } finally {
                                        MONITOR.endTask(feature.getName());
                                    }
                                }
                                configuration.getIncrementalFeatureResults().add(featureResults);
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Score = " + configuration.getRankingScore() + " + " + scoreDelta
                                            + " = " + (configuration.getRankingScore() + scoreDelta));
                                }
                                configuration.setRankingScore(configuration.getRankingScore() + scoreDelta);
                                deltaScorePerTransition.put(transition, scoreDelta);
                                if (Math.abs(scoreDelta) > absoluteMax)
                                    absoluteMax = Math.abs(scoreDelta);

                            } finally {
                                MONITOR.endTask("feature analyse");
                            }

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            while (nextHeapIndex <= currentHeapIndex)
                                nextHeapIndex++;

                            TreeSet<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                nextHeap = new TreeSet<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } // next transition

                        // Create a probability distribution of transitions
                        // normalise probabilities for each transition via normalised exponential
                        // e^(x/absmax)/sum(e^(x/absmax))
                        // where x/absmax is in [-1,1]
                        // e^(x/absmax) is in [1/e,e]

                        double total = 0.0;
                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double deltaScore = deltaScorePerTransition.get(transition);
                            deltaScore = Math.exp(deltaScore / absoluteMax);
                            deltaScorePerTransition.put(transition, deltaScore);
                            total += deltaScore;
                        }

                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double probability = deltaScorePerTransition.get(transition);
                            probability /= total;
                            Decision<Transition> decision = machineLearningService.createDecision(transition,
                                    probability);
                            transition.setDecision(decision);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Transition: " + transition.getCode() + ", Prob: " + probability);
                            }
                        }

                    } finally {
                        MONITOR.endTask("heap sort");
                    }
                } // have we any transitions?

                // beam width test
                if (j == maxSolutions)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.pollFirst());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            if (correctSolution != null) {
                LOG.debug("Gold transitions: " + correctSolution.getIncrementalOutcomes());
            }
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

From source file:com.joliciel.jochre.graphics.ShapeImpl.java

@Override
public Collection<BridgeCandidate> getBridgeCandidates(double maxBridgeWidth) {
    if (this.bridgeCandidates == null) {
        TreeSet<VerticalLineSegment> lines = this.getVerticalLineSegments();

        // Now, detect "bridges" which could indicate that the shape should be split

        // First, detect which spaces are "enclosed" and which touch the outer walls
        // To do this, build up a set of all inverse (white) lines
        TreeSet<VerticalLineSegment> inverseLines = new TreeSet<VerticalLineSegment>();
        int currentX = -1;
        VerticalLineSegment previousLine = null;
        for (VerticalLineSegment line : lines) {
            //LOG.debug("Checking line x = " + line.x + ", top = " + line.yTop + ", bottom = " + line.yBottom);
            if (line.x != currentX) {
                // new x-coordinate
                if (previousLine != null && previousLine.yBottom < this.getHeight() - 1) {
                    VerticalLineSegment inverseLine = new VerticalLineSegment(previousLine.x,
                            previousLine.yBottom + 1);
                    inverseLine.yBottom = this.getHeight() - 1;
                    inverseLines.add(inverseLine);
                    //LOG.debug("Adding inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop + ", bottom = " + inverseLine.yBottom);
                }// w ww.j  a v a2 s  .  c  o  m
                if (line.yTop > 0) {
                    VerticalLineSegment inverseLine = new VerticalLineSegment(line.x, line.yTop - 1);
                    inverseLine.yTop = 0;
                    inverseLines.add(inverseLine);
                    //LOG.debug("Adding inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop + ", bottom = " + inverseLine.yBottom);
                }
                currentX = line.x;
            } else if (previousLine != null) {
                VerticalLineSegment inverseLine = new VerticalLineSegment(previousLine.x,
                        previousLine.yBottom + 1);
                inverseLine.yBottom = line.yTop - 1;
                inverseLines.add(inverseLine);
                //LOG.debug("Adding inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop + ", bottom = " + inverseLine.yBottom);
            }
            previousLine = line;
        }
        if (previousLine != null && previousLine.yBottom < this.getHeight() - 1) {
            VerticalLineSegment inverseLine = new VerticalLineSegment(previousLine.x, previousLine.yBottom + 1);
            inverseLine.yBottom = this.getHeight() - 1;
            inverseLines.add(inverseLine);
            //LOG.debug("Adding inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop + ", bottom = " + inverseLine.yBottom);
        }
        LOG.debug("inverseLines size: " + inverseLines.size());

        // Calculate neighbours for inverse lines
        for (VerticalLineSegment inverseLine : inverseLines) {
            for (VerticalLineSegment otherLine : inverseLines) {
                if (otherLine.x == inverseLine.x + 1) {
                    if (inverseLine.yTop - 1 <= otherLine.yBottom
                            && otherLine.yTop <= inverseLine.yBottom + 1) {
                        inverseLine.rightSegments.add(otherLine);
                        otherLine.leftSegments.add(inverseLine);
                    }
                }
                if (otherLine.x == inverseLine.x - 1) {
                    if (inverseLine.yTop - 1 <= otherLine.yBottom
                            && otherLine.yTop <= inverseLine.yBottom + 1) {
                        inverseLine.leftSegments.add(otherLine);
                        otherLine.rightSegments.add(inverseLine);
                    }
                }
            }
        }

        // Eliminate any white lines which somehow touch an edge
        Stack<VerticalLineSegment> lineStack = new Stack<VerticalLineSegment>();
        Set<VerticalLineSegment> outerInverseLines = new HashSet<VerticalLineSegment>();
        for (VerticalLineSegment inverseLine : inverseLines) {
            if (inverseLine.yTop == 0 || inverseLine.x == 0 || inverseLine.yBottom == this.getHeight() - 1
                    || inverseLine.x == this.getWidth() - 1)
                lineStack.push(inverseLine);
        }
        while (!lineStack.isEmpty()) {
            VerticalLineSegment inverseLine = lineStack.pop();
            if (!inverseLine.touched) {
                inverseLine.touched = true;
                outerInverseLines.add(inverseLine);
                //LOG.debug("Outer inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop + ", bottom = " + inverseLine.yBottom);

                for (VerticalLineSegment rightLine : inverseLine.rightSegments)
                    lineStack.push(rightLine);
                for (VerticalLineSegment leftLine : inverseLine.leftSegments) {
                    lineStack.push(leftLine);
                }
            }
        }
        LOG.debug("outerInverseLines size: " + outerInverseLines.size());

        Set<VerticalLineSegment> enclosedInverseLines = new HashSet<VerticalLineSegment>(inverseLines);
        enclosedInverseLines.removeAll(outerInverseLines);
        LOG.debug("enclosedInverseLines.size: " + enclosedInverseLines.size());
        if (LOG.isDebugEnabled()) {
            for (VerticalLineSegment inverseLine : enclosedInverseLines)
                LOG.debug("Enclosed inverse line x = " + inverseLine.x + ", top = " + inverseLine.yTop
                        + ", bottom = " + inverseLine.yBottom);
        }

        // Add bridge candidates
        // based on maximum line length and having exactly one neighbour on each side      
        LOG.debug("Adding bridge candidates");
        List<BridgeCandidate> candidateList = new ArrayList<BridgeCandidate>();
        for (VerticalLineSegment line : lines) {
            if (line.rightSegments.size() == 1 && line.leftSegments.size() == 1
                    && line.length() <= maxBridgeWidth) {
                // also the bridge width should be considered where two vertical lines touch each other
                // rather than for the full length of the line
                BridgeCandidate candidate = null;
                VerticalLineSegment rightLine = line.rightSegments.iterator().next();
                VerticalLineSegment leftLine = line.leftSegments.iterator().next();
                int leftTopTouch = (leftLine.yTop > line.yTop ? leftLine.yTop : line.yTop);
                int leftBottomTouch = (leftLine.yBottom < line.yBottom ? leftLine.yBottom : line.yBottom);
                int rightTopTouch = (rightLine.yTop > line.yTop ? rightLine.yTop : line.yTop);
                int rightBottomTouch = (rightLine.yBottom < line.yBottom ? rightLine.yBottom : line.yBottom);

                int rightLength = rightTopTouch - rightBottomTouch;
                int leftLength = leftTopTouch - leftBottomTouch;

                if (line.length() <= maxBridgeWidth || rightLength <= maxBridgeWidth
                        || leftLength <= maxBridgeWidth) {
                    candidate = new BridgeCandidate(this, line);

                    if (rightLength < leftLength && rightLength < line.length()) {
                        candidate.topTouch = rightTopTouch;
                        candidate.bottomTouch = rightBottomTouch;
                    } else if (leftLength < line.length()) {
                        candidate.topTouch = leftTopTouch;
                        candidate.bottomTouch = leftBottomTouch;
                    }
                    LOG.debug("Adding bridge candidate x = " + candidate.x + ", top = " + candidate.yTop
                            + ", bottom = " + candidate.yBottom);
                    candidateList.add(candidate);
                }
            }
        }
        LOG.debug("Bridge candidate size: " + candidateList.size());

        LOG.debug("Eliminating candidates with shorter neighbor");
        Set<BridgeCandidate> candidatesToEliminate = null;
        if (candidateList.size() > 0) {
            // eliminate any bridge candidates that touch a shorter bridge candidate
            candidatesToEliminate = new HashSet<BridgeCandidate>();
            for (int i = 0; i < candidateList.size() - 1; i++) {
                BridgeCandidate candidate = candidateList.get(i);
                for (int j = i + 1; j < candidateList.size(); j++) {
                    BridgeCandidate otherCandidate = candidateList.get(j);
                    if (otherCandidate.x == candidate.x + 1
                            && candidate.rightSegments.contains(otherCandidate)) {
                        if ((candidate.bridgeWidth()) <= (otherCandidate.bridgeWidth())) {
                            LOG.debug("Eliminating candidate x = " + otherCandidate.x + ", top = "
                                    + otherCandidate.yTop + ", bottom = " + otherCandidate.yBottom);
                            candidatesToEliminate.add(otherCandidate);
                        } else {
                            LOG.debug("Eliminating candidate x = " + candidate.x + ", top = " + candidate.yTop
                                    + ", bottom = " + candidate.yBottom);
                            candidatesToEliminate.add(candidate);
                        }
                    }
                }
            }
            candidateList.removeAll(candidatesToEliminate);

            LOG.debug("Bridge candidate size: " + candidateList.size());

            // To be a bridge, three additional things have to be true:
            // (A) intersection between right & left shape = null
            // (B) weight of right shape & weight of left shape > a certain threshold
            // (C) little overlap right boundary of left shape, left boundary of right shape

            LOG.debug("Eliminating candidates touching enclosed space");
            // (A) intersection between right & left shape = null
            // Intersection between right and left shape is non-null
            // if the line segment X touches an enclosed space immediately above or below
            candidatesToEliminate = new HashSet<BridgeCandidate>();
            for (BridgeCandidate candidate : candidateList) {
                boolean nullIntersection = true;
                for (VerticalLineSegment inverseLine : enclosedInverseLines) {
                    if (candidate.x == inverseLine.x) {
                        if (inverseLine.yBottom == candidate.yTop - 1
                                || inverseLine.yTop == candidate.yBottom + 1) {
                            nullIntersection = false;
                            break;
                        }
                    }
                }
                if (!nullIntersection) {
                    LOG.debug("Eliminating candidate x = " + candidate.x + ", top = " + candidate.yTop
                            + ", bottom = " + candidate.yBottom);
                    candidatesToEliminate.add(candidate);
                }
            }
            candidateList.removeAll(candidatesToEliminate);
            LOG.debug("Remaining bridge candidate size: " + candidateList.size());

            // another criterion for avoiding "false splits" is that on both side of the bridge
            // the shapes pretty rapidly expand in width both up and down
            LOG.debug("Eliminating candidates without vertical expansion on both sides");
            candidatesToEliminate = new HashSet<BridgeCandidate>();
            int expansionLimit = (int) Math.ceil(((double) this.getWidth()) / 6.0);
            for (BridgeCandidate candidate : candidateList) {
                // take into account the portion touching on the right or left
                boolean isCandidate = true;
                Stack<VerticalLineSegment> leftLines = new Stack<VerticalLineSegment>();
                Stack<Integer> leftDepths = new Stack<Integer>();
                leftLines.push(candidate);
                leftDepths.push(0);
                int leftTop = candidate.topTouch;
                int leftBottom = candidate.bottomTouch;
                while (!leftLines.isEmpty()) {
                    VerticalLineSegment line = leftLines.pop();
                    int depth = leftDepths.pop();
                    if (line.yTop < leftTop)
                        leftTop = line.yTop;
                    if (line.yBottom > leftBottom)
                        leftBottom = line.yBottom;
                    if (depth <= expansionLimit) {
                        for (VerticalLineSegment leftSegment : line.leftSegments) {
                            leftLines.push(leftSegment);
                            leftDepths.push(depth + 1);
                        }
                    }
                }
                if (leftTop == candidate.topTouch || leftBottom == candidate.bottomTouch)
                    isCandidate = false;
                if (isCandidate) {
                    Stack<VerticalLineSegment> rightLines = new Stack<VerticalLineSegment>();
                    Stack<Integer> rightDepths = new Stack<Integer>();
                    rightLines.push(candidate);
                    rightDepths.push(0);
                    int rightTop = candidate.topTouch;
                    int rightBottom = candidate.bottomTouch;
                    while (!rightLines.isEmpty()) {
                        VerticalLineSegment line = rightLines.pop();
                        int depth = rightDepths.pop();
                        if (line.yTop < rightTop)
                            rightTop = line.yTop;
                        if (line.yBottom > rightBottom)
                            rightBottom = line.yBottom;
                        if (depth <= expansionLimit) {
                            for (VerticalLineSegment rightSegment : line.rightSegments) {
                                rightLines.push(rightSegment);
                                rightDepths.push(depth + 1);
                            }
                        }
                    }
                    if (rightTop == candidate.topTouch || rightBottom == candidate.bottomTouch)
                        isCandidate = false;
                }
                if (!isCandidate) {
                    LOG.debug("Eliminating candidate x = " + candidate.x + ", top = " + candidate.yTop
                            + ", bottom = " + candidate.yBottom);
                    candidatesToEliminate.add(candidate);
                }
            }
            candidateList.removeAll(candidatesToEliminate);
            LOG.debug("Remaining bridge candidate size: " + candidateList.size());

            if (LOG.isDebugEnabled()) {
                for (VerticalLineSegment candidate : candidateList) {
                    LOG.debug("Remaining candidate x = " + candidate.x + ", top = " + candidate.yTop
                            + ", bottom = " + candidate.yBottom);
                }
            }
        }

        if (candidateList.size() > 0) {
            // (B) weight of right shape & weight of left shape > a certain threshold
            // (C) little overlap right boundary of left shape, left boundary of right shape
            // 
            // We can now divide the shape into n groups, each separated by a candidate
            // We recursively build a group until we reach a candidate
            // and indicate whether it's the right or left border of the candidate.
            // We then keep going from the candidate on to the next one
            // We keep tab of the size of each group and of its right & left boundaries
            // at the end we can easily determine the right and left boundaries of each,
            // as well as the right & left pixel weight
            List<VerticalLineGroup> groups = new ArrayList<VerticalLineGroup>();

            VerticalLineSegment firstLine = lines.first();
            lineStack = new Stack<VerticalLineSegment>();
            Stack<BridgeCandidate> candidateStack = new Stack<BridgeCandidate>();
            Stack<Boolean> fromLeftStack = new Stack<Boolean>();
            Stack<Boolean> candidateFromLeftStack = new Stack<Boolean>();
            lineStack.push(firstLine);
            fromLeftStack.push(true);
            VerticalLineGroup group = new VerticalLineGroup(this);
            List<BridgeCandidate> touchedCandidates = new ArrayList<BridgeCandidate>();
            while (!lineStack.isEmpty()) {
                while (!lineStack.isEmpty()) {
                    VerticalLineSegment line = lineStack.pop();
                    boolean fromLeft = fromLeftStack.pop();
                    if (line.touched)
                        continue;

                    line.touched = true;
                    if (candidateList.contains(line)) {
                        // a candidate!
                        LOG.debug("Touching candidate x = " + line.x + ", top = " + line.yTop + ", bottom = "
                                + line.yBottom);
                        BridgeCandidate candidate = null;
                        for (BridgeCandidate existingCandidate : candidateList) {
                            if (existingCandidate.equals(line)) {
                                candidate = existingCandidate;
                                break;
                            }
                        }

                        boolean foundCandidate = touchedCandidates.contains(candidate);

                        if (!foundCandidate) {
                            touchedCandidates.add(candidate);
                            candidateStack.push(candidate);
                            candidateFromLeftStack.push(fromLeft);
                            if (fromLeft) {
                                // coming from the left
                                group.rightCandidates.add(candidate);
                                candidate.leftGroup = group;
                            } else {
                                group.leftCandidates.add(candidate);
                                candidate.rightGroup = group;
                            }
                        }
                    } else {
                        // not a candidate
                        LOG.debug("Touching line length = " + line.length() + ", x = " + line.x + ", top = "
                                + line.yTop + ", bottom = " + line.yBottom);
                        group.pixelCount += line.length();
                        if (line.x < group.leftBoundary)
                            group.leftBoundary = line.x;
                        if (line.x > group.rightBoundary)
                            group.rightBoundary = line.x;
                        if (line.yTop < group.topBoundary)
                            group.topBoundary = line.yTop;
                        if (line.yBottom > group.bottomBoundary)
                            group.bottomBoundary = line.yBottom;
                        for (VerticalLineSegment leftLine : line.leftSegments) {
                            lineStack.push(leftLine);
                            fromLeftStack.push(false);
                        }
                        for (VerticalLineSegment rightLine : line.rightSegments) {
                            lineStack.push(rightLine);
                            fromLeftStack.push(true);
                        }
                    }
                } // no more lines in this group
                groups.add(group);
                if (!candidateStack.isEmpty()) {
                    BridgeCandidate candidate = candidateStack.pop();
                    boolean fromLeft = candidateFromLeftStack.pop();
                    //lineStack.push(candidate.line);
                    //fromLeftStack.push(fromLeft);
                    LOG.debug("*** New Group ***");
                    LOG.debug("Next candidate:  x = " + candidate.x + ", top = " + candidate.yTop
                            + ", bottom = " + candidate.yBottom);
                    group = new VerticalLineGroup(this);
                    if (fromLeft) {
                        group.leftCandidates.add(candidate);
                        candidate.rightGroup = group;
                    } else {
                        group.rightCandidates.add(candidate);
                        candidate.leftGroup = group;
                    }

                    // add this candidate's neighbours to the lineStack
                    for (VerticalLineSegment leftLine : candidate.leftSegments) {
                        lineStack.push(leftLine);
                        fromLeftStack.push(false);
                    }
                    for (VerticalLineSegment rightLine : candidate.rightSegments) {
                        lineStack.push(rightLine);
                        fromLeftStack.push(true);
                    }
                } // next candidate on candidate stack
            } // no more lines to process

            if (LOG.isDebugEnabled()) {
                LOG.debug("Found " + groups.size() + " groups");
                int i = 1;
                for (VerticalLineGroup aGroup : groups) {
                    LOG.debug("Group " + i++ + ", pixelCount: " + aGroup.pixelCount + ", leftBoundary: "
                            + aGroup.leftBoundary + ", rightBoundary: " + aGroup.rightBoundary);
                    LOG.debug("Candidates on left: ");
                    for (BridgeCandidate candidate : aGroup.leftCandidates)
                        LOG.debug("Candidate x = " + candidate.x + ", top = " + candidate.yTop + ", bottom = "
                                + candidate.yBottom);
                    LOG.debug("Candidates on right: ");
                    for (BridgeCandidate candidate : aGroup.rightCandidates)
                        LOG.debug("Candidate x = " + candidate.x + ", top = " + candidate.yTop + ", bottom = "
                                + candidate.yBottom);

                }
                LOG.debug("Found " + candidateList.size() + " candidates");
                for (BridgeCandidate candidate : candidateList) {
                    LOG.debug("Candidate x = " + candidate.x + ", top = " + candidate.yTop + ", bottom = "
                            + candidate.yBottom);
                    LOG.debug("- Left group = pixelCount: " + candidate.leftGroup.pixelCount
                            + ", leftBoundary: " + candidate.leftGroup.leftBoundary + ", rightBoundary: "
                            + candidate.leftGroup.rightBoundary);
                    LOG.debug("- Right group = pixelCount: " + candidate.rightGroup.pixelCount
                            + ", leftBoundary: " + candidate.rightGroup.leftBoundary + ", rightBoundary: "
                            + candidate.rightGroup.rightBoundary);
                }
            } // should we log?

            // calculate each candidate's pixel totals and boundaries
            for (BridgeCandidate candidate : candidateList) {
                for (VerticalLineGroup lineGroup : groups)
                    lineGroup.touched = false;
                Stack<VerticalLineGroup> groupStack = new Stack<VerticalLineGroup>();
                groupStack.push(candidate.leftGroup);
                while (!groupStack.isEmpty()) {
                    VerticalLineGroup lineGroup = groupStack.pop();
                    if (lineGroup.touched)
                        continue;
                    lineGroup.touched = true;
                    candidate.leftPixels += lineGroup.pixelCount;
                    if (lineGroup.leftBoundary < candidate.leftShapeLeftBoundary)
                        candidate.leftShapeLeftBoundary = lineGroup.leftBoundary;
                    if (lineGroup.rightBoundary > candidate.leftShapeRightBoundary)
                        candidate.leftShapeRightBoundary = lineGroup.rightBoundary;
                    for (BridgeCandidate leftCandidate : lineGroup.leftCandidates) {
                        if (!candidate.equals(leftCandidate)) {
                            candidate.leftPixels += leftCandidate.length();
                            groupStack.push(leftCandidate.leftGroup);
                        }
                    }
                    for (BridgeCandidate rightCandidate : lineGroup.rightCandidates) {
                        if (!candidate.equals(rightCandidate)) {
                            candidate.leftPixels += rightCandidate.length();
                            groupStack.push(rightCandidate.rightGroup);
                        }
                    }
                } // next left group
                groupStack.push(candidate.rightGroup);
                while (!groupStack.isEmpty()) {
                    VerticalLineGroup lineGroup = groupStack.pop();
                    if (lineGroup.touched)
                        continue;
                    lineGroup.touched = true;
                    candidate.rightPixels += lineGroup.pixelCount;
                    if (lineGroup.leftBoundary < candidate.rightShapeLeftBoundary)
                        candidate.rightShapeLeftBoundary = lineGroup.leftBoundary;
                    if (lineGroup.rightBoundary > candidate.rightShapeRightBoundary)
                        candidate.rightShapeRightBoundary = lineGroup.rightBoundary;
                    for (BridgeCandidate leftCandidate : lineGroup.leftCandidates) {
                        if (!candidate.equals(leftCandidate)) {
                            candidate.rightPixels += leftCandidate.length();
                            groupStack.push(leftCandidate.leftGroup);
                        }
                    }
                    for (BridgeCandidate rightCandidate : lineGroup.rightCandidates) {
                        if (!candidate.equals(rightCandidate)) {
                            candidate.rightPixels += rightCandidate.length();
                            groupStack.push(rightCandidate.rightGroup);
                        }
                    }
                } // next right group
            } // next candidate

        } // do we have any candidates?
        this.bridgeCandidates = candidateList;
    } // lazy load

    return this.bridgeCandidates;
}

From source file:org.dasein.cloud.vcloud.compute.vAppSupport.java

private @Nullable VirtualMachine toVirtualMachine(@Nonnull String vdcId, @Nonnull String parentVAppId,
        @Nonnull Node vmNode, @Nonnull Iterable<VLAN> vlans) throws CloudException, InternalException {
    Node n = vmNode.getAttributes().getNamedItem("href");
    VirtualMachine vm = new VirtualMachine();

    vm.setProviderMachineImageId("unknown");
    vm.setArchitecture(Architecture.I64);
    vm.setClonable(true);/* w  w  w. j av a2s  .com*/
    vm.setCreationTimestamp(0L);
    vm.setCurrentState(VmState.PENDING);
    vm.setImagable(true);
    vm.setLastBootTimestamp(0L);
    vm.setLastPauseTimestamp(0L);
    vm.setPausable(false);
    vm.setPersistent(true);
    vm.setPlatform(Platform.UNKNOWN);
    vm.setProviderOwnerId(getContext().getAccountNumber());
    vm.setRebootable(true);
    vm.setProviderRegionId(getContext().getRegionId());
    vm.setProviderDataCenterId(vdcId);

    if (n != null) {
        vm.setProviderVirtualMachineId((getProvider()).toID(n.getNodeValue().trim()));
    }
    n = vmNode.getAttributes().getNamedItem("status");
    if (n != null) {
        vm.setCurrentState(toState(n.getNodeValue().trim()));
    }
    String vmName = null;
    String computerName = null;
    n = vmNode.getAttributes().getNamedItem("name");
    if (n != null) {
        vmName = n.getNodeValue().trim();
    }
    NodeList attributes = vmNode.getChildNodes();

    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (attribute.getNodeName().equalsIgnoreCase("Description") && attribute.hasChildNodes()) {
            vm.setDescription(attribute.getFirstChild().getNodeValue().trim());
        } else if (attribute.getNodeName().equalsIgnoreCase("GuestCustomizationSection")
                && attribute.hasChildNodes()) {
            NodeList elements = attribute.getChildNodes();
            String adminPassword = null;

            for (int j = 0; j < elements.getLength(); j++) {
                Node element = elements.item(j);

                if (element.getNodeName().equalsIgnoreCase("AdminPassword") && element.hasChildNodes()) {
                    adminPassword = element.getFirstChild().getNodeValue().trim();
                } else if (element.getNodeName().equalsIgnoreCase("ComputerName") && element.hasChildNodes()) {
                    computerName = element.getFirstChild().getNodeValue().trim();
                }
            }
            if (adminPassword != null) {
                vm.setRootUser(vm.getPlatform().isWindows() ? "administrator" : "root");
                vm.setRootPassword(adminPassword);
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("DateCreated") && attribute.hasChildNodes()) {
            vm.setCreationTimestamp((getProvider()).parseTime(attribute.getFirstChild().getNodeValue().trim()));
        } else if (attribute.getNodeName().equalsIgnoreCase("NetworkConnectionSection")
                && attribute.hasChildNodes()) {
            NodeList elements = attribute.getChildNodes();
            TreeSet<String> addrs = new TreeSet<String>();

            for (int j = 0; j < elements.getLength(); j++) {
                Node element = elements.item(j);

                if (element.getNodeName().equalsIgnoreCase("NetworkConnection")) {
                    if (element.hasChildNodes()) {
                        NodeList parts = element.getChildNodes();
                        Boolean connected = null;
                        String addr = null;

                        for (int k = 0; k < parts.getLength(); k++) {
                            Node part = parts.item(k);

                            if (part.getNodeName().equalsIgnoreCase("IpAddress") && part.hasChildNodes()) {
                                addr = part.getFirstChild().getNodeValue().trim();
                            }
                            if (part.getNodeName().equalsIgnoreCase("IsConnected") && part.hasChildNodes()) {
                                connected = part.getFirstChild().getNodeValue().trim().equalsIgnoreCase("true");
                            }
                        }
                        if ((connected == null || connected) && addr != null) {
                            addrs.add(addr);
                        }
                    }
                    if (element.hasAttributes()) {
                        Node net = element.getAttributes().getNamedItem("network");

                        if (net != null) {
                            String netNameOrId = net.getNodeValue().trim();
                            boolean compat = (getProvider()).isCompat();

                            for (VLAN vlan : vlans) {
                                boolean matches = false;

                                if (!compat && vlan.getProviderVlanId().equals(netNameOrId)) {
                                    matches = true;
                                } else if (compat
                                        && vlan.getProviderVlanId().equals("/network/" + netNameOrId)) {
                                    matches = true;
                                } else if (vlan.getName().equals(netNameOrId)) {
                                    matches = true;
                                }
                                if (matches) {
                                    vm.setProviderVlanId(vlan.getProviderVlanId());
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (addrs.size() > 0) {
                if (addrs.size() == 1) {
                    RawAddress a = new RawAddress(addrs.iterator().next());

                    if (isPublicIpAddress(a)) {
                        vm.setPublicAddresses(a);
                    } else {
                        vm.setPrivateAddresses(a);
                    }
                } else {
                    ArrayList<RawAddress> pub = new ArrayList<RawAddress>();
                    ArrayList<RawAddress> priv = new ArrayList<RawAddress>();

                    for (String addr : addrs) {
                        RawAddress r = new RawAddress(addr);

                        if (isPublicIpAddress(r)) {
                            pub.add(r);
                        } else {
                            priv.add(r);
                        }
                    }
                    if (priv.size() > 0) {
                        vm.setPrivateAddresses(priv.toArray(new RawAddress[priv.size()]));
                    }
                    if (pub.size() > 0) {
                        vm.setPublicAddresses(pub.toArray(new RawAddress[pub.size()]));
                    }
                }
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("ovf:OperatingSystemSection")
                && attribute.hasChildNodes()) {
            NodeList os = attribute.getChildNodes();

            for (int j = 0; j < os.getLength(); j++) {
                Node osdesc = os.item(j);

                if (osdesc.getNodeName().equalsIgnoreCase("ovf:Description") && osdesc.hasChildNodes()) {
                    String desc = osdesc.getFirstChild().getNodeValue();

                    vm.setPlatform(Platform.guess(desc));

                    if (desc.contains("32") || (desc.contains("x86") && !desc.contains("64"))) {
                        vm.setArchitecture(Architecture.I32);
                    }
                }
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("ovf:VirtualHardwareSection")
                && attribute.hasChildNodes()) {
            NodeList hardware = attribute.getChildNodes();
            int memory = 0, cpu = 0;

            for (int j = 0; j < hardware.getLength(); j++) {
                Node item = hardware.item(j);

                if (item.getNodeName().equalsIgnoreCase("ovf:item") && item.hasChildNodes()) {
                    NodeList bits = item.getChildNodes();
                    String rt = null;
                    int qty = 0;

                    for (int k = 0; k < bits.getLength(); k++) {
                        Node bit = bits.item(k);

                        if (bit.getNodeName().equalsIgnoreCase("rasd:ResourceType") && bit.hasChildNodes()) {
                            rt = bit.getFirstChild().getNodeValue().trim();
                        } else if (bit.getNodeName().equalsIgnoreCase("rasd:VirtualQuantity")
                                && bit.hasChildNodes()) {
                            try {
                                qty = Integer.parseInt(bit.getFirstChild().getNodeValue().trim());
                            } catch (NumberFormatException ignore) {
                                // ignore
                            }
                        }

                    }
                    if (rt != null) {
                        if (rt.equals("3")) { // cpu
                            cpu = qty;
                        } else if (rt.equals("4")) { // memory
                            memory = qty;
                        }
                        /*
                        else if( rt.equals("10") ) {     // NIC
                                
                        }
                        else if( rt.equals("17") ) {     // disk
                                
                        }
                        */
                    }
                }
            }
            VirtualMachineProduct product = null;

            for (VirtualMachineProduct prd : listProducts("bogus",
                    VirtualMachineProductFilterOptions.getInstance().withArchitecture(Architecture.I64))) {
                if (prd.getCpuCount() == cpu && memory == prd.getRamSize().intValue()) {
                    product = prd;
                    break;
                }
            }
            if (product == null) {
                vm.setProductId("custom:" + cpu + ":" + memory);
            } else {
                vm.setProductId(product.getProviderProductId());
            }
        }
    }
    if (vm.getProviderVirtualMachineId() == null) {
        return null;
    }
    if (vmName != null) {
        vm.setName(vmName);
    } else if (computerName != null) {
        vm.setName(computerName);
    } else {
        vm.setName(vm.getProviderVirtualMachineId());
    }
    if (vm.getDescription() == null) {
        vm.setDescription(vm.getName());
    }
    Platform p = vm.getPlatform();

    if (p == null || p.equals(Platform.UNKNOWN) || p.equals(Platform.UNIX)) {
        p = Platform.guess(vm.getName() + " " + vm.getDescription());
        if (Platform.UNIX.equals(vm.getPlatform())) {
            if (p.isUnix()) {
                vm.setPlatform(p);
            }
        } else {
            vm.setPlatform(p);
        }
    }
    try {
        vCloudMethod method = new vCloudMethod(getProvider());
        String xml = method.get("vApp", vm.getProviderVirtualMachineId() + "/metadata");

        if (xml != null && !xml.equals("")) {
            method.parseMetaData(vm, xml);

            String t;

            if (vm.getCreationTimestamp() < 1L) {
                t = (String) vm.getTag("dsnCreated");
                if (t != null) {
                    try {
                        vm.setCreationTimestamp(Long.parseLong(t));
                    } catch (Throwable parseWarning) {
                        if (logger.isDebugEnabled()) {
                            logger.warn("Failed to parse creation timestamp.", parseWarning);
                        } else {
                            logger.warn("Failed to parse creation timestamp.");
                        }
                    }
                }
            }
            t = (String) vm.getTag("dsnImageId");
            logger.debug("dsnImageId = " + t);
            if (t != null && "unknown".equals(vm.getProviderMachineImageId())) {
                vm.setProviderMachineImageId(t);
                logger.debug("Set provider machine image to " + t);
            }
        }
    } catch (Throwable warning) {
        if (logger.isDebugEnabled()) {
            logger.warn("Failed to get and parse vm metadata.", warning);
        } else {
            logger.warn("Failed to get and parse vm metadata.");
        }
    }
    vm.setTag(PARENT_VAPP_ID, parentVAppId);
    return vm;
}

From source file:ddf.catalog.source.solr.SolrProviderTest.java

@Test
public void testStartIndexWithSorting() throws Exception {
    deleteAllIn(provider);//w w  w.j  a  v  a  2s .  c  o m

    List<Metacard> metacards = new ArrayList<Metacard>();

    DateTime dt = new DateTime(1985, 1, 1, 1, 1, 1, 1, DateTimeZone.UTC);

    TreeSet<Date> calculatedDates = new TreeSet<Date>();

    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 100; i = i + 10) {

            MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());

            // ingest sporadically the effective dates so the default return
            // order won't be ordered
            Date calculatedDate = dt.plusDays(100 - i + 10 - j).toDate();
            calculatedDates.add(calculatedDate);
            metacard.setEffectiveDate(calculatedDate);
            metacards.add(metacard);

        }
    }

    // The TreeSet will sort them, the array will give me access to everyone
    // without an iterator
    Date[] dates = new Date[calculatedDates.size()];
    calculatedDates.toArray(dates);

    /** CREATE **/
    CreateResponse response = create(metacards);

    LOGGER.info("CREATED {} records.", response.getCreatedMetacards().size());

    CommonQueryBuilder queryBuilder = new CommonQueryBuilder();

    QueryImpl query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);

    int maxSize = 20;
    int startIndex = 2;

    // STARTINDEX=2, MAXSIZE=20
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    SortByImpl sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE),
            org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);

    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));

    assertEquals(maxSize, sourceResponse.getResults().size());

    for (int i = 0; i < sourceResponse.getResults().size(); i++) {

        Result r = sourceResponse.getResults().get(i);

        Date effectiveDate = r.getMetacard().getEffectiveDate();

        DateTime currentDate = new DateTime(effectiveDate.getTime());

        LOGGER.debug("Testing current index: {}", startIndex + i);

        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(),
                currentDate.getDayOfYear());

    }

    // STARTINDEX=20, MAXSIZE=5
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);

    maxSize = 5;
    startIndex = 20;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE),
            org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);

    sourceResponse = provider.query(new QueryRequestImpl(query));

    assertEquals(maxSize, sourceResponse.getResults().size());

    for (int i = 0; i < sourceResponse.getResults().size(); i++) {

        Result r = sourceResponse.getResults().get(i);

        Date effectiveDate = r.getMetacard().getEffectiveDate();

        DateTime currentDate = new DateTime(effectiveDate.getTime());

        LOGGER.debug("Testing current index: {}", startIndex + i);

        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(),
                currentDate.getDayOfYear());

    }

    // STARTINDEX=80, MAXSIZE=20
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);

    maxSize = 20;
    startIndex = 80;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE),
            org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);

    sourceResponse = provider.query(new QueryRequestImpl(query));

    assertEquals(maxSize, sourceResponse.getResults().size());

    for (int i = 0; i < sourceResponse.getResults().size(); i++) {

        Result r = sourceResponse.getResults().get(i);

        Date effectiveDate = r.getMetacard().getEffectiveDate();

        DateTime currentDate = new DateTime(effectiveDate.getTime());

        LOGGER.debug("Testing current index: {}", startIndex + i);

        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(),
                currentDate.getDayOfYear());

    }

    // STARTINDEX=1, MAXSIZE=100
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);

    maxSize = 100;
    startIndex = 1;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE),
            org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);

    sourceResponse = provider.query(new QueryRequestImpl(query));

    assertEquals(maxSize, sourceResponse.getResults().size());

    for (int i = 0; i < sourceResponse.getResults().size(); i++) {

        Result r = sourceResponse.getResults().get(i);

        Date effectiveDate = r.getMetacard().getEffectiveDate();

        DateTime currentDate = new DateTime(effectiveDate.getTime());

        LOGGER.debug("Testing current index: {}", startIndex + i);

        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(),
                currentDate.getDayOfYear());

    }
}

From source file:net.spfbl.http.ServerHTTP.java

private static String getDNSBLHTML(Locale locale, Client client, final String query, String message) {
    StringBuilder builder = new StringBuilder();
    boolean isSLAAC = SubnetIPv6.isSLAAC(query) && !Subnet.isReservedIP(query);
    Boolean isOpenSMTP = openSMTP.get(query);
    builder.append("<!DOCTYPE html>\n");
    builder.append("<html lang=\"");
    builder.append(locale.getLanguage());
    builder.append("\">\n");
    String title;//from w w  w. ja v a2 s .com
    if (locale.getLanguage().toLowerCase().equals("pt")) {
        title = "Pgina de checagem DNSBL";
    } else {
        title = "DNSBL check page";
    }
    if (isSLAAC && isOpenSMTP == null) {
        if (openSMTP.containsKey(query)) {
            buildHead(builder, title, Core.getURL(locale, query), 5);
        } else {
            buildHead(builder, title, Core.getURL(locale, query), 10);
            openSMTP.put(query, null);
            new Thread() {
                @Override
                public void run() {
                    Thread.currentThread().setName("BCKGROUND");
                    openSMTP.put(query, Analise.isOpenSMTP(query, 30000));
                }
            }.start();
        }
    } else {
        buildHead(builder, title);
    }
    builder.append("  <body>\n");
    builder.append("    <div id=\"container\">\n");
    builder.append(
            "      <iframe data-aa='455818' src='//ad.a-ads.com/455818?size=468x60' scrolling='no' style='width:468px; height:60px; border:0px; padding:0;overflow:hidden' allowtransparency='true'></iframe>");
    buildMessage(builder, message);
    TreeSet<String> emailSet = new TreeSet<String>();
    if (Subnet.isValidIP(query)) {
        String ip = Subnet.normalizeIP(query);
        if (Subnet.isReservedIP(ip)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Este  um IP reservado e por este motivo no  abordado nesta lista.");
            } else {
                buildText(builder, "This is a reserved IP and for this reason is not addressed in this list.");
            }
        } else if (isSLAAC && isOpenSMTP == null) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este IP contm sinalizao de autoconfigurao de endereo de rede (SLAAC).");
                buildText(builder, "Estamos verificando se existe servio SMTP neste IP. Aguarde...");
            } else {
                buildText(builder, "This IP contains network address autoconfiguration flag (SLAAC).");
                buildText(builder, "We are checking if there is SMTP service on this IP. Wait...");
            }
        } else if (isSLAAC && !isOpenSMTP) {
            openSMTP.remove(query);
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este IP contm sinalizao de autoconfigurao de endereo de rede (SLAAC) mas no foi possvel verificar se existe um servio de e-mail vlido nele.");
                buildText(builder,
                        "Se este IP est sendo usado por um servidor de e-mail legtimo, abra a porta 25 para que possamos verificar que existe um servio SMTP nele.");
            } else {
                buildText(builder,
                        "This IP contains network address autoconfiguration flag (SLAAC) but it was not possible to verify that there is a valid email service on it.");
                buildText(builder,
                        "If this IP is being used by genuine email server, open port 25 so we can check that there is a SMTP service on it.");
            }
        } else {
            openSMTP.remove(query);
            boolean generic = false;
            Reverse reverse = Reverse.get(ip, true);
            TreeSet<String> reverseSet = reverse.getAddressSet();
            if (reverseSet.isEmpty()) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Nenhum <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> foi encontrado.");
                } else {
                    buildText(builder,
                            "No <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> was found.");
                }
            } else {
                if (reverseSet.size() == 1) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este  o <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> encontrado:");
                    } else {
                        buildText(builder,
                                "This is the <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> found:");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Estes so os <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> encontrados:");
                    } else {
                        buildText(builder,
                                "These are the <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> found:");
                    }
                }
                Client abuseClient = Client.getByIP(ip);
                String abuseEmail = abuseClient == null || abuseClient.hasPermission(NONE) ? null
                        : abuseClient.getEmail();
                String clientEmail = client == null || client.hasPermission(NONE) ? null : client.getEmail();
                builder.append("        <ul>\n");
                String hostname = reverseSet.pollFirst();
                do {
                    hostname = Domain.normalizeHostname(hostname, false);
                    builder.append("          <li>&lt;");
                    builder.append(hostname);
                    builder.append("&gt; ");
                    if (Generic.containsDynamic(hostname)) {
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("<a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> ");
                            builder.append(
                                    "de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>.</li>\n");
                        } else {
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> ");
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a>.</li>\n");
                        }
                    } else if (SPF.matchHELO(ip, hostname, true)) {
                        String domain;
                        try {
                            domain = Domain.extractDomain(hostname, false);
                        } catch (ProcessException ex) {
                            domain = null;
                        }
                        if (domain == null) {
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio reservado.</li>\n");
                            } else {
                                builder.append("reserved domain.</li>\n");
                            }
                        } else if (hostname.endsWith(".arpa")) {
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio reservado.</li>\n");
                            } else {
                                builder.append("reserved domain.</li>\n");
                            }
                        } else if (Generic.containsGeneric(domain)) {
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio genrico.</li>\n");
                            } else {
                                builder.append("generic domain.</li>\n");
                            }
                        } else if (Generic.containsGeneric(hostname)) {
                            emailSet.add("postmaster@" + domain);
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (clientEmail != null) {
                                emailSet.add(clientEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/generic/\">rDNS genrico</a>.</li>\n");
                            } else {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/en/generic/\">generic rDNS</a>.</li>\n");
                            }
                        } else {
                            int loop = 0;
                            String subdominio = hostname;
                            while (loop++ < 32 && subdominio.endsWith(domain)) {
                                emailSet.add("postmaster@" + subdominio);
                                int index = subdominio.indexOf('.', 1) + 1;
                                subdominio = subdominio.substring(index);
                            }
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (clientEmail != null) {
                                emailSet.add(clientEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.</li>\n");
                            } else {
                                builder.append(
                                        "valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.</li>\n");
                            }
                        }
                    } else {
                        if (abuseEmail != null) {
                            emailSet.add(abuseEmail);
                        }
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> invlido.</li>\n");
                        } else {
                            builder.append(
                                    "invalid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.</li>\n");
                        }
                    }
                } while ((hostname = reverseSet.pollFirst()) != null);
                builder.append("        </ul>\n");
            }
            Distribution distribution;
            if ((distribution = SPF.getDistribution(ip, true)).isNotGreen(ip)) {
                float probability = distribution.getSpamProbability(ip);
                boolean blocked = Block.containsCIDR(ip);
                if (blocked || distribution.isRed(ip)) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este IP " + (blocked ? "foi bloqueado" : "est listado")
                                        + " por m reputao com " + Core.PERCENT_FORMAT.format(probability)
                                        + " de pontos negativos.");
                        buildText(builder,
                                "Para que este IP possa ser removido desta lista,  necessrio que o MTA de origem reduza o volume de envios para os destinatrios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a> na camada SMTP.");
                    } else {
                        buildText(builder,
                                "This IP " + (blocked ? "was blocked" : "is listed") + " by poor reputation in "
                                        + Core.PERCENT_FORMAT.format(probability) + " of negative points.");
                        buildText(builder,
                                "In order for this IP to be removed from this list, it is necessary that the source MTA reduce the sending volume for the recipients with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a> at SMTP layer.");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este IP no est listado neste sistema porm sua reputao est com "
                                        + Core.PERCENT_FORMAT.format(probability) + "de pontos negativos.");
                        buildText(builder,
                                "Se esta reputao tiver aumento significativo na quantidade de pontos negativos, este IP ser automaticamente listado neste sistema.");
                        buildText(builder,
                                "Para evitar que isto ocorra, reduza os envios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a>.");
                    } else {
                        buildText(builder, "This IP is not listed in this system but its reputation is with "
                                + Core.PERCENT_FORMAT.format(probability) + " of negative points.");
                        buildText(builder,
                                "If this reputation have significant increase in the number of negative points, this IP will automatically be listed in the system.");
                        buildText(builder,
                                "To prevent this from occurring, reduce sending with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a>.");
                    }
                }
            } else if (emailSet.isEmpty()) {
                boolean good = SPF.isGood(ip);
                boolean blocked = Block.containsCIDR(ip);
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    if (blocked) {
                        buildText(builder,
                                "Este IP foi bloqueado por no ter <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else if (good) {
                        buildText(builder,
                                "Este IP est com reputao muito boa, porm no tem um <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else {
                        buildText(builder,
                                "Este IP no est bloqueado porm no tem um <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    }
                    if (generic) {
                        buildText(builder,
                                "No sero aceitos <a target=\"_blank\" href=\"http://spfbl.net/generic/\">rDNS genricos</a>.");
                    }
                    buildText(builder,
                            "Cadastre um <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> vlido para este IP, que aponte para o mesmo IP.");
                    if (blocked) {
                        buildText(builder,
                                "O <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> deve estar sob seu prprio domnio para que a liberao seja efetivada.");
                    } else {
                        buildText(builder,
                                "Qualquer IP com <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> invlido pode ser bloqueado a qualquer momento.");
                    }
                } else {
                    if (blocked) {
                        buildText(builder,
                                "This IP has been blocked because have none valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.");
                    } else if (good) {
                        buildText(builder,
                                "This IP has a very good reputation, but does not have a <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else {
                        buildText(builder,
                                "This IP isn't blocked but have none valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.");
                    }
                    if (generic) {
                        buildText(builder,
                                "<a target=\"_blank\" href=\"http://spfbl.net/en/generic/\"Generic rDNS</a> will not be accepted.");
                    }
                    buildText(builder,
                            "Register a valid <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> for this IP, which points to the same IP.");
                    if (blocked) {
                        buildText(builder,
                                "The <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> must be registered under your own domain for us to be able to delist your system.");
                    } else {
                        buildText(builder,
                                "Any IP with invalid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a> can be blocked at any time.");
                    }
                }
            } else if (Block.containsCIDR(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP foi bloqueado, porm a reputao dele no est mais ruim.");
                } else {
                    buildText(builder, "This IP has been blocked, but it's reputation is not bad anymore.");
                }
                builder.append("      <hr>\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Para que a chave de desbloqueio possa ser enviada, selecione o endereo de e-mail do responsvel pelo IP:");
                } else {
                    buildText(builder,
                            "For the delist key can be sent, select the e-mail address responsible for this IP:");
                }
                builder.append("      <form method=\"POST\">\n");
                builder.append("        <ul>\n");
                int permittedCount = 0;
                for (String email : emailSet) {
                    if (Domain.isValidEmail(email)) {
                        if (!Trap.contaisAnything(email)) {
                            if (!NoReply.contains(email, false)) {
                                permittedCount++;
                            }
                        }
                    }
                }
                boolean permittedChecked = false;
                String email = emailSet.pollFirst();
                do {
                    if (!Domain.isValidEmail(email)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("invlido.<br>\n");
                        } else {
                            builder.append("invalid.<br>\n");
                        }
                    } else if (Trap.contaisAnything(email)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("inexistente.</li><br>\n");
                        } else {
                            builder.append("non-existent.</li><br>\n");
                        }
                    } else if (NoReply.contains(email, false)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("no permitido.<br>\n");
                        } else {
                            builder.append("not permitted.<br>\n");
                        }
                    } else {
                        builder.append("          <input type=\"radio\" name=\"identifier\" ");
                        builder.append(
                                "onclick=\"document.getElementById('btngo').disabled = false;\" value=\"");
                        builder.append(email);
                        if (permittedChecked) {
                            builder.append("\">");
                        } else if (permittedCount == 1) {
                            builder.append("\" checked>");
                            permittedChecked = true;
                        } else {
                            builder.append("\">");
                        }
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("permitido.<br>\n");
                        } else {
                            builder.append("permitted.<br>\n");
                        }
                    }
                } while ((email = emailSet.pollFirst()) != null);
                builder.append("        </ul>\n");
                if (permittedCount == 0) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder, "Nenhum e-mail do responsvel pelo IP  permitido neste sistema.");
                    } else {
                        buildText(builder,
                                "None of the responsible for IP has e-mail permitted under this system.");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "O <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> do IP deve estar sob seu prprio domnio. No aceitamos <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> com domnios de terceiros.");
                    } else {
                        buildText(builder,
                                "The <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> must be registered under your own domain. We do not accept <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> with third-party domains.");
                    }
                    builder.append("        <div id=\"divcaptcha\">\n");
                    if (Core.hasRecaptchaKeys()) {
                        String recaptchaKeySite = Core.getRecaptchaKeySite();
                        String recaptchaKeySecret = Core.getRecaptchaKeySecret();
                        ReCaptcha captcha = ReCaptchaFactory.newReCaptcha(recaptchaKeySite, recaptchaKeySecret,
                                false);
                        builder.append("        ");
                        builder.append(captcha.createRecaptchaHtml(null, null).replace("\r", ""));
                        // novo reCAPCHA
                        //            builder.append("      <div class=\"g-recaptcha\" data-sitekey=\"");
                        //            builder.append(recaptchaKeySite);
                        //            builder.append("\"></div>\n");
                    }
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        builder.append(
                                "          <input id=\"btngo\" type=\"submit\" value=\"Solicitar chave de delist\"");
                    } else {
                        builder.append(
                                "          <input id=\"btngo\" type=\"submit\" value=\"Request delist key\"");
                    }
                    if (permittedCount == 1) {
                        builder.append(">\n");
                    } else {
                        builder.append(" disabled>\n");
                    }
                    builder.append("        </div>\n");
                    builder.append("      </form>\n");
                }
            } else if (SPF.isGood(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est com reputao extremamente boa e por isso foi colocado em lista branca.");
                } else {
                    buildText(builder,
                            "This IP is extremely good reputation and therefore has been whitelisted.");
                }
            } else if (Ignore.containsCIDR(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est marcado como servio essencial e por isso foi colocado em lista branca.");
                } else {
                    buildText(builder,
                            "This IP is marked as an essential service and therefore has been whitelisted.");
                }
            } else if (White.containsIP(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est marcado como servio de mensagem estritamente corporativo e por isso foi colocado em lista branca.");
                    if (Core.hasAbuseEmail()) {
                        buildText(builder,
                                "Se voc tiver recebido alguma mensagem promocional deste IP, sem prvia autorizao, faa uma denuncia para "
                                        + Core.getAbuseEmail() + ".");
                    }
                } else {
                    buildText(builder,
                            "This IP is marked as strictly corporate message service and therefore has been whitelisted.");
                    if (Core.hasAbuseEmail()) {
                        buildText(builder,
                                "If you received any promotional message from this IP, without permission, make a complaint to "
                                        + Core.getAbuseEmail() + ".");
                    }
                }
            } else if (Block.containsHREF(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este IP est bloqueado para uso de URL.");
                    buildText(builder,
                            "Para que este IP seja removido desta lista,  necessrio enviar uma solicitao para "
                                    + Core.getAdminEmail() + ".");
                } else {
                    buildText(builder, "This IP is blocked for URL usage.");
                    buildText(builder, "In order to remove this IP from this list, you must send a request to "
                            + Core.getAdminEmail() + ".");
                }
            } else {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Nenhum bloqueio foi encontrado para este IP.");
                    buildText(builder,
                            "Se este IP estiver sendo rejeitado por algum MTA, aguarde a propagao de DNS deste servio.");
                    buildText(builder, "O tempo de propagao pode levar alguns dias.");
                } else {
                    buildText(builder, "No block was found for this IP.");
                    buildText(builder,
                            "If this IP is being rejected by some MTA, wait for the DNS propagation of this service.");
                    buildText(builder, "The propagation time can take a few days.");
                }
            }
        }
    } else if (Domain.isHostname(query)) {
        Distribution distribution;
        String domain = Domain.normalizeHostname(query, true);
        if (domain.equals(".test") || domain.equals(".invalid")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este  um domnio reservado e por este motivo no  abordado nesta lista.");
            } else {
                buildText(builder,
                        "This is a reserved domain and for this reason is not addressed in this list.");
            }
        } else if (Generic.containsDynamic(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este hostname est listado por referncia a um padro de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "Se voc discorda que se trata de hostname com padro de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>, entre em contato conosco atravs do e-mail "
                                    + Core.getAdminEmail() + ".");
                }
            } else {
                buildText(builder,
                        "This hostname is listed by reference to a <a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> pattern.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "If you disagree that this is hostname with <a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> pattern, contact us by email "
                                    + Core.getAdminEmail() + ".");
                }
            }
        } else if ((distribution = SPF.getDistribution(domain, true)).isNotGreen(domain)) {
            float probability = distribution.getSpamProbability(domain);
            boolean blocked = Block.containsDomain(domain, false);
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio " + (blocked ? "foi bloqueado" : "est listado")
                                + " por m reputao com " + Core.PERCENT_FORMAT.format(probability)
                                + " de pontos negativos do volume total de envio.");
                buildText(builder,
                        "Para que este domnio possa ser removido desta lista,  necessrio que todos os MTAs de origem reduzam o volume de envios para os destinatrios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a> na camada SMTP.");
            } else {
                buildText(builder, "This domain " + (blocked ? "was blocked" : "is listed")
                        + " by poor reputation in " + Core.PERCENT_FORMAT.format(probability)
                        + " of negative points of total sending the recipients with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a> at SMTP layer.");
            }
        } else if (Block.containsDomain(domain, true)) {
            if (Reverse.isInexistentDomain(domain)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este domnio est listado por no existir oficialmente.");
                } else {
                    buildText(builder, "This domain is listed because it does not exist officially.");
                }
            } else {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este domnio est listado por bloqueio manual.");
                    if (Core.hasAdminEmail()) {
                        buildText(builder,
                                "Para que este domnio seja removido desta lista,  necessrio enviar uma solicitao para "
                                        + Core.getAdminEmail() + ".");
                    }
                } else {
                    buildText(builder, "This domain is listed by manual block.");
                    if (Core.hasAdminEmail()) {
                        buildText(builder,
                                "In order to remove this domain from this list, you must send a request to "
                                        + Core.getAdminEmail() + ".");
                    }
                }
            }
        } else if (SPF.isGood(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est com reputao extremamente boa e por isso foi colocado em lista branca.");
            } else {
                buildText(builder,
                        "This domain is extremely good reputation and therefore has been whitelisted.");
            }
        } else if (Ignore.containsHost(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est marcado como servio essencial e por isso foi colocado em lista branca.");
            } else {
                buildText(builder,
                        "This domain is marked as an essential service and therefore has been whitelisted.");
            }
        } else if (White.containsDomain(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est marcado como servio de mensagem estritamente corporativo e por isso foi colocado em lista branca.");
                if (Core.hasAbuseEmail()) {
                    buildText(builder,
                            "Se voc tiver recebido alguma mensagem promocional deste domnio, sem prvia autorizao, faa uma denuncia para "
                                    + Core.getAbuseEmail() + ".");
                }
            } else {
                buildText(builder,
                        "This domain is marked as strictly corporate message service and therefore has been whitelisted.");
                if (Core.hasAbuseEmail()) {
                    buildText(builder,
                            "If you received any promotional message from this domain, without permission, make a complaint to "
                                    + Core.getAbuseEmail() + ".");
                }
            }
        } else if (Block.containsHREF(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Este domnio est bloqueado para uso em URL.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "Para que este domnio seja removido desta lista,  necessrio enviar uma solicitao para "
                                    + Core.getAdminEmail() + ".");
                }
            } else {
                buildText(builder, "This domain is blocked for URL usage.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "In order to remove this domain from this list, you must send a request to "
                                    + Core.getAdminEmail() + ".");
                }
            }
        } else {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Nenhum bloqueio foi encontrado para este domnio.");
            } else {
                buildText(builder, "No block was found for this domain.");
            }
        }
    }
    buildFooter(builder, locale);
    builder.append("    </div>\n");
    builder.append("  </body>\n");
    builder.append("</html>\n");
    return builder.toString();
}

From source file:com.hichinaschool.flashcards.anki.DeckPicker.java

private void updateDecksList(TreeSet<Object[]> decks, int eta, int count) {
    if (decks == null) {
        Log.e(AnkiDroidApp.TAG, "updateDecksList: empty decks list");
        return;//from  w w w  .  jav a  2 s. co  m
    }

    ArrayList<String> deckTitles = new ArrayList<String>();
    String currentName = null;
    Object[] defaultDeck = null;
    for (Object[] d : decks) {
        currentName = readableDeckName(((String[]) d[0]));
        if (!currentName.equals("Default"))
            deckTitles.add(currentName);
        else
            defaultDeck = d;
    }

    decks.remove(defaultDeck);

    if (!deckTitles.contains("HSK 1 Vocabulary")) {
        String[] strings = { "HSK 1 Vocabulary" };
        decks.add(new Object[] { strings, Long.valueOf(-10), 0, 0, 0, false,
                "https://s3.amazonaws.com/s3.hichinaschool.com.br/HSK+1+Vocabulary.apkg" });
    }

    if (!deckTitles.contains("HSK 2 Vocabulary")) {
        String[] strings = { "HSK 2 Vocabulary" };
        decks.add(new Object[] { strings, Long.valueOf(-20), 0, 0, 0, false,
                "https://s3.amazonaws.com/s3.hichinaschool.com.br/HSK+2+Vocabulary.apkg" });
    }

    if (!deckTitles.contains("HSK 3 Vocabulary")) {
        String[] strings = { "HSK 3 Vocabulary" };
        decks.add(new Object[] { strings, Long.valueOf(-30), 0, 0, 0, false,
                "https://s3.amazonaws.com/s3.hichinaschool.com.br/HSK+3+Vocabulary.apkg" });
    }

    if (!deckTitles.contains("HSK 4 Vocabulary")) {
        String[] strings = { "HSK 4 Vocabulary" };
        decks.add(new Object[] { strings, Long.valueOf(-40), 0, 0, 0, false,
                "https://s3.amazonaws.com/s3.hichinaschool.com.br/HSK+4+Vocabulary.apkg" });
    }

    if (!deckTitles.contains("HSK 5 Vocabulary")) {
        String[] strings = { "HSK 5 Vocabulary" };
        decks.add(new Object[] { strings, Long.valueOf(-50), 0, 0, 0, false,
                "https://s3.amazonaws.com/s3.hichinaschool.com.br/HSK+5+Vocabulary.apkg" });
    }

    mDeckList.clear();
    int due = 0;
    for (Object[] d : decks) {
        HashMap<String, String> m = new HashMap<String, String>();
        String[] name = ((String[]) d[0]);
        m.put("name", readableDeckName(name));
        m.put("did", ((Long) d[1]).toString());
        m.put("new", ((Integer) d[2]).toString());
        m.put("lrn", ((Integer) d[3]).toString());
        m.put("rev", ((Integer) d[4]).toString());
        m.put("dyn", ((Boolean) d[5]) ? "d1" : "d0");
        if (d.length > 6)
            m.put("url", ((String) d[6].toString()));
        // m.put("complMat", ((Float)d[5]).toString());
        // m.put("complAll", ((Float)d[6]).toString());
        if (name.length == 1) {
            due += Integer.parseInt(m.get("new")) + Integer.parseInt(m.get("lrn"))
                    + Integer.parseInt(m.get("rev"));
            // top position
            m.put("sep", "top");
            // correct previous deck
            if (mDeckList.size() > 0) {
                HashMap<String, String> map = mDeckList.get(mDeckList.size() - 1);
                if (map.get("sep").equals("top")) {
                    map.put("sep", "ful");
                } else {
                    map.put("sep", "bot");
                }
            }
        } else {
            // center position
            m.put("sep", "cen");
        }
        if (mDeckList.size() > 0 && mDeckList.size() == decks.size() - 1) {
            // bottom position
            if (name.length == 1) {
                m.put("sep", "ful");
            } else {
                m.put("sep", "bot");
            }
        }
        mDeckList.add(m);
    }

    mDeckListAdapter.notifyDataSetChanged();

    // set title
    Resources res = getResources();
    //        if (count != -1) {
    //            String time = "-";
    //            if (eta != -1) {
    //                time = res.getQuantityString(R.plurals.deckpicker_title_minutes, eta, eta);
    //            }
    //            AnkiDroidApp.getCompat().setSubtitle(this, res.getQuantityString(R.plurals.deckpicker_title, due, due, count, time));
    //        }
    setTitle(res.getString(R.string.app_name));

    // update widget
    WidgetStatus.update(this, decks);
}

From source file:org.wso2.carbon.service.mgt.ServiceGroupAdmin.java

/**
 * List all the available service groups
 *
 * @param pageNumber The number of the page to be retrieved
 * @return The service group metadata//w  w  w.  ja  v  a 2 s. c  o  m
 * @throws org.apache.axis2.AxisFault If an error occurs while retrieving service groups
 */
public ServiceGroupMetaDataWrapper listServiceGroups(String serviceTypeFilter, String serviceGroupSearchString,
        int pageNumber) throws AxisFault {
    if (serviceTypeFilter == null) {
        serviceTypeFilter = "ALL";
    }
    if (pageNumber < 0 || pageNumber == Integer.MAX_VALUE) {
        pageNumber = 0;
    }
    List<ServiceGroupMetaData> sgList = new ArrayList<ServiceGroupMetaData>();
    TreeSet<String> serviceTypes = new TreeSet<String>();
    serviceTypes.add("axis2");

    List<AxisServiceGroup> axisServiceGroupList = new ArrayList<AxisServiceGroup>();

    for (Iterator sgs = getAxisConfig().getServiceGroups(); sgs.hasNext();) {
        AxisServiceGroup serviceGroup = (AxisServiceGroup) sgs.next();
        // Filtering the admin services
        if (SystemFilter.isFilteredOutService(serviceGroup)) {
            continue; // No advancement of currentIndex
        }
        String serviceType = "axis2";
        Parameter serviceTypeParam;
        if (serviceGroup.getServices().hasNext()) {
            serviceTypeParam = (serviceGroup.getServices().next()).getParameter(ServerConstants.SERVICE_TYPE);
            if (serviceTypeParam != null) {
                serviceType = (String) serviceTypeParam.getValue();
                serviceTypes.add(serviceType);
            }
        }
        if (!serviceTypeFilter.equals("ALL")) {
            if (!serviceTypeFilter.equals(serviceType)) {
                continue;
            }
        }
        if (serviceGroupSearchString != null && serviceGroupSearchString.trim().length() > 0 && !serviceGroup
                .getServiceGroupName().toLowerCase().contains(serviceGroupSearchString.toLowerCase())) {
            continue;
        }
        boolean isClientSide = false;
        int noOfServices = 0;
        for (Iterator serviceIter = serviceGroup.getServices(); serviceIter.hasNext();) {
            AxisService axisService = (AxisService) serviceIter.next();
            if (axisService.isClientSide()) {
                isClientSide = true;
                break;
            }
            noOfServices++;
        }
        if (noOfServices == 0 || isClientSide) {
            continue; // No advancement of currentIndex
        }
        axisServiceGroupList.add(serviceGroup);
    }

    if (axisServiceGroupList.size() > 0) {
        Collections.sort(axisServiceGroupList, new Comparator<AxisServiceGroup>() {
            public int compare(AxisServiceGroup arg0, AxisServiceGroup arg1) {
                return arg0.getServiceGroupName().compareToIgnoreCase(arg1.getServiceGroupName());
            }
        });
    }
    //
    //        String itemsPerPage = ServerConfiguration.getInstance().getFirstProperty("ItemsPerPage");
    //        int itemsPerPageInt = 10; // the default number of item per page
    //        if (itemsPerPage != null) {
    //            itemsPerPageInt = Integer.parseInt(itemsPerPage);
    //        }
    //        int startIndex = pageNumber * itemsPerPageInt;
    //        int endIndex = (pageNumber + 1) * itemsPerPageInt;

    List<AxisServiceGroup> axisServiceGroupsRequiredForPage = new ArrayList<AxisServiceGroup>();
    //        for (int i = startIndex; i < endIndex && i < axisServiceGroupList.size(); i++) {
    //            axisServiceGroupsRequiredForPage.add(axisServiceGroupList.get(i));
    //        }
    for (AxisServiceGroup anAxisServiceGroupList : axisServiceGroupList) {
        axisServiceGroupsRequiredForPage.add(anAxisServiceGroupList);
    }
    for (AxisServiceGroup serviceGroup : axisServiceGroupsRequiredForPage) {
        String serviceType = "axis2";
        Parameter serviceTypeParam;
        if (serviceGroup.getServices().hasNext()) {
            serviceTypeParam = (serviceGroup.getServices().next()).getParameter(ServerConstants.SERVICE_TYPE);
            if (serviceTypeParam != null) {
                serviceType = (String) serviceTypeParam.getValue();
                serviceTypes.add(serviceType);
            }
        }

        ServiceGroupMetaData sgMetaData = new ServiceGroupMetaData();

        List<ServiceMetaData> services = new ArrayList<ServiceMetaData>();
        for (Iterator serviceIter = serviceGroup.getServices(); serviceIter.hasNext();) {
            AxisService axisService = (AxisService) serviceIter.next();

            ServiceMetaData service = new ServiceMetaData();
            String serviceName = axisService.getName();
            service.setName(serviceName);

            // extract service type
            serviceTypeParam = axisService.getParameter(ServerConstants.SERVICE_TYPE);
            if (serviceTypeParam != null) {
                serviceType = (String) serviceTypeParam.getValue();
            }
            service.setServiceType(serviceType);
            AxisConfiguration axisConfiguration = getAxisConfig();
            service.setWsdlURLs(Utils.getWsdlInformation(serviceName, axisConfiguration));
            service.setTryitURL(Utils.getTryitURL(serviceName, getConfigContext()));
            service.setActive(axisService.isActive());
            Parameter parameter = axisService.getParameter(ServiceAdmin.DISABLE_TRY_IT_PARAM);
            if (parameter != null && Boolean.TRUE.toString().equalsIgnoreCase((String) parameter.getValue())) {
                service.setDisableTryit(true);
            }
            parameter = axisService.getParameter(ServiceAdmin.DISABLE_DELETION_PARAM);
            if (parameter != null && Boolean.TRUE.toString().equalsIgnoreCase((String) parameter.getValue())) {
                sgMetaData.setDisableDeletion(true);
            }
            services.add(service);
        }

        String sgName = serviceGroup.getServiceGroupName();
        sgMetaData.setServices(services.toArray(new ServiceMetaData[services.size()]));
        sgMetaData.setServiceGroupName(sgName);
        sgMetaData.setServiceContextPath(getConfigContext().getServiceContextPath());
        Parameter parameter = serviceGroup.getParameter(Constants.Configuration.ENABLE_MTOM);
        if (parameter != null) {
            sgMetaData.setMtomStatus((String) parameter.getValue());
        } else {
            sgMetaData.setMtomStatus("false");
        }
        sgList.add(sgMetaData);
    }

    ServiceGroupMetaDataWrapper wrapper;
    wrapper = new ServiceGroupMetaDataWrapper();
    wrapper.setNumberOfCorrectServiceGroups(sgList.size());
    wrapper.setNumberOfFaultyServiceGroups(getAxisConfig().getFaultyServices().size());
    wrapper.setServiceTypes(serviceTypes.toArray(new String[serviceTypes.size()]));
    try {
        wrapper.setNumberOfActiveServices(new ServiceAdmin(getAxisConfig()).getNumberOfActiveServices());
    } catch (Exception e) {
        throw new AxisFault("Cannot get active services from ServiceAdmin", e);
    }
    //   DataPaginator.doPaging(pageNumber, axisServiceGroupList, sgList, wrapper);
    DataPaginator.doPaging(pageNumber, sgList, wrapper);
    return wrapper;
}