List of usage examples for com.google.common.collect ImmutableListMultimap builder
public static <K, V> Builder<K, V> builder()
From source file:com.proofpoint.http.client.MediaType.java
private static MediaType create(String type, String subtype, Multimap<String, String> parameters) { checkNotNull(type);/*from www .ja v a 2 s . co m*/ checkNotNull(subtype); checkNotNull(parameters); String normalizedType = normalizeToken(type); String normalizedSubtype = normalizeToken(subtype); checkArgument(!WILDCARD.equals(normalizedType) || WILDCARD.equals(normalizedSubtype), "A wildcard type cannot be used with a non-wildcard subtype"); ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); for (Entry<String, String> entry : parameters.entries()) { String attribute = normalizeToken(entry.getKey()); builder.put(attribute, normalizeParameterValue(attribute, entry.getValue())); } MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build()); // Return one of the constants if the media type is a known type. return Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); }
From source file:com.proofpoint.http.client.MediaType.java
/** * Parses a media type from its string representation. * * @throws IllegalArgumentException if the input is not parsable *//* w w w . j a va 2 s. c om*/ public static MediaType parse(String input) { checkNotNull(input); Tokenizer tokenizer = new Tokenizer(input); try { String type = tokenizer.consumeToken(TOKEN_MATCHER); tokenizer.consumeCharacter('/'); String subtype = tokenizer.consumeToken(TOKEN_MATCHER); ImmutableListMultimap.Builder<String, String> parameters = ImmutableListMultimap.builder(); while (tokenizer.hasMore()) { tokenizer.consumeCharacter(';'); tokenizer.consumeToken(LINEAR_WHITE_SPACE); String attribute = tokenizer.consumeToken(TOKEN_MATCHER); tokenizer.consumeCharacter('='); final String value; if ('"' == tokenizer.previewChar()) { tokenizer.consumeCharacter('"'); StringBuilder valueBuilder = new StringBuilder(); while ('"' != tokenizer.previewChar()) { if ('\\' == tokenizer.previewChar()) { tokenizer.consumeCharacter('\\'); valueBuilder.append(tokenizer.consumeCharacter(ASCII)); } else { valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER)); } } value = valueBuilder.toString(); tokenizer.consumeCharacter('"'); } else { value = tokenizer.consumeToken(TOKEN_MATCHER); } parameters.put(attribute, value); } return create(type, subtype, parameters.build()); } catch (IllegalStateException e) { throw new IllegalArgumentException(e); } }
From source file:org.elasticsoftware.elasticactors.cluster.LocalActorSystemInstance.java
@Override public ActorRefGroup groupOf(Collection<ActorRef> members) throws IllegalArgumentException { // all members have to be persistent actor refs for (ActorRef member : members) { if (!(member instanceof ActorShardRef)) { throw new IllegalArgumentException( "Only Persistent Actors (annotated with @Actor) of the same ElasticActors cluster are allowed to form a group"); }//ww w. jav a 2 s .c om } // build the map ImmutableListMultimap.Builder<ActorShardRef, ActorRef> memberMap = ImmutableListMultimap.builder(); for (ActorRef member : members) { memberMap.put((ActorShardRef) ((ActorShardRef) member).getActorContainer().getActorRef(), member); } return new LocalActorRefGroup(memberMap.build()); }
From source file:org.ambraproject.wombat.controller.SearchController.java
/** * Examine the current @code{ArticleSearchQuery} object and build a single URL parameter * string to append to the current search URL. * * @param q the search query to rebuild search URL parameters from * @return ImmutableListMultimap that contains the URL parameter list *//*from ww w . j ava 2 s .c om*/ private static ImmutableListMultimap<String, String> rebuildUrlParameters(ArticleSearchQuery q) { Preconditions.checkArgument(!q.isForRawResults()); Preconditions.checkArgument(!q.getFacet().isPresent()); ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); builder.put(q.isSimple() ? "q" : "unformattedQuery", q.getQuery().orElse("")); int rows = q.getRows(); builder.put("resultsPerPage", Integer.toString(rows)); if (rows > 0) { int page = q.getStart() / rows + 1; builder.put("page", Integer.toString(page)); } builder.putAll("filterJournals", q.getJournalKeys()); builder.putAll("filterSubjects", q.getSubjects()); builder.putAll("filterAuthors", q.getAuthors()); builder.putAll("filterSections", q.getSections()); builder.putAll("filterArticleTypes", q.getArticleTypes()); builder.putAll("filterStartDate", q.getStartDate() == null ? "" : q.getStartDate()); builder.putAll("filterEndDate", q.getEndDate() == null ? "" : q.getEndDate()); // TODO: Support dateRange. Note this is different from startDate and endDate // TODO: Support sortOrder for (Map.Entry<String, String> entry : q.getRawParameters().entrySet()) { builder.put(entry); } return builder.build(); }
From source file:com.linecorp.armeria.common.MediaType.java
/** * <em>Replaces</em> all parameters with the given parameters. * * @throws IllegalArgumentException if any parameter or value is invalid *///from w w w. ja v a 2 s. c om public MediaType withParameters(Map<String, ? extends Iterable<String>> parameters) { final ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); for (Map.Entry<String, ? extends Iterable<String>> e : parameters.entrySet()) { final String k = e.getKey(); for (String v : e.getValue()) { builder.put(k, v); } } return create(type, subtype, builder.build()); }
From source file:com.facebook.presto.sql.planner.RelationPlanner.java
@Override protected RelationPlan visitUnion(Union node, Void context) { checkArgument(!node.getRelations().isEmpty(), "No relations specified for UNION"); List<Symbol> unionOutputSymbols = null; ImmutableList.Builder<PlanNode> sources = ImmutableList.builder(); ImmutableListMultimap.Builder<Symbol, Symbol> symbolMapping = ImmutableListMultimap.builder(); List<RelationPlan> subPlans = node.getRelations().stream() .map(relation -> processAndCoerceIfNecessary(relation, context)).collect(toImmutableList()); boolean hasSampleWeight = false; for (RelationPlan subPlan : subPlans) { if (subPlan.getSampleWeight().isPresent()) { hasSampleWeight = true;// w w w . j a va 2s . c o m break; } } Optional<Symbol> outputSampleWeight = Optional.empty(); for (RelationPlan relationPlan : subPlans) { if (hasSampleWeight && !relationPlan.getSampleWeight().isPresent()) { relationPlan = addConstantSampleWeight(relationPlan); } List<Symbol> childOutputSymbols = relationPlan.getOutputSymbols(); if (unionOutputSymbols == null) { // Use the first Relation to derive output symbol names RelationType descriptor = relationPlan.getDescriptor(); ImmutableList.Builder<Symbol> outputSymbolBuilder = ImmutableList.builder(); for (Field field : descriptor.getVisibleFields()) { int fieldIndex = descriptor.indexOf(field); Symbol symbol = childOutputSymbols.get(fieldIndex); outputSymbolBuilder.add( symbolAllocator.newSymbol(symbol.getName(), symbolAllocator.getTypes().get(symbol))); } unionOutputSymbols = outputSymbolBuilder.build(); outputSampleWeight = relationPlan.getSampleWeight(); } RelationType descriptor = relationPlan.getDescriptor(); checkArgument(descriptor.getVisibleFieldCount() == unionOutputSymbols.size(), "Expected relation to have %s symbols but has %s symbols", descriptor.getVisibleFieldCount(), unionOutputSymbols.size()); int unionFieldId = 0; for (Field field : descriptor.getVisibleFields()) { int fieldIndex = descriptor.indexOf(field); symbolMapping.put(unionOutputSymbols.get(unionFieldId), childOutputSymbols.get(fieldIndex)); unionFieldId++; } sources.add(relationPlan.getRoot()); } PlanNode planNode = new UnionNode(idAllocator.getNextId(), sources.build(), symbolMapping.build()); if (node.isDistinct()) { planNode = distinct(planNode); } return new RelationPlan(planNode, analysis.getOutputDescriptor(node), planNode.getOutputSymbols(), outputSampleWeight); }
From source file:com.linecorp.armeria.common.MediaType.java
/** * <em>Replaces</em> all parameters with the given attribute with a single parameter with the * given value. If multiple parameters with the same attributes are necessary use * {@link #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter * when using a {@link Charset} object./*ww w . j a va 2 s . c om*/ * * @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid */ public MediaType withParameter(String attribute, String value) { checkNotNull(attribute); checkNotNull(value); String normalizedAttribute = normalizeToken(attribute); Builder<String, String> builder = ImmutableListMultimap.builder(); for (Entry<String, String> entry : parameters.entries()) { String key = entry.getKey(); if (!normalizedAttribute.equals(key)) { builder.put(key, entry.getValue()); } } builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value)); MediaType mediaType = new MediaType(type, subtype, builder.build()); // Return one of the constants if the media type is a known type. return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); }
From source file:com.linecorp.armeria.common.MediaType.java
private static MediaType create(String type, String subtype, Multimap<String, String> parameters) { checkNotNull(type);// ww w .ja v a 2 s . com checkNotNull(subtype); checkNotNull(parameters); String normalizedType = normalizeToken(type); String normalizedSubtype = normalizeToken(subtype); checkArgument(!WILDCARD.equals(normalizedType) || WILDCARD.equals(normalizedSubtype), "A wildcard type cannot be used with a non-wildcard subtype"); Builder<String, String> builder = ImmutableListMultimap.builder(); for (Entry<String, String> entry : parameters.entries()) { String attribute = normalizeToken(entry.getKey()); builder.put(attribute, normalizeParameterValue(attribute, entry.getValue())); } MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build()); // Return one of the constants if the media type is a known type. return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); }
From source file:com.google.devtools.build.lib.analysis.RuleContext.java
/** * Returns the a prerequisites keyed by the CPU of their configurations. * If the split transition is not active (e.g. split() returned an empty * list), the key is an empty Optional.//from ww w .ja v a2s.c o m */ public Map<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> getSplitPrerequisites( String attributeName) { checkAttribute(attributeName, Mode.SPLIT); Attribute attributeDefinition = attributes().getAttributeDefinition(attributeName); @SuppressWarnings("unchecked") // Attribute.java doesn't have the BuildOptions symbol. SplitTransition<BuildOptions> transition = (SplitTransition<BuildOptions>) attributeDefinition .getSplitTransition(rule); List<ConfiguredTarget> deps = targetMap.get(attributeName); List<BuildOptions> splitOptions = transition.split(getConfiguration().getOptions()); if (splitOptions.isEmpty()) { // The split transition is not active. Defer the decision on which CPU to use. return ImmutableMap.of(Optional.<String>absent(), deps); } Set<String> cpus = new HashSet<>(); for (BuildOptions options : splitOptions) { // This method should only be called when the split config is enabled on the command line, in // which case this cpu can't be null. cpus.add(options.get(BuildConfiguration.Options.class).cpu); } // Use an ImmutableListMultimap.Builder here to preserve ordering. ImmutableListMultimap.Builder<Optional<String>, TransitiveInfoCollection> result = ImmutableListMultimap .builder(); for (TransitiveInfoCollection t : deps) { if (t.getConfiguration() != null) { result.put(Optional.of(t.getConfiguration().getCpu()), t); } else { // Source files don't have a configuration, so we add them to all architecture entries. for (String cpu : cpus) { result.put(Optional.of(cpu), t); } } } return Multimaps.asMap(result.build()); }
From source file:com.linecorp.armeria.common.MediaType.java
/** * Parses a media type from its string representation. * * @throws IllegalArgumentException if the input is not parsable *///from w ww. j a v a2 s.c om public static MediaType parse(String input) { checkNotNull(input); Tokenizer tokenizer = new Tokenizer(input); try { String type = tokenizer.consumeToken(TOKEN_MATCHER); tokenizer.consumeCharacter('/'); String subtype = tokenizer.consumeToken(TOKEN_MATCHER); Builder<String, String> parameters = ImmutableListMultimap.builder(); while (tokenizer.hasMore()) { tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); tokenizer.consumeCharacter(';'); tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); String attribute = tokenizer.consumeToken(TOKEN_MATCHER); tokenizer.consumeCharacter('='); final String value; if ('"' == tokenizer.previewChar()) { tokenizer.consumeCharacter('"'); StringBuilder valueBuilder = new StringBuilder(); while ('"' != tokenizer.previewChar()) { if ('\\' == tokenizer.previewChar()) { tokenizer.consumeCharacter('\\'); valueBuilder.append(tokenizer.consumeCharacter(ascii())); } else { valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER)); } } value = valueBuilder.toString(); tokenizer.consumeCharacter('"'); } else { value = tokenizer.consumeToken(TOKEN_MATCHER); } parameters.put(attribute, value); } return create(type, subtype, parameters.build()); } catch (IllegalStateException e) { throw new IllegalArgumentException("Could not parse '" + input + '\'', e); } }