Exemplo n.º 1
0
    @Override
    public PlanNode visitUnion(UnionNode node, RewriteContext<Set<Symbol>> context) {
      // Find out which output symbols we need to keep
      ImmutableListMultimap.Builder<Symbol, Symbol> rewrittenSymbolMappingBuilder =
          ImmutableListMultimap.builder();
      for (Symbol symbol : node.getOutputSymbols()) {
        if (context.get().contains(symbol)) {
          rewrittenSymbolMappingBuilder.putAll(symbol, node.getSymbolMapping().get(symbol));
        }
      }
      ListMultimap<Symbol, Symbol> rewrittenSymbolMapping = rewrittenSymbolMappingBuilder.build();

      // Find the corresponding input symbol to the remaining output symbols and prune the subplans
      ImmutableList.Builder<PlanNode> rewrittenSubPlans = ImmutableList.builder();
      for (int i = 0; i < node.getSources().size(); i++) {
        ImmutableSet.Builder<Symbol> expectedInputSymbols = ImmutableSet.builder();
        for (Collection<Symbol> symbols : rewrittenSymbolMapping.asMap().values()) {
          expectedInputSymbols.add(Iterables.get(symbols, i));
        }
        rewrittenSubPlans.add(
            context.rewrite(node.getSources().get(i), expectedInputSymbols.build()));
      }

      return new UnionNode(
          node.getId(),
          rewrittenSubPlans.build(),
          rewrittenSymbolMapping,
          ImmutableList.copyOf(rewrittenSymbolMapping.keySet()));
    }
Exemplo n.º 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();
  }
Exemplo n.º 3
0
  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();
  }
Exemplo n.º 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);
  }
Exemplo n.º 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);
 }
Exemplo n.º 6
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);
  }
 // 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();
 }
Exemplo n.º 8
0
  public static void dumpRegistry(File minecraftDir) {
    if (customItemStacks == null) {
      return;
    }
    if (Boolean.valueOf(System.getProperty("fml.dumpRegistry", "false")).booleanValue()) {
      ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
      for (String modId : customItemStacks.rowKeySet()) {
        builder.putAll(modId, customItemStacks.row(modId).keySet());
      }

      File f = new File(minecraftDir, "itemStackRegistry.csv");
      MapJoiner mapJoiner = Joiner.on("\n").withKeyValueSeparator(",");
      try {
        Files.write(mapJoiner.join(builder.build().entries()), f, Charsets.UTF_8);
        FMLLog.log(Level.INFO, "Dumped item registry data to %s", f.getAbsolutePath());
      } catch (IOException e) {
        FMLLog.log(Level.ERROR, e, "Failed to write registry data to %s", f.getAbsolutePath());
      }
    }
  }
Exemplo n.º 9
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);
  }
Exemplo n.º 10
0
 public Builder concatenate(Term term) {
   if (term instanceof CellCollection) {
     CellCollection cellCollection = (CellCollection) term;
     cellsBuilder.putAll(cellCollection.cells);
     collectionVariablesBuilder.addAll(cellCollection.collectionVariables);
   } else if (term instanceof Variable && term.kind == Kind.CELL_COLLECTION) {
     collectionVariablesBuilder.add((Variable) term);
   } else {
     assert false : "unexpected concatenated term " + term;
   }
   return this;
 }
Exemplo n.º 11
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());
 }
Exemplo n.º 12
0
 public Term build() {
   ImmutableListMultimap<CellLabel, Cell> cells = cellsBuilder.build();
   ImmutableMultiset<Variable> collectionVariables = collectionVariablesBuilder.build();
   if (cells.isEmpty()) {
     switch (collectionVariables.size()) {
       case 1:
         return collectionVariables.iterator().next();
       default:
         return new CellCollection(cells, collectionVariables, cellSort, definition);
     }
   } else {
     return new CellCollection(cells, collectionVariables, cellSort, definition);
   }
 }
Exemplo n.º 13
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);
   }
 }
Exemplo n.º 14
0
 public Builder add(Cell cell) {
   cellsBuilder.put(cell.cellLabel(), cell);
   return this;
 }
