Java Code Examples for com.google.common.collect.ImmutableMultimap#Builder

The following examples show how to use com.google.common.collect.ImmutableMultimap#Builder . 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: SplitZipStep.java    From buck with Apache License 2.0 6 votes vote down vote up
public Supplier<Multimap<Path, Path>> getOutputToInputsMapSupplier(
    Path secondaryOutputDir, Path additionalOutputDir) {
  return () -> {
    Preconditions.checkState(
        outputFiles != null,
        "SplitZipStep must complete successfully before listing its outputs.");

    ImmutableMultimap.Builder<Path, Path> builder = ImmutableMultimap.builder();
    for (APKModule dexStore : outputFiles.keySet()) {
      Path storeRoot;
      if (dexStore.getName().equals(SECONDARY_DEX_ID)) {
        storeRoot = secondaryOutputDir;
      } else {
        storeRoot = additionalOutputDir.resolve(dexStore.getName());
      }
      ImmutableList<Path> outputList = outputFiles.get(dexStore).asList();
      for (int i = 0; i < outputList.size(); i++) {
        String dexName = dexSplitMode.getDexStore().fileNameForSecondary(dexStore, i);
        Path outputDexPath = storeRoot.resolve(dexName);
        builder.put(outputDexPath, outputList.get(i));
      }
    }
    return builder.build();
  };
}
 
Example 2
Source File: ReflectiveSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Multimap<String, Function> createFunctionMap() {
  final ImmutableMultimap.Builder<String, Function> builder =
      ImmutableMultimap.builder();
  for (Method method : clazz.getMethods()) {
    final String methodName = method.getName();
    if (method.getDeclaringClass() == Object.class
        || methodName.equals("toString")) {
      continue;
    }
    if (TranslatableTable.class.isAssignableFrom(method.getReturnType())) {
      final TableMacro tableMacro =
          new MethodTableMacro(this, method);
      builder.put(methodName, tableMacro);
    }
  }
  return builder.build();
}
 
Example 3
Source File: ScreenDensityResourcesSplitter.java    From bundletool with Apache License 2.0 6 votes vote down vote up
private ImmutableMultimap<ResourceId, ConfigValue> getClaimedConfigs(
    Iterable<ModuleSplit> moduleSplits) {
  ImmutableMultimap.Builder<ResourceId, ConfigValue> result = new ImmutableMultimap.Builder<>();
  for (ModuleSplit moduleSplit : moduleSplits) {
    checkState(
        moduleSplit.getResourceTable().isPresent(),
        "Resource table not found in the density split.");
    for (Package pkg : moduleSplit.getResourceTable().get().getPackageList()) {
      for (Type type : pkg.getTypeList()) {
        for (Entry entry : type.getEntryList()) {
          for (ConfigValue configValue : entry.getConfigValueList()) {
            result.put(ResourceId.create(pkg, type, entry), configValue);
          }
        }
      }
    }
  }
  return result.build();
}
 
Example 4
Source File: Gsons.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Override
public Multimap<Character, Map.Entry<String, ValueAttribute>> apply(Iterable<ValueAttribute> attributes) {
  ImmutableMultimap.Builder<Character, Map.Entry<String, ValueAttribute>> builder = ImmutableMultimap.builder();

  for (ValueAttribute attribute : attributes) {
    String serializedName = attribute.getMarshaledName();
    builder.put(serializedName.charAt(0), Maps.immutableEntry(serializedName, attribute));

    for (String alternateName : attribute.getAlternateSerializedNames()) {
      if (!alternateName.isEmpty()) {
        builder.put(alternateName.charAt(0), Maps.immutableEntry(alternateName, attribute));
      }
    }
  }

  return builder.build();
}
 
Example 5
Source File: ClassFileManifest.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Returns a per-jar map of .class files changed in the new manifest */
public static Diff modifiedClasses(ClassFileManifest oldManifest, ClassFileManifest newManifest) {
  ImmutableMultimap.Builder<File, String> map = ImmutableMultimap.builder();
  for (Map.Entry<File, JarManifest> entry : newManifest.jarManifests.entrySet()) {
    // quick test for object equality -- jars are often not rebuilt
    JarManifest old = oldManifest.jarManifests.get(entry.getKey());
    if (old == entry.getValue()) {
      continue;
    }
    ImmutableList<String> changedClasses = JarManifest.diff(old, entry.getValue());
    if (!changedClasses.isEmpty()) {
      map.putAll(entry.getKey(), changedClasses);
    }
  }
  return new Diff(map.build());
}
 
