List of usage examples for com.google.common.collect Multimap get
Collection<V> get(@Nullable K key);
From source file:com.zimbra.cs.db.DbBlobConsistency.java
public static void export(DbConnection conn, Mailbox mbox, String tableName, String idColName, Multimap<Integer, Integer> idRevs, String path) throws ServiceException { Set<Integer> mail_itemIds = new HashSet<Integer>(); Multimap<Integer, Integer> rev_itemIds = HashMultimap.create(); for (Integer itemId : idRevs.keySet()) { Collection<Integer> revs = idRevs.get(itemId); for (int rev : revs) { if (rev == 0) { mail_itemIds.add(itemId); } else { rev_itemIds.put(itemId, rev); }//from w w w .j a v a2s . c om } } PreparedStatement stmt = null; if (!(Db.getInstance() instanceof MySQL)) { throw ServiceException.INVALID_REQUEST("export is only supported for MySQL", null); } ZimbraLog.sqltrace.info("Exporting %d items in table %s to %s.", idRevs.size(), tableName, path); try { StringBuffer sql = new StringBuffer(); boolean revisionTable = tableName.startsWith(DbMailItem.TABLE_REVISION); sql.append("SELECT * FROM ").append(DbMailbox.qualifyTableName(mbox, tableName)).append(" WHERE ") .append(DbMailItem.IN_THIS_MAILBOX_AND); if (!revisionTable || mail_itemIds.size() > 0) { if (mail_itemIds.size() == 0) { sql.append(idColName).append(" in ('')"); } else { sql.append(DbUtil.whereIn(idColName, mail_itemIds.size())); } } if (revisionTable) { if (mail_itemIds.size() > 0 && rev_itemIds.size() > 0) { sql.append(" OR "); } if (rev_itemIds.size() > 0) { sql.append(DbUtil.whereIn(Db.getInstance().concat(idColName, "'-'", "version"), rev_itemIds.size())); } } sql.append(" INTO OUTFILE ?"); stmt = conn.prepareStatement(sql.toString()); int pos = 1; pos = DbMailItem.setMailboxId(stmt, mbox, pos); for (int itemId : mail_itemIds) { stmt.setInt(pos++, itemId); } if (revisionTable) { for (Integer itemId : rev_itemIds.keySet()) { Collection<Integer> revs = rev_itemIds.get(itemId); for (int rev : revs) { stmt.setString(pos++, itemId + "-" + rev); } } } stmt.setString(pos++, path); stmt.execute(); } catch (SQLException e) { throw ServiceException.FAILURE("exporting table " + tableName + " to " + path, e); } finally { DbPool.quietCloseStatement(stmt); } }
From source file:org.apache.crunch.lib.Quantiles.java
private static <V> Collection<Pair<Double, V>> findQuantiles(Iterator<V> sortedCollectionIterator, long collectionSize, List<Double> quantiles) { Collection<Pair<Double, V>> output = Lists.newArrayList(); Multimap<Long, Double> quantileIndices = ArrayListMultimap.create(); for (double quantile : quantiles) { long idx = Math.max((int) Math.ceil(quantile * collectionSize) - 1, 0); quantileIndices.put(idx, quantile); }/*w w w.j ava2 s . c om*/ long index = 0; while (sortedCollectionIterator.hasNext()) { V value = sortedCollectionIterator.next(); if (quantileIndices.containsKey(index)) { for (double quantile : quantileIndices.get(index)) { output.add(Pair.of(quantile, value)); } } index++; } return output; }
From source file:com.opengamma.strata.loader.csv.RatesCurvesCsvLoader.java
private static Map<LocalDate, Map<CurveName, Curve>> parseCurves(Predicate<LocalDate> datePredicate, CharSource settingsResource, Collection<CharSource> curvesResources) { // load curve settings Map<CurveName, LoadedCurveSettings> settingsMap = parseCurveSettings(settingsResource); // load curves, ensuring curves only be seen once within a date Map<LocalDate, Map<CurveName, Curve>> resultMap = new TreeMap<>(); for (CharSource curvesResource : curvesResources) { Multimap<LocalDate, Curve> fileCurvesByDate = parseSingle(datePredicate, curvesResource, settingsMap); // Ensure curve names are unique, with a good error message for (LocalDate date : fileCurvesByDate.keySet()) { Collection<Curve> fileCurves = fileCurvesByDate.get(date); Map<CurveName, Curve> resultCurves = resultMap.computeIfAbsent(date, d -> new HashMap<>()); for (Curve fileCurve : fileCurves) { if (resultCurves.put(fileCurve.getName(), fileCurve) != null) { throw new IllegalArgumentException( "Rates curve loader found multiple curves with the same name: " + fileCurve.getName()); }//from w w w . j a v a 2 s. co m } } } return resultMap; }
From source file:grakn.core.graql.executor.WriteExecutor.java
/** * <a href=https://en.wikipedia.org/wiki/Composition_of_relations>Compose</a> two Multimaps together, * treating them like many-to-many relations. *///from w w w. j a v a 2 s .c om private static Multimap<Writer, Writer> writerDependencies(Multimap<Writer, Variable> writerToVar, Multimap<Variable, Writer> varToWriter) { Multimap<Writer, Writer> dependency = HashMultimap.create(); for (Map.Entry<Writer, Variable> entry : writerToVar.entries()) { Writer dependant = entry.getKey(); Variable intermediateVar = entry.getValue(); for (Writer depended : varToWriter.get(intermediateVar)) { dependency.put(dependant, depended); } } return dependency; }
From source file:org.sosy_lab.cpachecker.cfa.postprocessing.global.FunctionCallUnwinder.java
/** checks, iff there is an call-stack from father to child. * In the graph this would be a way from father to child. * It should be more efficient to search backwards, * because children have only one father in most cases. */ private static boolean isFatherOf(final String child, final String possibleFather, final Multimap<String, String> reverseGraph) { final Set<String> finished = new HashSet<>(); final Deque<String> waitlist = new ArrayDeque<>(); waitlist.add(child);//from w ww.j a v a 2 s . c o m while (!waitlist.isEmpty()) { String current = waitlist.pop(); if (!finished.add(current)) { continue; } if (current.equals(possibleFather)) { return true; } waitlist.addAll(reverseGraph.get(current)); } return false; }
From source file:org.gradle.model.internal.manage.binding.DefaultStructBindingsStore.java
private static ModelType<?> determineManagedPropertyType(StructBindingExtractionContext<?> extractionContext, String propertyName, Multimap<PropertyAccessorType, StructMethodBinding> accessorBindings) { Collection<StructMethodBinding> isGetter = accessorBindings.get(IS_GETTER); for (StructMethodBinding isGetterBinding : isGetter) { if (!((ManagedPropertyMethodBinding) isGetterBinding).getDeclaredPropertyType() .equals(ModelType.of(Boolean.TYPE))) { WeaklyTypeReferencingMethod<?, ?> isGetterMethod = isGetterBinding.getViewMethod(); extractionContext.add(isGetterMethod, String.format("it should either return 'boolean', or its name should be '%s()'", "get" + isGetterMethod.getName().substring(2))); }/*from w w w . jav a 2 s . c o m*/ } Set<ModelType<?>> potentialPropertyTypes = Sets.newLinkedHashSet(); for (StructMethodBinding binding : accessorBindings.values()) { if (binding.getAccessorType() == SETTER) { continue; } ManagedPropertyMethodBinding propertyBinding = (ManagedPropertyMethodBinding) binding; potentialPropertyTypes.add(propertyBinding.getDeclaredPropertyType()); } Collection<ModelType<?>> convergingPropertyTypes = findConvergingTypes(potentialPropertyTypes); if (convergingPropertyTypes.size() != 1) { extractionContext.add(propertyName, String.format("it must have a consistent type, but it's defined as %s", Joiner.on(", ").join(ModelTypes.getDisplayNames(convergingPropertyTypes)))); return convergingPropertyTypes.iterator().next(); } ModelType<?> propertyType = Iterables.getOnlyElement(convergingPropertyTypes); for (StructMethodBinding setterBinding : accessorBindings.get(SETTER)) { ManagedPropertyMethodBinding propertySetterBinding = (ManagedPropertyMethodBinding) setterBinding; ModelType<?> declaredSetterType = propertySetterBinding.getDeclaredPropertyType(); if (!declaredSetterType.equals(propertyType)) { extractionContext.add(setterBinding.getViewMethod(), String.format("it should take parameter with type '%s'", propertyType.getDisplayName())); } } return propertyType; }
From source file:com.yahoo.yqlplus.engine.internal.java.sequences.Sequences.java
public static <ROW, LEFT, RIGHT, KEY> List<ROW> doJoin(boolean inner, List<LEFT> left, List<RIGHT> right, List<KEY> leftKeys, Function<RIGHT, KEY> rightKey, JoinEmit<ROW, LEFT, RIGHT> output) { List<ROW> result = Lists.newArrayList(); Multimap<KEY, RIGHT> rights = ArrayListMultimap.create(leftKeys.size(), 4); for (RIGHT row : right) { rights.put(rightKey.apply(row), row); }/* w ww . ja va 2s.c o m*/ for (int i = 0; i < left.size(); ++i) { LEFT leftRow = left.get(i); KEY leftKey = leftKeys.get(i); Collection<RIGHT> match = rights.get(leftKey); if (match == null || match.isEmpty()) { if (!inner) { ROW row = output.create(); output.setLeft(row, leftRow); result.add(row); } continue; } for (RIGHT rightRow : match) { ROW row = output.create(); output.setLeft(row, leftRow); output.setRight(row, rightRow); result.add(row); } } return result; }
From source file:com.bigdata.dastor.dht.BootStrapper.java
static Multimap<InetAddress, Range> getWorkMap(Multimap<Range, InetAddress> rangesWithSourceTarget, IFailureDetector failureDetector) { /*//w w w . java2 s . c om * Map whose key is the source node and the value is a map whose key is the * target and value is the list of ranges to be sent to it. */ Multimap<InetAddress, Range> sources = ArrayListMultimap.create(); // TODO look for contiguous ranges and map them to the same source for (Range range : rangesWithSourceTarget.keySet()) { for (InetAddress source : rangesWithSourceTarget.get(range)) { if (failureDetector.isAlive(source)) { sources.put(source, range); break; } } } return sources; }
From source file:es.usc.citius.composit.core.composition.search.CompositSearch.java
private static <E, T extends Comparable<T>> Set<Set<Operation<E>>> nodeEquivalence( Set<Set<Operation<E>>> successors, ServiceMatchNetwork<E, T> network) { // Group candidates if (successors.size() < 2) return successors; Multimap<Set<Set<Operation<E>>>, Set<Operation<E>>> mmap = HashMultimap.create(); for (Set<Operation<E>> group : successors) { Set<E> inputs = Operations.inputs(group); // Find providers Set<Set<Operation<E>>> successorGroup = new HashSet<Set<Operation<E>>>(); for (E input : inputs) { for (E source : network.getSourceElementsThatMatch(input).keySet()) { successorGroup.add(Sets.newHashSet(network.getOperationsWithOutput(source))); }// w w w. jav a 2 s. c o m } mmap.get(successorGroup).add(group); } // Select one from each equivalent group Set<Set<Operation<E>>> nonEquivalentGroups = new HashSet<Set<Operation<E>>>(); for (Collection<Set<Operation<E>>> group : mmap.asMap().values()) { nonEquivalentGroups.add(group.iterator().next()); } return nonEquivalentGroups; }
From source file:org.sonar.plugins.pitest.Mutant.java
public static String toJSON(List<Mutant> mutants) { Multimap<Integer, String> mutantsByLine = ArrayListMultimap.create(); for (Mutant mutant : mutants) { mutantsByLine.put(mutant.getLineNumber(), mutant.toJSON()); }//from www .ja v a 2s . c o m StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (int line : mutantsByLine.keySet()) { if (!first) { builder.append(","); } first = false; builder.append("\""); builder.append(line); builder.append("\":["); builder.append(Joiner.on(",").join(mutantsByLine.get(line))); builder.append("]"); } builder.append("}"); return builder.toString(); }