Exemplo n.º 15
0
  private void doProcess(RoundEnvironment roundEnv) {
    for (Element element : roundEnv.getElementsAnnotatedWith(Provided.class)) {
      providedChecker.checkProvidedParameter(element);
    }

    ImmutableListMultimap.Builder<String, FactoryMethodDescriptor> indexedMethods =
        ImmutableListMultimap.builder();
    ImmutableSet.Builder<ImplementationMethodDescriptor> implementationMethodDescriptors =
        ImmutableSet.builder();
    for (Element element : roundEnv.getElementsAnnotatedWith(AutoFactory.class)) {
      Optional<AutoFactoryDeclaration> declaration = declarationFactory.createIfValid(element);
      if (declaration.isPresent()) {
        TypeElement extendingType = declaration.get().extendingType();
        List<ExecutableElement> supertypeMethods =
            ElementFilter.methodsIn(elements.getAllMembers(extendingType));
        for (ExecutableElement supertypeMethod : supertypeMethods) {
          if (supertypeMethod.getModifiers().contains(Modifier.ABSTRACT)) {
            ExecutableType methodType =
                Elements2.getExecutableElementAsMemberOf(types, supertypeMethod, extendingType);
            implementationMethodDescriptors.add(
                new ImplementationMethodDescriptor.Builder()
                    .name(supertypeMethod.getSimpleName().toString())
                    .returnType(getAnnotatedType(element).getQualifiedName().toString())
                    .publicMethod()
                    .passedParameters(
                        Parameter.forParameterList(
                            supertypeMethod.getParameters(), methodType.getParameterTypes()))
                    .build());
          }
        }
        for (TypeElement implementingType : declaration.get().implementingTypes()) {
          List<ExecutableElement> interfaceMethods =
              ElementFilter.methodsIn(elements.getAllMembers(implementingType));
          for (ExecutableElement interfaceMethod : interfaceMethods) {
            if (interfaceMethod.getModifiers().contains(Modifier.ABSTRACT)) {
              ExecutableType methodType =
                  Elements2.getExecutableElementAsMemberOf(
                      types, interfaceMethod, implementingType);
              implementationMethodDescriptors.add(
                  new ImplementationMethodDescriptor.Builder()
                      .name(interfaceMethod.getSimpleName().toString())
                      .returnType(getAnnotatedType(element).getQualifiedName().toString())
                      .publicMethod()
                      .passedParameters(
                          Parameter.forParameterList(
                              interfaceMethod.getParameters(), methodType.getParameterTypes()))
                      .build());
            }
          }
        }
      }

      ImmutableSet<FactoryMethodDescriptor> descriptors =
          factoryDescriptorGenerator.generateDescriptor(element);
      indexedMethods.putAll(
          Multimaps.index(
              descriptors,
              new Function<FactoryMethodDescriptor, String>() {
                @Override
                public String apply(FactoryMethodDescriptor descriptor) {
                  return descriptor.factoryName();
                }
              }));
    }

    for (Entry<String, Collection<FactoryMethodDescriptor>> entry :
        indexedMethods.build().asMap().entrySet()) {
      ImmutableSet.Builder<String> extending = ImmutableSet.builder();
      ImmutableSortedSet.Builder<String> implementing = ImmutableSortedSet.naturalOrder();
      boolean publicType = false;
      Boolean allowSubclasses = null;
      boolean skipCreation = false;
      for (FactoryMethodDescriptor methodDescriptor : entry.getValue()) {
        extending.add(methodDescriptor.declaration().extendingType().getQualifiedName().toString());
        for (TypeElement implementingType : methodDescriptor.declaration().implementingTypes()) {
          implementing.add(implementingType.getQualifiedName().toString());
        }
        publicType |= methodDescriptor.publicMethod();
        if (allowSubclasses == null) {
          allowSubclasses = methodDescriptor.declaration().allowSubclasses();
        } else if (!allowSubclasses.equals(methodDescriptor.declaration().allowSubclasses())) {
          skipCreation = true;
          messager.printMessage(
              Kind.ERROR,
              "Cannot mix allowSubclasses=true and allowSubclasses=false in one factory.",
              methodDescriptor.declaration().target(),
              methodDescriptor.declaration().mirror(),
              methodDescriptor.declaration().valuesMap().get("allowSubclasses"));
        }
      }
      if (!skipCreation) {
        try {
          factoryWriter.writeFactory(
              new FactoryDescriptor(
                  entry.getKey(),
                  Iterables.getOnlyElement(extending.build()),
                  implementing.build(),
                  publicType,
                  ImmutableSet.copyOf(entry.getValue()),
                  // TODO(gak): this needs to be indexed too
                  implementationMethodDescriptors.build(),
                  allowSubclasses));
        } catch (IOException e) {
          messager.printMessage(Kind.ERROR, "failed");
        }
      }
    }
  }
Exemplo n.º 16
0
 public Builder putAll(ListMultimap<CellLabel, Cell> cellMap) {
   cellsBuilder.putAll(cellMap);
   return this;
 }
Exemplo n.º 17
0
 public Builder put(CellLabel cellLabel, Term content) {
   cellsBuilder.put(cellLabel, new Cell(cellLabel, content));
   return this;
 }
Exemplo n.º 18
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);
  }