Beispiel #1
0
  public MediaType withParameter(String attribute, String value) {
    Preconditions.checkNotNull(attribute);
    Preconditions.checkNotNull(value);
    String normalizedAttribute = normalizeToken(attribute);
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    for (Map.Entry<String, String> entry : this.parameters.entries()) {
      String key = (String) entry.getKey();
      if (!normalizedAttribute.equals(key)) {
        builder.put(key, entry.getValue());
      }
    }
    builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
    MediaType mediaType = new MediaType(this.type, this.subtype, builder.build());

    return (MediaType) Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
  }
Beispiel #2
0
  public Multimap<String, String> headers() {
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();

    for (Header header : httpResponse.getAllHeaders()) {
      builder.put(header.getName(), header.getValue());
    }

    return builder.build();
  }
  public static <K, V> ImmutableListMultimap<K, V> groupBy(
      Iterable<? extends V> iterable, Transformer<? extends K, V> grouper) {
    ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder();

    for (V element : iterable) {
      K key = grouper.transform(element);
      builder.put(key, element);
    }

    return builder.build();
  }
Beispiel #4
0
  public RuleNames(Grammar grammar, boolean installAdapter) {
    this.contextGrammar = grammar;
    Adapter adapter = new Adapter(this);
    if (installAdapter) {
      installAdapterIfMissing(adapter, grammar);
    }
    List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
    ImmutableListMultimap.Builder<String, AbstractRule> simpleNameToRulesBuilder =
        ImmutableListMultimap.builder();
    ImmutableMap.Builder<String, AbstractRule> qualifiedNameToRuleBuilder = ImmutableMap.builder();
    ImmutableBiMap.Builder<String, AbstractRule> uniqueNameToRuleBuilder = ImmutableBiMap.builder();
    ImmutableBiMap.Builder<String, AbstractRule> antlrNameToRuleBuilder = ImmutableBiMap.builder();

    Map<String, AbstractRule> names = Maps.newHashMap();
    Set<String> usedAntlrNames = Sets.newHashSet();
    Set<String> usedUniqueNames = Sets.newHashSet();
    for (AbstractRule rule : allRules) {
      String name = rule.getName();
      simpleNameToRulesBuilder.put(name, rule);
      String qualifiedName = getQualifiedName(rule);
      qualifiedNameToRuleBuilder.put(qualifiedName, rule);
      String uniqueName = name;
      String antlrRuleName;
      if (names.containsKey(name)) {
        name = qualifiedName;
        uniqueName = getInheritedUniqueName(rule, usedUniqueNames);
        antlrRuleName = getInheritedAntlrRuleName(rule, usedAntlrNames);
      } else {
        antlrRuleName = getDefaultAntlrRuleName(rule);
      }
      names.put(name, rule);
      if (!usedUniqueNames.add(uniqueName)) {
        throw new IllegalStateException(uniqueName);
      }
      uniqueNameToRuleBuilder.put(uniqueName, rule);
      if (!usedAntlrNames.add(antlrRuleName)) {
        throw new IllegalStateException(antlrRuleName);
      }
      antlrNameToRuleBuilder.put(antlrRuleName, rule);
      if (installAdapter) {
        installAdapterIfMissing(adapter, rule);
      }
    }
    simpleNameToRules = simpleNameToRulesBuilder.build();
    qualifiedNameToRule = qualifiedNameToRuleBuilder.build();
    nameToRule = ImmutableBiMap.copyOf(names);
    uniqueNameToRule = uniqueNameToRuleBuilder.build();
    antlrNameToRule = antlrNameToRuleBuilder.build();
    this.allRules = ImmutableList.copyOf(allRules);
  }
