Java Code Examples for com.google.common.collect.ImmutableMultimap#keySet()

The following examples show how to use com.google.common.collect.ImmutableMultimap#keySet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonLdSerializer.java    From schemaorg-java with Apache License 2.0 6 votes vote down vote up
private void writeReverse(JsonWriter out, ImmutableMultimap<String, Thing> map)
    throws IOException {
  Set<String> keySet = map.keySet();
  if (keySet.size() == 0) {
    return;
  }
  out.name(JsonLdConstants.REVERSE);
  out.beginObject();
  for (String key : keySet) {
    out.name(shortNameOf(key));
    Collection<Thing> values = map.get(key);
    if (values.size() == 0) {
      throw new JsonLdSyntaxException(
          String.format("JSON-LD reverse property %s has no value", key));
    } else if (values.size() == 1) {
      writeObject(out, (Thing) values.toArray()[0], false /* isTopLevelObject */);
    } else {
      out.beginArray();
      for (Thing value : values) {
        writeObject(out, value, false /* isTopLevelObject */);
      }
      out.endArray();
    }
  }
  out.endObject();
}
 
Example 2
Source File: APKModuleGraph.java    From buck with Apache License 2.0 6 votes vote down vote up
private void verifyNoSharedSeeds() {
  ImmutableMultimap<BuildTarget, String> sharedSeeds = sharedSeedsSupplier.get();
  if (!sharedSeeds.isEmpty()) {
    StringBuilder errorMessage = new StringBuilder();
    for (BuildTarget seed : sharedSeeds.keySet()) {
      errorMessage
          .append("BuildTarget: ")
          .append(seed)
          .append(" is used as seed in multiple modules: ");
      for (String module : sharedSeeds.get(seed)) {
        errorMessage.append(module).append(' ');
      }
      errorMessage.append('\n');
    }
    throw new IllegalArgumentException(errorMessage.toString());
  }
}
 
Example 3
Source File: AndroidBinaryGraphEnhancer.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Group DexProducedFromJavaLibrary rules by module, into partitions of at most
 * dex_group_lib_limit. Create a single partition per APK module if dex_group_lib_limit is 0
 */
private ImmutableMultimap<APKModule, List<DexProducedFromJavaLibrary>> groupDexes(
    ImmutableMultimap<APKModule, DexProducedFromJavaLibrary> dexFilesToMerge) {
  ImmutableMultimap.Builder<APKModule, List<DexProducedFromJavaLibrary>> resultBuilder =
      ImmutableMultimap.builder();

  int limit = dexSplitMode.getDexGroupLibLimit();
  for (APKModule module : dexFilesToMerge.keySet()) {
    List<DexProducedFromJavaLibrary> currentDexContents = null;
    for (DexProducedFromJavaLibrary dexWithClasses : dexFilesToMerge.get(module)) {
      if (currentDexContents == null || (limit != 0 && currentDexContents.size() + 1 > limit)) {
        currentDexContents = new ArrayList<>();
        resultBuilder.put(module, currentDexContents);
      }
      currentDexContents.add(dexWithClasses);
    }
  }
  return resultBuilder.build();
}
 
Example 4
Source File: JSqlSchema.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Multimap<String, Function> getFunctionMultimap() {
	ImmutableMultimap<String, ScalarFunction> funcs = ScalarFunctionImpl.createAll(JSONFunction.class);
	Multimap<String, Function> functions = HashMultimap.create();
	for (String key : funcs.keySet()) {
		for (ScalarFunction func : funcs.get(key)) {
			functions.put(key, func);
		}
	}
	return functions;
}
 
Example 5
Source File: JavaTestModuleRule.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(TargetNode<JavaTestDescription.CoreArg> target, ModuleBuildContext context) {
  Optional<Path> presetResourcesRoot = target.getConstructorArg().getResourcesRoot();
  ImmutableSortedSet<SourcePath> resources = target.getConstructorArg().getResources();
  ImmutableSet<Path> resourcePaths;
  if (presetResourcesRoot.isPresent()) {
    resourcePaths =
        getResourcePaths(target.getConstructorArg().getResources(), presetResourcesRoot.get());
    addResourceFolders(
        IjResourceFolderType.JAVA_TEST_RESOURCE,
        resourcePaths,
        presetResourcesRoot.get(),
        context);
  } else {
    resourcePaths = getResourcePaths(resources);
    ImmutableMultimap<Path, Path> resourcesRootsToResources =
        getResourcesRootsToResources(packageFinder, resourcePaths);
    for (Path resourcesRoot : resourcesRootsToResources.keySet()) {
      addResourceFolders(
          IjResourceFolderType.JAVA_TEST_RESOURCE,
          resourcesRootsToResources.get(resourcesRoot),
          resourcesRoot,
          context);
    }
  }
  addDepsAndTestSources(target, true /* wantsPackagePrefix */, context, resourcePaths);
  JavaLibraryRuleHelper.addCompiledShadowIfNeeded(projectConfig, target, context);
  context.setJavaLanguageLevel(JavaLanguageLevelHelper.getLanguageLevel(projectConfig, target));
}
 