Example 6
Source File: MockRemoteTaskFactory.java    From presto with Apache License 2.0 6 votes vote down vote up
public MockRemoteTask createTableScanTask(TaskId taskId, InternalNode newNode, List<Split> splits, PartitionedSplitCountTracker partitionedSplitCountTracker)
{
    Symbol symbol = new Symbol("column");
    PlanNodeId sourceId = new PlanNodeId("sourceId");
    PlanFragment testFragment = new PlanFragment(
            new PlanFragmentId("test"),
            TableScanNode.newInstance(
                    sourceId,
                    TEST_TABLE_HANDLE,
                    ImmutableList.of(symbol),
                    ImmutableMap.of(symbol, new TestingColumnHandle("column"))),
            ImmutableMap.of(symbol, VARCHAR),
            SOURCE_DISTRIBUTION,
            ImmutableList.of(sourceId),
            new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)),
            ungroupedExecution(),
            StatsAndCosts.empty(),
            Optional.empty());

    ImmutableMultimap.Builder<PlanNodeId, Split> initialSplits = ImmutableMultimap.builder();
    for (Split sourceSplit : splits) {
        initialSplits.put(sourceId, sourceSplit);
    }
    return createRemoteTask(TEST_SESSION, taskId, newNode, testFragment, initialSplits.build(), OptionalInt.empty(), createInitialEmptyOutputBuffers(BROADCAST), partitionedSplitCountTracker, true);
}
 
Example 7
Source File: LegalEntityRatesCurvesCsvLoader.java    From Strata with Apache License 2.0 6 votes vote down vote up
private static Multimap<LocalDate, Curve> buildCurves(
    Map<CurveName, LoadedCurveSettings> settingsMap,
    Map<LoadedCurveKey, List<LoadedCurveNode>> allNodes) {

  ImmutableMultimap.Builder<LocalDate, Curve> results = ImmutableMultimap.builder();

  for (Map.Entry<LoadedCurveKey, List<LoadedCurveNode>> entry : allNodes.entrySet()) {
    LoadedCurveKey key = entry.getKey();
    LoadedCurveSettings settings = settingsMap.get(key.getCurveName());

    if (settings == null) {
      throw new IllegalArgumentException(Messages.format("Missing settings for curve: {}", key));
    }
    results.put(key.getCurveDate(), settings.createCurve(key.getCurveDate(), entry.getValue()));
  }
  return results.build();
}
 
Example 8
Source File: KillAllEppResourcesActionTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private ImmutableMultimap<String, Object> getDatastoreContents() {
  ImmutableMultimap.Builder<String, Object> contentsBuilder = new ImmutableMultimap.Builder<>();
  // Filter out raw Entity objects created by the mapreduce.
  for (Object obj : Iterables.filter(ofy().load(), not(instanceOf(Entity.class)))) {
    contentsBuilder.put(Key.getKind(obj.getClass()), obj);
  }
  return contentsBuilder.build();
}
 
Example 9
Source File: JobUpdaterIT.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateSpecificInstances() throws Exception {
  expectTaskKilled();

  control.replay();

  JobUpdate builder =
      setInstanceCount(makeJobUpdate(makeInstanceConfig(0, 0, OLD_CONFIG)), 1).newBuilder();
  builder.getInstructions().getSettings().setUpdateOnlyTheseInstances(
      ImmutableSet.of(new Range(0, 0)));
  IJobUpdate update = IJobUpdate.build(builder);
  insertPendingTasks(OLD_CONFIG, ImmutableSet.of(0, 1));

  changeState(JOB, 0, ASSIGNED, STARTING, RUNNING);
  changeState(JOB, 1, ASSIGNED, STARTING, RUNNING);
  clock.advance(WATCH_TIMEOUT);

  // Instance 0 is updated
  updater.start(update, AUDIT);
  changeState(JOB, 0, KILLED, ASSIGNED, STARTING, RUNNING);
  clock.advance(WATCH_TIMEOUT);

  ImmutableMultimap.Builder<Integer, JobUpdateAction> actions = ImmutableMultimap.builder();
  assertState(
      ROLLED_FORWARD,
      actions.putAll(0, INSTANCE_UPDATING, INSTANCE_UPDATED).build());
  assertJobState(
      JOB,
      ImmutableMap.of(0, NEW_CONFIG, 1, OLD_CONFIG));
}
 
