List of usage examples for com.google.common.collect Multiset entrySet
Set<Entry<E>> entrySet();
From source file:tufts.vue.ds.Field.java
/** @return the count of all unique values in the Multiset */ private static int count(Multiset m) { // to fulfill the java.util.Collection contract, Multiset.size() returns the *virtual* // count of items in the set, not the unqiue items as a counting HashMap impl would do -- // we have to actually pull the entrySet/elementSet and count that to get the count of // unique values. Forunately, the impl appears to cache the entrySet, so it's not creating // a new one each time. (The elementSet is also cached, tho in the current google impl, the // entrySet has to do a tad less delegation to extract the backingMap size) return m == null ? 0 : m.entrySet().size(); //return m == null ? 0 : m.elementSet().size(); }
From source file:org.sakaiproject.nakamura.meservice.LiteAbstractMyGroupsServlet.java
private void writeFacetFields(List<ValueMap> filteredProfiles, ExtendedJSONWriter writer) throws JSONException { Multiset<String> tags = HashMultiset.create(); for (ValueMap profile : filteredProfiles) { Object profileTags = profile.get("sakai:tags"); if (profileTags != null && profileTags instanceof String[]) { Collections.addAll(tags, (String[]) profileTags); }//from w ww . j av a 2 s .c om } // sort the tags in descending order of their occurrence List<Multiset.Entry<String>> sortedTags = Lists.newArrayList(tags.entrySet()); Collections.sort(sortedTags, new Comparator<Multiset.Entry<String>>() { @Override public int compare(Multiset.Entry<String> a, Multiset.Entry<String> b) { return Ints.compare(b.getCount(), a.getCount()); } }); // write out the tag names and their counts writer.key("facet_fields"); writer.array(); writer.object(); writer.key("tagname"); writer.array(); for (Multiset.Entry<String> tag : sortedTags) { writer.object(); writer.key(tag.getElement()); writer.value(tag.getCount()); writer.endObject(); } writer.endArray(); writer.endObject(); writer.endArray(); }
From source file:com.seniorproject.semanticweb.services.WebServices.java
public String prepareResultWithCount(ArrayList<String> in) throws IOException { Multiset<String> multiset = HashMultiset.create(); for (int i = 0; i < in.size(); i++) { if (in.get(i).length() > 0 && !in.get(i).equals("\"\"")) { multiset.add(in.get(i));/*from w w w .j av a 2 s.c o m*/ } } JsonArrayBuilder out = Json.createArrayBuilder(); JsonObjectBuilder resultObject = Json.createObjectBuilder(); for (Multiset.Entry<String> entry : multiset.entrySet()) { List<String> matchList = new ArrayList<>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); Matcher regexMatcher = regex.matcher(entry.getElement()); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); } resultObject.add("elem", matchList.get(0)); resultObject.add("count", entry.getCount()); if (matchList.size() >= 2) { String label = ""; for (int j = 1; j < matchList.size(); j++) { label += matchList.get(j); } resultObject.add("label", label); } else { resultObject.add("label", ""); } out.add(resultObject); } return out.build().toString(); }
From source file:com.sun.tools.hat.internal.server.FinalizerSummaryQuery.java
private void printFinalizerSummary(Collection<? extends JavaHeapObject> objs) { int count = 0; Multiset<JavaClass> bag = HashMultiset.create(); for (JavaHeapObject obj : objs) { count++;// w ww .j a va 2s . c o m bag.add(obj.getClazz()); } out.println("<p align='center'>"); out.println("<b>"); out.println("Total "); if (count != 0) { out.print("<a href='/finalizerObjects/'>instances</a>"); } else { out.print("instances"); } out.println(" pending finalization: "); out.print(count); out.println("</b></p><hr>"); if (count == 0) { return; } // calculate and print histogram out.println("<table border=1 align=center>"); out.println("<tr><th>Count</th><th>Class</th></tr>"); bag.entrySet().stream().sorted(Ordering.natural().reverse().onResultOf(entry -> entry.getCount())) .forEach(entry -> { out.println("<tr><td>"); out.println(entry.getCount()); out.println("</td><td>"); printClass(entry.getElement()); out.println("</td><tr>"); }); out.println("</table>"); }
From source file:org.sonar.java.checks.FieldMatchMethodNameCheck.java
@Override public void visitNode(Tree tree) { Symbol.TypeSymbol classSymbol = ((ClassTree) tree).symbol(); if (classSymbol != null) { Map<String, Symbol> indexSymbol = Maps.newHashMap(); Multiset<String> fields = HashMultiset.create(); Map<String, String> fieldsOriginal = Maps.newHashMap(); Set<String> methodNames = Sets.newHashSet(); Collection<Symbol> symbols = classSymbol.memberSymbols(); for (Symbol sym : symbols) { String symName = sym.name().toLowerCase(); if (sym.isVariableSymbol()) { indexSymbol.put(symName, sym); fields.add(symName);/*w w w. j av a 2 s . c o m*/ fieldsOriginal.put(symName, sym.name()); } if (sym.isMethodSymbol()) { methodNames.add(symName); } } fields.addAll(methodNames); for (Multiset.Entry<String> entry : fields.entrySet()) { if (entry.getCount() > 1) { Tree field = indexSymbol.get(entry.getElement()).declaration(); if (field != null) { addIssue(field, "Rename the \"" + fieldsOriginal.get(entry.getElement()) + "\" member."); } } } } }
From source file:io.janusproject.util.MultisetView.java
@Override public boolean equals(Object obj) { if (obj == this) { return true; }//w w w.ja v a2s .c om if (obj instanceof Multiset) { Multiset<?> that = (Multiset<?>) obj; // We can't simply check whether the entry sets are equal, since that // approach fails when a TreeMultiset has a comparator that returns 0 // when passed unequal elements. if (size() != that.size() || entrySet().size() != that.entrySet().size()) { return false; } for (Entry<?> entry : that.entrySet()) { if (count(entry.getElement()) != entry.getCount()) { return false; } } return true; } return false; }
From source file:org.sonar.java.checks.naming.FieldMatchMethodNameCheck.java
@Override public void visitNode(Tree tree) { Symbol.TypeSymbol classSymbol = ((ClassTree) tree).symbol(); if (classSymbol != null) { Map<String, Symbol> indexSymbol = Maps.newHashMap(); Multiset<String> fields = HashMultiset.create(); Map<String, String> fieldsOriginal = Maps.newHashMap(); Set<String> methodNames = Sets.newHashSet(); Collection<Symbol> symbols = classSymbol.memberSymbols(); for (Symbol sym : symbols) { String symName = sym.name().toLowerCase(Locale.US); if (sym.isVariableSymbol()) { indexSymbol.put(symName, sym); fields.add(symName);//from w w w . j a v a 2 s. c o m fieldsOriginal.put(symName, sym.name()); } if (sym.isMethodSymbol()) { methodNames.add(symName); } } fields.addAll(methodNames); for (Multiset.Entry<String> entry : fields.entrySet()) { if (entry.getCount() > 1) { Tree field = indexSymbol.get(entry.getElement()).declaration(); if (field != null) { reportIssue(((VariableTree) field).simpleName(), "Rename the \"" + fieldsOriginal.get(entry.getElement()) + "\" member."); } } } } }
From source file:org.summer.dsl.model.types.util.TypeConformanceComputer.java
/** * Keeps the cumulated distance for all the common raw super types of the given references. * Interfaces that are more directly implemented will get a lower total count than more general * interfaces./*w ww . j a va 2 s .c o m*/ */ protected void cumulateDistance(final List<JvmTypeReference> references, Multimap<JvmType, JvmTypeReference> all, Multiset<JvmType> cumulatedDistance) { for (JvmTypeReference other : references) { Multiset<JvmType> otherDistance = LinkedHashMultiset.create(); initializeDistance(other, all, otherDistance); cumulatedDistance.retainAll(otherDistance); for (Multiset.Entry<JvmType> typeToDistance : otherDistance.entrySet()) { if (cumulatedDistance.contains(typeToDistance.getElement())) cumulatedDistance.add(typeToDistance.getElement(), typeToDistance.getCount()); } } }
From source file:org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2.java
private SortedSet<OWLIndividual> computeNegativeExamples(OWLClass classToDescribe, Multiset<OWLClass> positiveExamplesTypes, Map<Strategy, Double> strategiesWithWeight, int maxNrOfReturnedInstances) { SortedSet<OWLIndividual> negativeExamples = new TreeSet<>(); for (Entry<Strategy, Double> entry : strategiesWithWeight.entrySet()) { Strategy strategy = entry.getKey(); Double weight = entry.getValue(); // the max number of instances returned by the current strategy int strategyLimit = (int) (weight * maxNrOfReturnedInstances); // the highest frequency value int maxFrequency = positiveExamplesTypes.entrySet().iterator().next().getCount(); if (strategy == SIBLING) {//get sibling class based examples negativeExamples.addAll(negativeExamplesBySiblingClasses(positiveExamplesTypes, strategyLimit, maxNrOfReturnedInstances)); } else if (strategy == SUPERCLASS) {//get super class based examples negativeExamples.addAll(negativeExamplesBySuperClasses(positiveExamplesTypes, negativeExamples, strategyLimit, maxNrOfReturnedInstances)); } else if (strategy == RANDOM) {//get some random examples logger.info("Applying random strategy..."); SortedSet<OWLIndividual> randomNegativeExamples = new TreeSet<>(); String query = "SELECT DISTINCT ?s WHERE {?s a ?type. ?type a owl:Class ."; if (classToDescribe != null) { query += "FILTER NOT EXISTS{?s a <" + classToDescribe.toStringID() + "> }"; } else { for (OWLClass nc : positiveExamplesTypes.elementSet()) { }//from w w w. j a v a 2s .c o m throw new UnsupportedOperationException( "Currently it's not possible to get random examples for unknown class to describe."); } query += "} LIMIT " + maxNrOfReturnedInstances; try (QueryExecution qe = qef.createQueryExecution(query)) { ResultSet rs = qe.execSelect(); while (rs.hasNext()) { QuerySolution qs = rs.next(); randomNegativeExamples .add(df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI()))); } } randomNegativeExamples.removeAll(negativeExamples); negativeExamples.addAll(new ArrayList<>(randomNegativeExamples).subList(0, Math .min(randomNegativeExamples.size(), maxNrOfReturnedInstances - negativeExamples.size()))); logger.info("Negative examples(" + randomNegativeExamples.size() + "): " + randomNegativeExamples); } } return negativeExamples; }
From source file:com.koloboke.compile.KolobokeMapBackedMultiset.java
@Override public final boolean equals(Object obj) { if (this == obj) { return true; }//from www . j a va2s . c o m if (obj instanceof Multiset) { Multiset<?> that = (Multiset<?>) obj; /* * We can't simply check whether the entry sets are equal, since that * approach fails when a TreeMultiset has a comparator that returns 0 * when passed unequal elements. */ if (this.size() != that.size() || this.entrySet().size() != that.entrySet().size()) { return false; } for (Entry<?> entry : that.entrySet()) { if (this.count(entry.getElement()) != entry.getCount()) { return false; } } return true; } return false; }