Example usage for com.google.common.collect TreeMultimap create

List of usage examples for com.google.common.collect TreeMultimap create

Introduction

In this page you can find the example usage for com.google.common.collect TreeMultimap create.

Prototype

public static <K extends Comparable, V extends Comparable> TreeMultimap<K, V> create() 

Source Link

Document

Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.

Usage

From source file:com.thesmartweb.swebrank.ElasticGetWordList.java

/**
 * Method gets all the top N max words for each topic of all the documents with their IDs (of the documents) passed as input.
 * @param ids It contains all the ids for which the words are going to be captured
 * @param top It contains the number of max words to be returned
 * @return All the words in a List/*w  ww  .  j a  v  a2 s .c om*/
 */
public List<String> getMaxWords(List<String> ids, int top, String config_path) {
    try {
        ReadInput ri = new ReadInput();
        List<String> elasticIndexes = ri.GetKeyFile(config_path, "elasticSearchIndexes");
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "lshrankldacluster")
                .build();
        Client client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
        //Node node = nodeBuilder().client(true).clusterName("lshrankldacluster").node();
        //Client client = node.client();
        List<String> MaxwordList = new ArrayList<>();
        HashMap<String, Double> wordsMap = new HashMap<>();
        SortedSetMultimap<Double, String> wordsMultisorted = TreeMultimap.create();
        for (String id : ids) {//for every id loop
            SearchResponse responseSearch = client.prepareSearch(elasticIndexes.get(2))
                    .setSearchType(SearchType.QUERY_AND_FETCH).setQuery(QueryBuilders.idsQuery().ids(id))
                    .execute().actionGet();//search for this id
            //----build an object with the response
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            responseSearch.toXContent(builder, ToXContent.EMPTY_PARAMS);
            builder.endObject();
            String JSONresponse = builder.string();
            //----parse the JSON response
            JsonParser parser = new JsonParser();
            JsonObject JSONobject = (JsonObject) parser.parse(JSONresponse);
            JsonObject hitsJsonObject = JSONobject.getAsJsonObject("hits");
            JsonArray hitsJsonArray = hitsJsonObject.getAsJsonArray("hits");
            //get all the JSON hits (check ElasticSearch typical response format for more)
            for (JsonElement hitJsonElement : hitsJsonArray) {
                JsonObject jsonElementObj = hitJsonElement.getAsJsonObject();
                jsonElementObj = jsonElementObj.getAsJsonObject("_source");
                JsonArray TopicsArray = jsonElementObj.getAsJsonArray("TopicsWordMap");//get the topics word map (every word has a probability
                for (JsonElement Topic : TopicsArray) {//for every topic I get the word with the max score
                    JsonObject TopicObj = Topic.getAsJsonObject();
                    JsonObject wordsmap = TopicObj.getAsJsonObject("wordsmap");//get the wordmap
                    Set<Map.Entry<String, JsonElement>> entrySet = wordsmap.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
                    double max = 0.0;
                    String maxword = "";
                    while (iterator.hasNext()) {
                        Map.Entry<String, JsonElement> next = iterator.next();
                        if (next.getValue().getAsDouble() > max) {
                            maxword = next.getKey();
                            max = next.getValue().getAsDouble();
                        }
                    }
                    if (wordsMap.containsKey(maxword)) {
                        if (wordsMap.get(maxword) < max) {
                            wordsMap.put(maxword, max);
                        }
                    } else {
                        wordsMap.put(maxword, max);
                    }
                }
            }
        }
        //we are going to sort all the max words
        Map<String, Double> wordsMapsorted = new HashMap<>();
        wordsMapsorted = sortByValue(wordsMap);//sorts the map in ascending fashion
        Iterator<Entry<String, Double>> iterator = wordsMapsorted.entrySet().iterator();
        //we are going to get the first top words from the list of Max words
        int beginindex = 0;
        //===we find the beginning index
        if (wordsMapsorted.entrySet().size() > top) {
            beginindex = wordsMapsorted.entrySet().size() - top;
        }
        int index = 0;
        //if the beginning index is larger we try to find the element
        while (index < beginindex) {
            iterator.next();
            index++;
        }
        //while the maxword list size is smaller than the top number and we have an extra value, add this word
        while (MaxwordList.size() < top && iterator.hasNext()) {
            String word = iterator.next().getKey();
            MaxwordList.add(word);

        }
        client.close();
        //node.close();
        return MaxwordList;
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        List<String> MaxwordList = new ArrayList<>();
        return MaxwordList;
    }

}