Example 6
Source File: JavaLibraryModuleRule.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(TargetNode<JavaLibraryDescription.CoreArg> target, ModuleBuildContext context) {
  Optional<Path> presetResourcesRoot = target.getConstructorArg().getResourcesRoot();
  ImmutableSortedSet<SourcePath> resources = target.getConstructorArg().getResources();
  ImmutableSet<Path> resourcePaths;
  if (presetResourcesRoot.isPresent()) {
    resourcePaths = getResourcePaths(resources, presetResourcesRoot.get());
    addResourceFolders(
        IjResourceFolderType.JAVA_RESOURCE, resourcePaths, presetResourcesRoot.get(), context);
  } else {
    resourcePaths = getResourcePaths(resources);
    ImmutableMultimap<Path, Path> resourcesRootsToResources =
        getResourcesRootsToResources(packageFinder, resourcePaths);
    for (Path resourcesRoot : resourcesRootsToResources.keySet()) {
      addResourceFolders(
          IjResourceFolderType.JAVA_RESOURCE,
          resourcesRootsToResources.get(resourcesRoot),
          resourcesRoot,
          context);
    }
  }

  addDepsAndSources(target, true /* wantsPackagePrefix */, context, resourcePaths);
  JavaLibraryRuleHelper.addCompiledShadowIfNeeded(projectConfig, target, context);
  JavaLibraryRuleHelper.addNonSourceBuildTargets(target, context);
  context.setJavaLanguageLevel(JavaLanguageLevelHelper.getLanguageLevel(projectConfig, target));
  context.setCompilerOutputPath(moduleFactoryResolver.getCompilerOutputPath(target));
}
 
Example 7
Source File: APKModuleGraph.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Group the classes in the input jars into a multimap based on the APKModule they belong to
 *
 * @param apkModuleToJarPathMap the mapping of APKModules to the path for the jar files
 * @param translatorFunction function used to translate the class names to obfuscated names
 * @param filesystem filesystem representation for resolving paths
 * @return The mapping of APKModules to the class names they contain
 * @throws IOException
 */
public static ImmutableMultimap<APKModule, String> getAPKModuleToClassesMap(
    ImmutableMultimap<APKModule, Path> apkModuleToJarPathMap,
    Function<String, String> translatorFunction,
    ProjectFilesystem filesystem)
    throws IOException {
  ImmutableMultimap.Builder<APKModule, String> builder = ImmutableSetMultimap.builder();
  if (!apkModuleToJarPathMap.isEmpty()) {
    for (APKModule dexStore : apkModuleToJarPathMap.keySet()) {
      for (Path jarFilePath : apkModuleToJarPathMap.get(dexStore)) {
        ClasspathTraverser classpathTraverser = new DefaultClasspathTraverser();
        classpathTraverser.traverse(
            new ClasspathTraversal(ImmutableSet.of(jarFilePath), filesystem) {
              @Override
              public void visit(FileLike entry) {
                if (!entry.getRelativePath().endsWith(".class")) {
                  // ignore everything but class files in the jar.
                  return;
                }

                String classpath = entry.getRelativePath().replaceAll("\\.class$", "");

                if (translatorFunction.apply(classpath) != null) {
                  builder.put(dexStore, translatorFunction.apply(classpath));
                }
              }
            });
      }
    }
  }
  return builder.build();
}
 