Example 10
Source File: ExopackageUtil.java    From buck with Apache License 2.0 5 votes vote down vote up
public static ImmutableMultimap<Path, Path> applyFilenameFormat(
    Multimap<String, Path> filesToHashes, Path deviceDir, String filenameFormat) {
  ImmutableMultimap.Builder<Path, Path> filesBuilder = ImmutableMultimap.builder();
  for (Map.Entry<String, Path> entry : filesToHashes.entries()) {
    filesBuilder.put(
        deviceDir.resolve(String.format(filenameFormat, entry.getKey())), entry.getValue());
  }
  return filesBuilder.build();
}
 
Example 11
Source File: BaseCartePlugin.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override public Map<String, Collection<String>> getHeaders() {
  ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
  for ( String name : fromEnumeration( req.getHeaderNames() ) ) {
    builder.putAll( name, fromEnumeration( req.getHeaders( name ) ) );
  }
  return builder.build().asMap();
}
 
Example 12
Source File: SmartDexingStep.java    From buck with Apache License 2.0 5 votes vote down vote up
private ImmutableMultimap<Path, Path> createXzsOutputsToInputs(
    Multimap<Path, Path> outputToInputs) {
  // Concatenate if solid compression is specified.
  // create a mapping of the xzs file target and the dex.jar files that go into it
  ImmutableMultimap.Builder<Path, Path> xzsMultimapBuilder = ImmutableMultimap.builder();
  for (Path p : outputToInputs.keySet()) {
    if (DexStore.XZS.matchesPath(p)) {
      String[] matches = p.getFileName().toString().split("-");
      Path output = p.getParent().resolve(matches[0].concat(SECONDARY_SOLID_DEX_EXTENSION));
      xzsMultimapBuilder.put(output, p);
    }
  }
  return xzsMultimapBuilder.build();
}
 
Example 13
Source File: KnowledgeBase.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
@Value.Lazy
public ImmutableMultimap<EntityNode, EntityMentionAssertion> entityToMentionAssertions() {
  final ImmutableMultimap.Builder<EntityNode, EntityMentionAssertion> ret =
      ImmutableMultimap.builder();
  ret.putAll(FluentIterable.from(assertions())
      .filter(NonCanonicalEntityMentionAssertion.class)
      .index(EntityMentionAssertion.SubjectFunction.INSTANCE));
  ret.putAll(FluentIterable.from(assertions())
      .filter(EntityCanonicalMentionAssertion.class)
      .index(EntityCanonicalMentionAssertionFunctions.subject()));
  return ret.build();
}
 
Example 14
Source File: ReflectiveRelMetadataProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <M extends Metadata> Multimap<Method, MetadataHandler<M>> handlers(
    MetadataDef<M> def) {
  final ImmutableMultimap.Builder<Method, MetadataHandler<M>> builder =
      ImmutableMultimap.builder();
  for (Map.Entry<Method, MetadataHandler> entry : handlerMap.entries()) {
    if (def.methods.contains(entry.getKey())) {
      //noinspection unchecked
      builder.put(entry.getKey(), entry.getValue());
    }
  }
  return builder.build();
}
 
Example 15
Source File: DefaultDataCenters.java    From emodb with Apache License 2.0 5 votes vote down vote up
private CachedInfo(Map<String, DataCenter> dataCenterByName) {
    verifySystemDataCenters(dataCenterByName.values());

    _dataCenterByName = dataCenterByName;

    ImmutableMultimap.Builder<String, DataCenter> builder = ImmutableMultimap.builder();
    for (DataCenter dataCenter : dataCenterByName.values()) {
        for (String keyspace : dataCenter.getCassandraKeyspaces()) {
            builder.put(keyspace, dataCenter);
        }
    }
    _dataCenterByKeyspace = builder.build();
}
 
Example 16
Source File: FilterAnswerKeyByTypeAndRole.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private static Multimap<Symbol, Symbol> loadTypesToValidRolesMap(final Parameters params)
    throws IOException {
  final Multimap<Symbol, Symbol> typesToValidRolesInitial = FileUtils.loadSymbolMultimap(
      Files.asCharSource(params.getExistingFile("typeAndRoleFile"), Charsets.UTF_8));
  final Set<Symbol> alwaysValidRoles = params.getSymbolSet("alwaysValidRoles");
  final ImmutableMultimap.Builder<Symbol, Symbol> typesToValidRolesB = ImmutableMultimap.builder();
  typesToValidRolesB.putAll(typesToValidRolesInitial);
  for (final Symbol eventType : typesToValidRolesInitial.keySet()) {
    typesToValidRolesB.putAll(eventType, alwaysValidRoles);
  }
  return typesToValidRolesB.build();
}
 