From source file:org.opennms.web.controller.admin.thresholds.ThresholdController.java

private void addStandardEditingBits(ModelAndView modelAndView) {
    Map<String, String> dsTypes = new LinkedHashMap<String, String>();
    dsTypes.put("node", "Node");
    dsTypes.put("if", "Interface"); // "interface" is a wrong word

    Collection<OnmsResourceType> resourceTypes = m_resourceDao.getResourceTypes();
    Multimap<String, String> genericDsTypes = TreeMultimap.create();
    for (OnmsResourceType resourceType : resourceTypes) {
        if (resourceType instanceof GenericIndexResourceType)
        // Put these in by label to sort them, we'll get them out in a moment
        {//from w w w  .jav  a 2  s.  com
            genericDsTypes.put(resourceType.getLabel(), resourceType.getName());
        }
    }

    // Now get the resource types out of the TreeMultimap
    for (String rtLabel : genericDsTypes.keys()) {
        Collection<String> rtNames = genericDsTypes.get(rtLabel);
        for (String rtName : rtNames) {
            if (rtNames.size() > 1) {
                dsTypes.put(rtName, rtLabel + " [" + rtName + "]");
            } else {
                dsTypes.put(rtName, rtLabel);
            }
        }
    }

    // Finally, set the sorted resource types into the model
    modelAndView.addObject("dsTypes", dsTypes);

    Collection<String> thresholdTypes = new ArrayList<String>();
    thresholdTypes.add("high");
    thresholdTypes.add("low");
    thresholdTypes.add("relativeChange");
    thresholdTypes.add("absoluteChange");
    thresholdTypes.add("rearmingAbsoluteChange");
    modelAndView.addObject("thresholdTypes", thresholdTypes);

    Collection<String> filterOperators = new ArrayList<String>();
    filterOperators.add("and");
    filterOperators.add("or");
    modelAndView.addObject("filterOperators", filterOperators);

    modelAndView.addObject("saveButtonTitle", SAVE_BUTTON_TITLE);
    modelAndView.addObject("cancelButtonTitle", CANCEL_BUTTON_TITLE);
    modelAndView.addObject("addFilterButtonTitle", ADDFILTER_BUTTON_TITLE);
    modelAndView.addObject("editButtonTitle", EDIT_BUTTON_TITLE);
    modelAndView.addObject("deleteButtonTitle", DELETE_BUTTON_TITLE);
    modelAndView.addObject("updateButtonTitle", UPDATE_BUTTON_TITLE);
    modelAndView.addObject("moveUpButtonTitle", MOVEUP_BUTTON_TITLE);
    modelAndView.addObject("moveDownButtonTitle", MOVEDOWN_BUTTON_TITLE);
}

From source file:ome.services.graphs.GraphPathReport.java

/**
 * Process the Hibernate domain object model and write a report of the mapped objects.
 * @throws IOException if there was a problem in writing to the output file
 */// w  w w.j av  a 2s . c  o  m