Example 8
Source File: AbiNativeLibrariesSplitter.java    From bundletool with Apache License 2.0 4 votes vote down vote up
/** Generates {@link ModuleSplit} objects dividing the native libraries by ABI. */
@Override
public ImmutableCollection<ModuleSplit> split(ModuleSplit moduleSplit) {
  if (!moduleSplit.getNativeConfig().isPresent()) {
    return ImmutableList.of(moduleSplit);
  }

  ImmutableList.Builder<ModuleSplit> splits = new ImmutableList.Builder<>();
  // Flatten all targeted directories.
  List<TargetedNativeDirectory> allTargetedDirectories =
      moduleSplit.getNativeConfig().get().getDirectoryList();
  // Currently we only support targeting via ABI, so grouping it by Targeting.equals() should be
  // enough.
  ImmutableMultimap<NativeDirectoryTargeting, TargetedNativeDirectory> targetingMap =
      Multimaps.index(allTargetedDirectories, TargetedNativeDirectory::getTargeting);
  // We need to know the exact set of ABIs that we will generate, to set alternatives correctly.
  ImmutableSet<Abi> abisToGenerate =
      targetingMap.keySet().stream()
          .map(NativeDirectoryTargeting::getAbi)
          .collect(toImmutableSet());

  // Any entries not claimed by the ABI splits will be returned in a separate split using the
  // original targeting.
  HashSet<ModuleEntry> leftOverEntries = new HashSet<>(moduleSplit.getEntries());
  for (NativeDirectoryTargeting targeting : targetingMap.keySet()) {
    ImmutableList<ModuleEntry> entriesList =
        targetingMap
            .get(targeting)
            .stream()
            .flatMap(directory -> moduleSplit.findEntriesUnderPath(directory.getPath()))
            .collect(toImmutableList());
      ModuleSplit.Builder splitBuilder =
          moduleSplit
              .toBuilder()
              .setApkTargeting(
                  moduleSplit
                      .getApkTargeting()
                      .toBuilder()
                      .setAbiTargeting(
                          AbiTargeting.newBuilder()
                              .addValue(targeting.getAbi())
                              .addAllAlternatives(
                                  Sets.difference(
                                      abisToGenerate, ImmutableSet.of(targeting.getAbi()))))
                      .build())
              .setMasterSplit(false)
              .addMasterManifestMutator(withSplitsRequired(true))
              .setEntries(entriesList);
      splits.add(splitBuilder.build());
    leftOverEntries.removeAll(entriesList);
  }
  if (!leftOverEntries.isEmpty()) {
    splits.add(moduleSplit.toBuilder().setEntries(ImmutableList.copyOf(leftOverEntries)).build());
  }
  return splits.build();
}
 
Example 9
Source File: FilterLinkingStore.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
private static void trueMain(String[] argv) throws IOException {
  final Parameters params = Parameters.loadSerifStyle(new File(argv[0]));
  final File inputStore = params.getExistingDirectory("inputStore");
  final File outputStore = params.getCreatableDirectory("outputStore");
  final SystemOutputLayout layout = SystemOutputLayout.ParamParser.fromParamVal(
      params.getString("outputLayout"));

  checkArgument(layout instanceof KBPEA2016OutputLayout,
      "Not a compatible output layout: expected KBP_EAL_2017 or KBP_EAL_2016");

  final CrossDocSystemOutputStore input = (CrossDocSystemOutputStore) layout.open(inputStore);
  final CrossDocSystemOutputStore output =
      (CrossDocSystemOutputStore) layout.openOrCreate(outputStore);
  final ImmutableSet<KBPRealis> linkableRealises =
      ImmutableSet.of(KBPRealis.Actual, KBPRealis.Other);

  final Predicate<Response> realisIsLinkable =
      compose(in(linkableRealises), ResponseFunctions.realis());

  // collect all the surviving filtering
  final ImmutableSet.Builder<DocEventFrameReference> survivingIDsB = ImmutableSet.builder();
  for (final Symbol docID : input.docIDs()) {
    final DocumentSystemOutput2015 oldOutput = input.read(docID);
    final ResponseLinking filtered =
        oldOutput.linking().copyWithFilteredResponses(realisIsLinkable);
    for (final String surviving : filtered.responseSetIds().get().keySet()) {
      survivingIDsB.add(DocEventFrameReference.of(docID, surviving));
    }
    final DocumentSystemOutput2015 newOutput =
        DocumentSystemOutput2015.from(oldOutput.arguments(), filtered);
    output.write(newOutput);
  }
  final ImmutableSet<DocEventFrameReference> survivingFrames = survivingIDsB.build();

  // remove those from the CorpusEventLinking
  final CorpusEventLinking.Builder newCorpusLinkingB = CorpusEventLinking.builder();
  final ImmutableMultimap<CorpusEventFrame, DocEventFrameReference> corpusEventToDocEvent =
      input.readCorpusEventFrames().docEventsToCorpusEvents().inverse();
  for (final CorpusEventFrame cef : corpusEventToDocEvent.keySet()) {
    final ImmutableSet<DocEventFrameReference> survivingDocEvents =
        FluentIterable.from(corpusEventToDocEvent.get(cef)).filter(in(survivingFrames)).toSet();
    if (survivingDocEvents.size() > 0) {
      final CorpusEventFrame res = CorpusEventFrame.of(cef.id(), survivingDocEvents);
      newCorpusLinkingB.addCorpusEventFrames(res);
    }
  }

  output.writeCorpusEventFrames(newCorpusLinkingB.build());
}
 
