Java Code Examples for com.google.common.collect.ImmutableSortedMap#forEach()

The following examples show how to use com.google.common.collect.ImmutableSortedMap#forEach() . 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: CellMappingsFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link NewCellPathResolver} from the root cell's path and config. We currently
 * require that all cells appear in the root cell's config.
 */
public static NewCellPathResolver create(AbsPath rootPath, Config rootConfig) {
  // TODO(cjhopman): We should support cells that the root cell doesn't know about. To do that, we
  // should probably continue to compute this mapping first (because that's hardest to get wrong).
  // It would require here that we be able to traverse all the other cells in the build and look
  // at their buckconfigs. We could construct canonical names for newly discovered cells by adding
  // a namespacing of some sort (i.e. secondary#tertiary).
  ImmutableSortedMap<String, AbsPath> cellMapping = getCellMapping(rootPath, rootConfig);

  Map<AbsPath, CanonicalCellName> canonicalNameMap = new LinkedHashMap<>();

  canonicalNameMap.put(rootPath, CanonicalCellName.rootCell());
  cellMapping.forEach(
      (name, path) ->
          canonicalNameMap.computeIfAbsent(
              path, ignored -> CanonicalCellName.of(Optional.of(name))));

  return DefaultNewCellPathResolver.of(ImmutableMap.copyOf(canonicalNameMap));
}
 
Example 2
Source File: URLDumpPane.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void printHeaders(String title, ImmutableSortedMap<String, ImmutableList<HttpNameValue>> headers) {
  appendLine(title);
  headers.forEach((key, value) -> {
    Formatter f = new Formatter();
    value.forEach(nv -> {
      if (nv.getValue() == null) {
        f.format("%s ", nv.getName());
      } else {
        f.format("%s=%s ", nv.getName(), nv.getValue());
      }
    });
    appendLine(String.format("  %s: %s", key, f.toString()));
  });
}
 
Example 3
Source File: NinjaTarget.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * We expand the rule's variables with the following assumptions: Rule variables can refer to
 * target's variables (and file variables). Interdependence between rule variables can happen
 * only for 'command' variable, for now we ignore other possible dependencies between rule
 * variables (seems the only other variable which can meaningfully depend on sibling variables
 * is description, and currently we are ignoring it).
 *
 * <p>Also, for resolving rule's variables we are using scope+offset of target, according to
 * specification (https://ninja-build.org/manual.html#_variable_expansion).
 *
 * <p>See {@link NinjaRuleVariable} for the list.
 */
private static ImmutableSortedMap<NinjaRuleVariable, NinjaVariableValue> reduceRuleVariables(
    NinjaScope targetScope,
    long targetOffset,
    Map<NinjaRuleVariable, NinjaVariableValue> ruleVariables,
    ImmutableSortedMap<String, String> targetVariables,
    Interner<String> interner) {
  ImmutableSortedMap.Builder<String, List<Pair<Long, String>>> variablesBuilder =
      ImmutableSortedMap.naturalOrder();
  targetVariables.forEach(
      (key, value) -> variablesBuilder.put(key, ImmutableList.of(Pair.of(0L, value))));
  NinjaScope scopeWithVariables =
      targetScope.createScopeFromExpandedValues(variablesBuilder.build());

  ImmutableSortedMap.Builder<NinjaRuleVariable, NinjaVariableValue> builder =
      ImmutableSortedMap.naturalOrder();

  // Description is taken from the "build" statement (instead of the referenced rule)
  // if it's available.
  boolean targetHasDescription = false;
  String targetVariable = targetVariables.get("description");
  if (targetVariable != null) {
    builder.put(
        NinjaRuleVariable.DESCRIPTION, NinjaVariableValue.createPlainText(targetVariable));
    targetHasDescription = true;
  }

  for (Map.Entry<NinjaRuleVariable, NinjaVariableValue> entry : ruleVariables.entrySet()) {
    NinjaRuleVariable type = entry.getKey();
    if (type.equals(NinjaRuleVariable.DESCRIPTION) && targetHasDescription) {
      // Don't use the rule description, as the target defined a specific description.
      continue;
    }
    NinjaVariableValue reducedValue =
        scopeWithVariables.getReducedValue(
            targetOffset, entry.getValue(), INPUTS_OUTPUTS_VARIABLES, interner);
    builder.put(type, reducedValue);
  }
  return builder.build();
}
 
Example 4
Source File: CellMappingsFactory.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link CellNameResolver} for a cell. */
public static CellNameResolver createCellNameResolver(
    AbsPath cellPath, Config config, NewCellPathResolver cellPathResolver) {
  ImmutableSortedMap<String, AbsPath> cellMapping = getCellMapping(cellPath, config);

  Map<Optional<String>, CanonicalCellName> builder = new LinkedHashMap<>();
  builder.put(Optional.empty(), cellPathResolver.getCanonicalCellName(cellPath.getPath()));
  cellMapping.forEach(
      (name, path) ->
          builder.put(Optional.of(name), cellPathResolver.getCanonicalCellName(path.getPath())));
  return DefaultCellNameResolver.of(builder);
}