private static void report() throws IOException {
    /* note all the direct superclasses and subclasses */
    final Map<String, String> superclasses = new HashMap<String, String>();
    final SortedSetMultimap<String, String> subclasses = TreeMultimap.create();
    @SuppressWarnings("unchecked")
    final Map<String, ClassMetadata> classesMetadata = sessionFactory.getAllClassMetadata();
    for (final String className : classesMetadata.keySet()) {
        try {
            final Class<?> actualClass = Class.forName(className);
            if (IObject.class.isAssignableFrom(actualClass)) {
                @SuppressWarnings("unchecked")
                final Set<String> subclassNames = sessionFactory.getEntityPersister(className)
                        .getEntityMetamodel().getSubclassEntityNames();
                for (final String subclassName : subclassNames) {
                    if (!subclassName.equals(className)) {
                        final Class<?> actualSubclass = Class.forName(subclassName);
                        if (actualSubclass.getSuperclass() == actualClass) {
                            superclasses.put(subclassName, className);
                            subclasses.put(getSimpleName(className), getSimpleName(subclassName));
                        }
                    }
                }
            } else {
                System.err.println("error: mapped class " + className + " is not a " + IObject.class.getName());
            }
        } catch (ClassNotFoundException e) {
            System.err.println("error: could not instantiate class: " + e);
        }
    }
    /* queue for processing all the properties of all the mapped entities: name, type, nullability */
    final Queue<PropertyDetails> propertyQueue = new LinkedList<PropertyDetails>();
    final Map<String, Set<String>> allPropertyNames = new HashMap<String, Set<String>>();
    for (final Map.Entry<String, ClassMetadata> classMetadata : classesMetadata.entrySet()) {
        final String className = classMetadata.getKey();
        final ClassMetadata metadata = classMetadata.getValue();
        final String[] propertyNames = metadata.getPropertyNames();
        final Type[] propertyTypes = metadata.getPropertyTypes();
        final boolean[] propertyNullabilities = metadata.getPropertyNullability();
        for (int i = 0; i < propertyNames.length; i++) {
            if (!ignoreProperty(propertyNames[i])) {
                final List<String> propertyPath = Collections.singletonList(propertyNames[i]);
                propertyQueue.add(new PropertyDetails(className, propertyPath, propertyTypes[i],
                        propertyNullabilities[i]));
            }
        }
        final Set<String> propertyNamesSet = new HashSet<String>(propertyNames.length);
        propertyNamesSet.addAll(Arrays.asList(propertyNames));
        allPropertyNames.put(className, propertyNamesSet);
    }
    /* for linkedBy, X -> Y, Z: class X is linked to by class Y with Y's property Z */
    final SetMultimap<String, Map.Entry<String, String>> linkedBy = HashMultimap.create();
    final SetMultimap<String, String> linkers = HashMultimap.create();
    final SortedMap<String, SortedMap<String, String>> classPropertyReports = new TreeMap<String, SortedMap<String, String>>();
    /* process each property to note entity linkages */
    while (!propertyQueue.isEmpty()) {
        final PropertyDetails property = propertyQueue.remove();
        /* if the property has a component type, queue the parts for processing */
        if (property.type instanceof ComponentType) {
            final ComponentType componentType = (ComponentType) property.type;
            final String[] componentPropertyNames = componentType.getPropertyNames();
            final Type[] componentPropertyTypes = componentType.getSubtypes();
            final boolean[] componentPropertyNullabilities = componentType.getPropertyNullability();
            for (int i = 0; i < componentPropertyNames.length; i++) {
                if (!ignoreProperty(componentPropertyNames[i])) {
                    final List<String> componentPropertyPath = new ArrayList<String>(property.path.size() + 1);
                    componentPropertyPath.addAll(property.path);
                    componentPropertyPath.add(componentPropertyNames[i]);
                    propertyQueue.add(new PropertyDetails(property.holder, componentPropertyPath,
                            componentPropertyTypes[i], componentPropertyNullabilities[i]));
                }
            }
        } else {
            /* determine if this property links to another entity */
            final boolean isAssociatedEntity;
            if (property.type instanceof CollectionType) {
                final CollectionType ct = (CollectionType) property.type;
                isAssociatedEntity = sessionFactory.getCollectionPersister(ct.getRole()).getElementType()
                        .isEntityType();
            } else {
                isAssociatedEntity = property.type instanceof AssociationType;
            }
            /* determine the class and property name for reporting */
            final String holderSimpleName = getSimpleName(property.holder);
            final String propertyPath = Joiner.on('.').join(property.path);
            /* build a report line for this property */
            final StringBuffer sb = new StringBuffer();
            final String valueClassName;
            if (isAssociatedEntity) {
                /* entity linkages by non-inherited properties are recorded */
                final String valueName = ((AssociationType) property.type)
                        .getAssociatedEntityName(sessionFactory);
                final String valueSimpleName = getSimpleName(valueName);
                final Map.Entry<String, String> classPropertyName = Maps.immutableEntry(holderSimpleName,
                        propertyPath);
                linkers.put(holderSimpleName, propertyPath);
                linkedBy.put(valueSimpleName, classPropertyName);
                valueClassName = linkTo(valueSimpleName);
            } else {
                /* find a Sphinx representation for this property value type */
                final UserType userType;
                if (property.type instanceof CustomType) {
                    userType = ((CustomType) property.type).getUserType();
                } else {
                    userType = null;
                }
                if (property.type instanceof EnumType) {
                    valueClassName = "enumeration";
                } else if (userType instanceof GenericEnumType) {
                    @SuppressWarnings("unchecked")
                    final Class<? extends Unit> unitQuantityClass = ((GenericEnumType) userType)
                            .getQuantityClass();
                    valueClassName = "enumeration of " + linkToJavadoc(unitQuantityClass.getName());
                } else if (property.type instanceof ListType || userType instanceof ListAsSQLArrayUserType) {
                    valueClassName = "list";
                } else if (property.type instanceof MapType) {
                    valueClassName = "map";
                } else {
                    valueClassName = "``" + property.type.getName() + "``";
                }
            }
            sb.append(valueClassName);
            if (property.type.isCollectionType()) {
                sb.append(" (multiple)");
            } else if (property.isNullable) {
                sb.append(" (optional)");
            }
            /* determine from which class the property is inherited, if at all */
            String superclassWithProperty = null;
            String currentClass = property.holder;
            while (true) {
                currentClass = superclasses.get(currentClass);
                if (currentClass == null) {
                    break;
                } else if (allPropertyNames.get(currentClass).contains(property.path.get(0))) {
                    superclassWithProperty = currentClass;
                }
            }
            /* check if the property actually comes from an interface */
            final String declaringClassName = superclassWithProperty == null ? property.holder
                    : superclassWithProperty;
            final Class<? extends IObject> interfaceForProperty = getInterfaceForProperty(declaringClassName,
                    property.path.get(0));
            /* report where the property is declared */
            if (superclassWithProperty != null) {
                sb.append(" from ");
                sb.append(linkTo(getSimpleName(superclassWithProperty)));
            } else {
                if (interfaceForProperty != null) {
                    sb.append(", see ");
                    sb.append(linkToJavadoc(interfaceForProperty.getName()));
                }
            }
            SortedMap<String, String> byProperty = classPropertyReports.get(holderSimpleName);
            if (byProperty == null) {
                byProperty = new TreeMap<String, String>();
                classPropertyReports.put(holderSimpleName, byProperty);
            }
            byProperty.put(propertyPath, sb.toString());
        }
    }
    /* the information is gathered, now write the report */
    out.write("Glossary of all OMERO Model Objects\n");
    out.write("===================================\n\n");
    out.write("Overview\n");
    out.write("--------\n\n");
    out.write("Reference\n");
    out.write("---------\n\n");
    for (final Map.Entry<String, SortedMap<String, String>> byClass : classPropertyReports.entrySet()) {
        /* label the class heading */
        final String className = byClass.getKey();
        out.write(".. _" + labelFor(className) + ":\n\n");
        out.write(className + "\n");
        final char[] underline = new char[className.length()];
        for (int i = 0; i < underline.length; i++) {
            underline[i] = '"';
        }
        out.write(underline);
        out.write("\n\n");
        /* note the class' relationships */
        final SortedSet<String> superclassOf = new TreeSet<String>();
        for (final String subclass : subclasses.get(className)) {
            superclassOf.add(linkTo(subclass));
        }
        final SortedSet<String> linkerText = new TreeSet<String>();
        for (final Map.Entry<String, String> linker : linkedBy.get(className)) {
            linkerText.add(linkTo(linker.getKey(), linker.getValue()));
        }
        if (!(superclassOf.isEmpty() && linkerText.isEmpty())) {
            /* write the class' relationships */
            /*
            out.write("Relationships\n");
            out.write("^^^^^^^^^^^^^\n\n");
            */
            if (!superclassOf.isEmpty()) {
                out.write("Subclasses: " + Joiner.on(", ").join(superclassOf) + "\n\n");
            }
            if (!linkerText.isEmpty()) {
                out.write("Used by: " + Joiner.on(", ").join(linkerText) + "\n\n");
            }
        }
        /* write the class' properties */
        /*
        out.write("Properties\n");
        out.write("^^^^^^^^^^\n\n");
        */
        out.write("Properties:\n");
        for (final Map.Entry<String, String> byProperty : byClass.getValue().entrySet()) {
            final String propertyName = byProperty.getKey();
            // if (linkers.containsEntry(className, propertyName)) {
            //     /* label properties that have other entities as values */
            //     out.write(".. _" + labelFor(className, propertyName) + ":\n\n");
            // }
            out.write("  | " + propertyName + ": " + byProperty.getValue() + "\n" /* \n */);
        }
        out.write("\n");
    }
}