Example 10
Source File: DalvikAwareZipSplitter.java    From buck with Apache License 2.0 4 votes vote down vote up
private DalvikAwareZipSplitter(
    ProjectFilesystem filesystem,
    Set<Path> inFiles,
    Path outPrimary,
    Path outSecondaryDir,
    String secondaryPattern,
    Path outDexStoresDir,
    long linearAllocLimit,
    Predicate<String> requiredInPrimaryZip,
    Set<String> wantedInPrimaryZip,
    ImmutableSet<String> secondaryHeadSet,
    ImmutableSet<String> secondaryTailSet,
    ImmutableMultimap<APKModule, String> additionalDexStoreSets,
    APKModule rootAPKModule,
    DexSplitStrategy dexSplitStrategy,
    Path reportDir) {
  if (linearAllocLimit <= 0) {
    throw new HumanReadableException("linear_alloc_hard_limit must be greater than zero.");
  }
  this.filesystem = filesystem;
  this.inFiles = ImmutableSet.copyOf(inFiles);
  this.outPrimary = outPrimary;
  this.secondaryDexWriter =
      new MySecondaryDexHelper("secondary", outSecondaryDir, secondaryPattern);
  this.additionalDexWriters = new HashMap<>();
  this.requiredInPrimaryZip = requiredInPrimaryZip;
  this.wantedInPrimaryZip = ImmutableSet.copyOf(wantedInPrimaryZip);
  this.secondaryHeadSet = secondaryHeadSet;
  this.secondaryTailSet = secondaryTailSet;
  this.classPathToDexStore = additionalDexStoreSets.inverse();
  for (APKModule dexStore : additionalDexStoreSets.keySet()) {
    if (!dexStore.equals(rootAPKModule)) {
      additionalDexWriters.put(
          dexStore,
          new MySecondaryDexHelper(
              dexStore.getCanaryClassName(),
              outDexStoresDir.resolve(dexStore.getName()),
              secondaryPattern));
    }
  }
  this.rootModule = rootAPKModule;
  this.reportDir = reportDir;
  this.dexSplitStrategy = dexSplitStrategy;
  this.linearAllocLimit = linearAllocLimit;
  this.dalvikStatsCache = new DalvikStatsCache();
}
 
Example 11
Source File: PreDexedFilesSorterTest.java    From buck with Apache License 2.0 4 votes vote down vote up
private ImmutableMap<String, PreDexedFilesSorter.Result> generatePreDexSorterResults(
    int numberOfPrimaryDexes, int numberOfSecondaryDexes, int numberOfExtraDexes)
    throws IOException {
  FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
  ImmutableMultimap.Builder<APKModule, DexWithClasses> inputDexes = ImmutableMultimap.builder();
  for (int i = 0; i < numberOfPrimaryDexes; i++) {
    inputDexes.put(
        moduleGraph.getRootAPKModule(),
        createFakeDexWithClasses(
            filesystem,
            Paths.get("primary").resolve(String.format("primary%d.dex", i)),
            ImmutableSet.of(String.format("primary.primary%d.class", i)),
            STANDARD_DEX_FILE_ESTIMATE));
  }
  for (int i = 0; i < numberOfSecondaryDexes; i++) {
    inputDexes.put(
        moduleGraph.getRootAPKModule(),
        createFakeDexWithClasses(
            filesystem,
            Paths.get("secondary").resolve(String.format("secondary%d.dex", i)),
            ImmutableSet.of(String.format("secondary.secondary%d.class", i)),
            STANDARD_DEX_FILE_ESTIMATE));
  }
  for (int i = 0; i < numberOfExtraDexes; i++) {
    inputDexes.put(
        extraModule,
        createFakeDexWithClasses(
            filesystem,
            Paths.get("extra").resolve(String.format("extra%d.dex", i)),
            ImmutableSet.of(String.format("extra.extra%d.class", i)),
            STANDARD_DEX_FILE_ESTIMATE));
  }

  ImmutableMultimap<APKModule, DexWithClasses> dexes = inputDexes.build();
  ImmutableMap.Builder<String, PreDexedFilesSorter.Result> results = ImmutableMap.builder();
  ImmutableList.Builder<Step> steps = ImmutableList.builder();

  for (APKModule module : dexes.keySet()) {
    PreDexedFilesSorter sorter =
        new PreDexedFilesSorter(
            dexes.get(module),
            ImmutableSet.of(PRIMARY_DEX_PATTERN),
            moduleGraph,
            module,
            tempDir.newFolder(module.getName(), "scratch").toPath(),
            DEX_WEIGHT_LIMIT,
            DexStore.JAR,
            tempDir.newFolder(module.getName(), "secondary").toPath(),
            Optional.empty());
    results.put(module.getName(), sorter.sortIntoPrimaryAndSecondaryDexes(filesystem, steps));
  }
  return results.build();
}