Example 17
Source File: SelectForDualAnnotation.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private static ImmutableMultimap<Symbol, Symbol> makeAnnotatorsToDocumentsMap(
    Table<Symbol, Symbol, Symbol> annotatorsTable, Set<Symbol> targetDocIDs) {
  final ImmutableMultimap.Builder<Symbol, Symbol> ret = ImmutableMultimap.builder();

  for (final Symbol docID : targetDocIDs) {
    for (final Symbol annotator : annotatorsTable.row(docID).values()) {
      ret.put(annotator, docID);
    }
  }

  return ret.build();
}
 
Example 18
Source File: OkJsons.java    From immutables with Apache License 2.0 5 votes vote down vote up
AttributesByFirstLetter(Iterable<ValueAttribute> attributes) {
  ImmutableMultimap.Builder<Character, ValueAttribute> builder = ImmutableMultimap.builder();

  for (ValueAttribute attribute : attributes) {
    String name = attribute.getMarshaledName();
    char firstChar = name.charAt(0);
    builder.put(firstChar, attribute);
  }

  byFirst = builder.build();
  asMap = byFirst.asMap();
}
 
Example 19
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();
}
 
Example 20
Source File: OutputsMaterializer.java    From buck with Apache License 2.0 4 votes vote down vote up
private void processFetchAndMaterialize() {
  ImmutableList.Builder<PendingMaterialization> builder = ImmutableList.builder();
  int size = 0;
  int items = 0;
  while (!waitingMaterialization.isEmpty()) {
    PendingMaterialization data = waitingMaterialization.poll();
    if (data == null) {
      break;
    }
    if (items == 0 || (data.digest.getSize() + size < sizeLimit)) {
      builder.add(data);
      size += data.digest.getSize();
      items++;
    } else {
      waitingMaterialization.addFirst(data);
      break;
    }
  }
  ImmutableList<PendingMaterialization> pending = builder.build();

  if (!pending.isEmpty()) {
    try (SimplePerfEvent.Scope ignored =
        SimplePerfEvent.scope(
            buckEventBus,
            SimplePerfEvent.PerfEventId.of("outputs-materializer"),
            "size",
            size,
            "items",
            pending.size())) {
      if (size > sizeLimit) {
        LOG.debug("Starting stream request for: " + pending.size() + " requests, size: " + size);
        PendingMaterialization large = Iterables.getOnlyElement(pending);

        // Download large files as a stream
        WritableByteChannel channel =
            large.materializer.getOutputChannel(large.path, large.isExecutable);
        ListenableFuture<Unit> fetchToStream = fetcher.fetchToStream(large.digest, channel);
        try {
          // Wait for the stream to finish downloading before picking up more work
          large.future.setFuture(fetchToStream);
          fetchToStream.get();
        } finally {
          tryCloseChannel(channel);
        }
      } else {
        LOG.debug("Starting batch request for: " + pending.size() + " items, size: " + size);
        // Download batches of small objects
        ImmutableMultimap.Builder<Digest, Callable<WritableByteChannel>> digestMap =
            ImmutableMultimap.builder();
        ImmutableMultimap.Builder<Digest, SettableFuture<Unit>> futureMap =
            ImmutableMultimap.builder();
        for (PendingMaterialization p : pending) {
          digestMap.put(p.digest, () -> p.materializer.getOutputChannel(p.path, p.isExecutable));
          futureMap.put(p.digest, p.future);
        }

        ImmutableMultimap<Digest, Callable<WritableByteChannel>> batch = digestMap.build();
        ImmutableMultimap<Digest, SettableFuture<Unit>> futures = futureMap.build();
        fetcher.batchFetchBlobs(batch, futures).get();
      }
      LOG.debug("Finished materializing: " + pending.size() + " requests, size: " + size);
    } catch (Exception e) {
      pending.forEach(materialization -> materialization.future.setException(e));
    }
  }

  if (!waitingMaterialization.isEmpty()) {
    materializerService.submit(this::processFetchAndMaterialize);
  }
}