From source file:com.spotify.heroic.test.FakeValueProvider.java

private Object lookupParameterized(final ParameterizedType type, final boolean secondary) {
    if (type.getRawType().equals(Optional.class)) {
        final Object inner = lookup(type.getActualTypeArguments()[0], secondary);
        return Optional.of(inner);
    }/*w  w w .  ja v a2s.  c  o  m*/

    if (type.getRawType().equals(List.class)) {
        if (secondary) {
            return ImmutableList.of();
        }

        final Object a = lookup(type.getActualTypeArguments()[0], !secondary);
        return ImmutableList.of(a);
    }

    if (type.getRawType().equals(Set.class)) {
        if (secondary) {
            return ImmutableSet.of();
        }

        final Object a = lookup(type.getActualTypeArguments()[0], !secondary);
        return ImmutableSet.of(a);
    }

    if (type.getRawType().equals(SortedSet.class)) {
        if (secondary) {
            return ImmutableSortedSet.of();
        }

        final Object a = lookup(type.getActualTypeArguments()[0], !secondary);
        return ImmutableSortedSet.of((Comparable) a);
    }

    if (type.getRawType().equals(Map.class)) {
        if (secondary) {
            return ImmutableMap.of();
        }

        final Object keya = lookup(type.getActualTypeArguments()[0], !secondary);
        final Object a = lookup(type.getActualTypeArguments()[1], !secondary);
        return ImmutableMap.of(keya, a);
    }

    if (type.getRawType().equals(Multimap.class)) {
        final TreeMultimap<Comparable<?>, Comparable<?>> mm = TreeMultimap.create();

        if (secondary) {
            return mm;
        }

        final Object keya = lookup(type.getActualTypeArguments()[0], !secondary);
        final Object a = lookup(type.getActualTypeArguments()[1], !secondary);
        mm.put((Comparable<?>) keya, (Comparable<?>) a);
        return mm;
    }

    throw new IllegalArgumentException("Lookup for type (" + type + ") failed");
}