Beispiel #5
0
 private LinkedResources(Iterable<LinkedResource> resources) {
   ImmutableList.Builder<LinkedResource> listBuilder = ImmutableList.builder();
   ImmutableListMultimap.Builder<String, LinkedResource> byPathBuilder =
       ImmutableListMultimap.builder();
   Map<String, LinkedResource> byIdMap = Maps.newHashMap();
   for (LinkedResource resource : resources) {
     listBuilder.add(resource);
     byPathBuilder.put(resource.getPath(), resource);
     if (resource.getId().isPresent()) byIdMap.put(resource.getId().get(), resource);
   }
   this.resources = listBuilder.build();
   this.resourcesByPath = byPathBuilder.build();
   this.resourcesById = ImmutableMap.copyOf(byIdMap);
 }
 // Here are the details of the matching. We track the current brace depth, and we artificially
 // consider that the whole file is at brace depth 1 inside a pseudo-class whose name is the
 // name of the package. When we see a class definition, we push the fully-qualified name of the
 // class on a stack so that we can associate abstract methods we find with the possibly-nested
 // class they belong to. A class definition must occur at brace depth one more than the class
 // containing it, which is equivalent to saying that the brace depth must be the same as the
 // class stack depth. This check excludes local class definitions within methods and
 // initializers. If we meet these constraints and we see the word "class" followed by an
 // identifier Foo, then we consider that we are entering the definition of class Foo. We determine
 // the fully-qualified name of Foo, which is container.Foo, where container is the current top of
 // the class stack (initially, the package name). We push this new fully-qualified name on the
 // class stack. We have not yet seen the left brace with the class definition so at this point the
 // class stack depth is one more than the brace depth. When we subsequently see a right brace that
 // takes us back to this situation then we know we have completed the definition of Foo and we can
 // pop it from the class stack.
 //
 // We check that the token after "class" is indeed an identifier to avoid confusion
 // with Foo.class. Even though the tokenizer does not distinguish between identifiers and
 // keywords, it is enough to exclude the single word "instanceof" because that is the only word
 // that can legally appear after Foo.class (though in a legal program the resultant expression
 // will always be true).
 //
 // Again, we are at the top level of a class when the brace depth is equal to the class stack
 // depth. If we then see the word "abstract" then that is the start either of an abstract class
 // definition or of an abstract method. We record that we have seen "abstract" and we cancel
 // that indication as soon as we see a left brace, to exclude the abstract class case, and also
 // the case of interfaces or @interfaces redundantly declared abstract. Now, when
 // we see an identifier that is preceded by an uncanceled "abstract" and followed by a left paren
 // then we have found an abstract method of the class on the top of the class stack. We record it
 // in the list of abstract methods of the class on the top of the class stack. We don't bother
 // checking that the method has no parameters, because an @AutoCursor class will cause a compiler
 // error if there are abstract methods with parameters, since the @AutoCursor processor doesn't
 // know how to implement them in the concrete subclass it generates.
 ImmutableListMultimap<String, String> abstractMethods(
     JavaTokenizer tokenizer, String packageName) {
   ImmutableListMultimap.Builder<String, String> abstractMethods = ImmutableListMultimap.builder();
   Deque<String> classStack = new ArrayDeque<String>();
   classStack.addLast(packageName);
   int braceDepth = 1;
   boolean sawAbstract = false;
   String className = null;
   for (String previousToken = "", token = tokenizer.nextToken();
       token != null;
       previousToken = token, token = tokenizer.nextToken()) {
     boolean topLevel = (braceDepth == classStack.size());
     if (className != null) {
       if (Character.isJavaIdentifierStart(className.charAt(0))
           && !className.equals("instanceof")) {
         String container = classStack.getLast();
         // container might be empty in the case of a packageless class
         classStack.add(container.isEmpty() ? className : container + "." + className);
       }
       className = null;
     }
     if (token.equals("{")) {
       braceDepth++;
       sawAbstract = false;
     } else if (token.equals("}")) {
       braceDepth--;
       if (topLevel) {
         classStack.removeLast();
       }
     } else if (topLevel) {
       if (token.equals("class") || token.equals("interface")) {
         className = tokenizer.nextToken();
       } else if (token.equals("abstract")) {
         sawAbstract = true;
       } else if (token.equals("(")) {
         if (sawAbstract && Character.isJavaIdentifierStart(previousToken.charAt(0))) {
           abstractMethods.put(classStack.getLast(), previousToken);
         }
         sawAbstract = false;
       }
     }
   }
   return abstractMethods.build();
 }
Beispiel #7
0
  private static MediaType create(
      String type, String subtype, Multimap<String, String> parameters) {
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(subtype);
    Preconditions.checkNotNull(parameters);
    String normalizedType = normalizeToken(type);
    String normalizedSubtype = normalizeToken(subtype);
    Preconditions.checkArgument(
        (!"*".equals(normalizedType)) || ("*".equals(normalizedSubtype)),
        "A wildcard type cannot be used with a non-wildcard subtype");

    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    for (Map.Entry<String, String> entry : parameters.entries()) {
      String attribute = normalizeToken((String) entry.getKey());
      builder.put(attribute, normalizeParameterValue(attribute, (String) entry.getValue()));
    }
    MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build());

    return (MediaType) Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
  }
Beispiel #8
0
 @Override
 public void apply(JsonArray value, Config.Builder builder) {
   ImmutableListMultimap.Builder<CustomPassExecutionTime, CompilerPassFactory> customPasses =
       ImmutableListMultimap.builder();
   Gson gson =
       new GsonBuilder()
           .registerTypeAdapter(
               CustomPassConfig.class, new CustomPassConfig.CustomPassConfigDeserializer())
           .create();
   for (JsonElement entry : value) {
     CustomPassConfig pass = gson.fromJson(entry, CustomPassConfig.class);
     Class<?> clazz;
     try {
       clazz = Class.forName(pass.getClassName());
     } catch (ClassNotFoundException e) {
       throw new RuntimeException(e);
     }
     CompilerPassFactory factory = new CompilerPassFactory(clazz);
     customPasses.put(pass.getWhen(), factory);
   }
   builder.setCustomPasses(customPasses.build());
 }
Beispiel #9
0
 public static MediaType parse(String input) {
   Preconditions.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.consumeTokenIfPresent(LINEAR_WHITE_SPACE);
       String attribute = tokenizer.consumeToken(TOKEN_MATCHER);
       tokenizer.consumeCharacter('=');
       String value;
       if ('"' == tokenizer.previewChar()) {
         tokenizer.consumeCharacter('"');
         StringBuilder valueBuilder = new StringBuilder();
         while ('"' != tokenizer.previewChar()) {
           if ('\\' == tokenizer.previewChar()) {
             tokenizer.consumeCharacter('\\');
             valueBuilder.append(tokenizer.consumeCharacter(CharMatcher.ASCII));
           } else {
             valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER));
           }
         }
         String 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);
   }
 }
Beispiel #10
0
 public Builder put(CellLabel cellLabel, Term content) {
   cellsBuilder.put(cellLabel, new Cell(cellLabel, content));
   return this;
 }
Beispiel #11
0
 public Builder add(Cell cell) {
   cellsBuilder.put(cell.cellLabel(), cell);
   return this;
 }
Beispiel #12
0
  @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;
        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
        TupleDescriptor 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();
      }

      TupleDescriptor 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);
  }