From source file:com.data2semantics.yasgui.client.tab.optionbar.LinkCreator.java

private TreeMultimap<String, String> getQueryArgs() {
    TreeMultimap<String, String> args = TreeMultimap.create();
    args.put(SettingKeys.QUERY_STRING, view.getSelectedTabSettings().getQueryString());
    args.put(SettingKeys.ENDPOINT, view.getSelectedTabSettings().getEndpoint());
    args.put(SettingKeys.OUTPUT_FORMAT, view.getSelectedTabSettings().getOutputFormat());
    args.put(SettingKeys.TAB_TITLE, view.getSelectedTabSettings().getTabTitle());
    args.put(SettingKeys.CONTENT_TYPE_SELECT, view.getSelectedTabSettings().getSelectContentType());
    args.put(SettingKeys.CONTENT_TYPE_CONSTRUCT, view.getSelectedTabSettings().getConstructContentType());
    args.put(SettingKeys.REQUEST_METHOD, view.getSelectedTabSettings().getRequestMethod());
    args.putAll(view.getSelectedTabSettings().getCustomQueryArgs());
    return args;/*from w  ww . java  2s .  c  om*/
}

From source file:com.android.repository.impl.meta.RepositoryPackages.java

private static <P extends RepoPackage> Multimap<String, P> computePackagePrefixes(
        Map<String, ? extends P> packages) {
    Multimap<String, P> packagesByPrefix = TreeMultimap.create();
    for (Map.Entry<String, ? extends P> entry : packages.entrySet()) {
        String key = entry.getKey();
        while (true) {
            packagesByPrefix.put(key, entry.getValue());
            int endIndex = key.lastIndexOf(RepoPackage.PATH_SEPARATOR);
            if (endIndex < 0) {
                break;
            }// w  w  w .  j  ava 2s .c  om
            key = key.substring(0, endIndex);
        }
    }
    return packagesByPrefix;
}

From source file:com.twitter.crunch.StableRdfMapping.java

private void buildRDFMapping(Node datacenter, Map<Node, List<Node>> mapping) throws MappingException {
    final List<Node> allNodes = datacenter.getAllLeafNodes();

    initializeCapcity(allNodes, mapping);

    // Generate candidates
    Map<Node, List<Node>> candidateMap = new TreeMap<Node, List<Node>>();
    for (Node node : mapping.keySet()) {
        for (Node candidate : mapping.keySet()) {
            if (mapping.get(candidate).size() >= this.rdfMax)
                continue;

            if (candidate != node && rules.acceptReplica(node, candidate)
                    && !mapping.get(node).contains(candidate)) {
                List<Node> nodeList = candidateMap.get(node);
                if (nodeList == null) {
                    nodeList = new ArrayList<Node>();
                    candidateMap.put(node, nodeList);
                }/*from w  w w  .ja va  2 s . co m*/
                nodeList.add(candidate);
            }
        }
    }

    TreeMultimap<Integer, Node> nodeReplicaSizeMap = TreeMultimap.create();
    for (Node node : mapping.keySet()) {
        nodeReplicaSizeMap.put(mapping.get(node).size(), node);
    }

    while (true) {
        // Pick node with least number of replicas
        Node candidate = null;
        Node minNode = null;
        int min = this.rdfMax;

        for (Map.Entry<Integer, Node> entry : nodeReplicaSizeMap.entries()) {
            int replicaSize = entry.getKey();
            Node node = entry.getValue();

            // break at this point since it sorted
            if (replicaSize >= this.rdfMax)
                break;

            Node findResult = findCandidate(node, candidateMap, nodeReplicaSizeMap, mapping);
            if (findResult != null) {
                candidate = findResult;
                minNode = node;
                min = replicaSize;
                break;
            }
        }

        if (minNode == null || mapping.get(minNode).size() >= this.rdfMin)
            break;

        // Pair the candidate up
        mapping.get(candidate).add(minNode);
        mapping.get(minNode).add(candidate);
        candidateMap.get(minNode).remove(candidate);
        candidateMap.get(candidate).remove(minNode);

        nodeReplicaSizeMap.remove(min, minNode);
        nodeReplicaSizeMap.put(min + 1, minNode);
        int candidateSize = mapping.get(candidate).size();
        nodeReplicaSizeMap.remove(candidateSize - 1, candidate);
        nodeReplicaSizeMap.put(candidateSize, candidate);

        updateReplicaUsage(mapping, minNode);

        logger.info("Added {} for {}", candidate.getName(), minNode.getName());
    }
}

From source file:com.zimbra.cs.mailbox.ContactGroup.java

public void derefAllMembers(Mailbox mbox, OperationContext octxt, SoapProtocol proxyProtocol) {
    derefedMembers = TreeMultimap.create();

    for (Member member : members) {
        member.derefMember(mbox, octxt, proxyProtocol);
        if (member.derefed()) {
            String key = member.getDerefedKey();
            derefedMembers.put(key, member);
        } else {/* w w w. j  a  v a  2 s.c  o  m*/
            ZimbraLog.contact.debug("contact group member cannot be derefed: " + member.getValue());
            derefedMembers.put(member.getValue(), member);
        }
    }
}

From source file:org.lobid.lodmill.hadoop.NTriplesToJsonLd.java

private SortedSetMultimap<String, String> groupByIndexCollection() {
    final SortedSetMultimap<String, String> indices = TreeMultimap.create();
    for (String index : client.admin().indices().prepareStats().execute().actionGet().getIndices().keySet()) {
        final String[] nameAndTimestamp = index.split("-(?=\\d)");
        indices.put(nameAndTimestamp[0], index);
    }/*from ww  w. j av a  2 s.c  o  m*/
    return indices;
}

From source file:com.ikanow.infinit.e.harvest.extraction.document.HarvestStatus_Integrated.java

/**
 * getLogMessages/*ww w .j  a  v a 2 s .co m*/
 * Returns a list of up to 5 errors (eg encountered when parsing JavaScript) for 
 * a source, sorted by frequency in ascending order
 * @return
 */
private StringBuffer getLogMessages(boolean bReset) {
    if ((null != _messages) && (_messages.size() > 0)) {
        StringBuffer messagesString = new StringBuffer();

        // Create multimap to store errors in, reverse the order of key (error message) and
        // value (count) to sort on error count
        Multimap<Integer, String> mm = TreeMultimap.create();
        for (java.util.Map.Entry<String, Integer> entry : _messages.entrySet()) {
            StringBuffer msg = new StringBuffer(entry.getKey()).append(" (Occurences: ")
                    .append(entry.getValue()).append(')');
            mm.put(-entry.getValue(), msg.toString());
        }

        // Write the error messages to a Collection<String>
        Collection<String> messages = mm.values();

        // Append up to the top five messages to our StringBuffer and return
        int messageCount = 1;
        for (String s : messages) {
            if (messageCount > 1) {
                messagesString.append('\n');
            }
            messagesString.append(s);
            messageCount++;
            if (messageCount > 5)
                break;
        }
        if (bReset) {
            _messages.clear();
        }
        return messagesString;
    } else {
        return null;
    }
}