Example usage for com.google.common.collect ImmutableMap.Builder put

List of usage examples for com.google.common.collect ImmutableMap.Builder put

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMap.Builder put.

Prototype

public final V put(K k, V v) 

Source Link

Usage

From source file:com.facebook.buck.cxx.platform.NativeLinkables.java

/**
 * Extract from the dependency graph all the libraries which must be considered for linking.
 *
 * <p>Traversal proceeds depending on whether each dependency is to be statically or dynamically
 * linked./*w w  w. j  ava 2 s. co m*/
 *
 * @param linkStyle how dependencies should be linked, if their preferred_linkage is {@code
 *     NativeLinkable.Linkage.ANY}.
 */
public static ImmutableMap<BuildTarget, NativeLinkable> getNativeLinkables(final CxxPlatform cxxPlatform,
        Iterable<? extends NativeLinkable> inputs, final Linker.LinkableDepType linkStyle,
        final Predicate<? super NativeLinkable> traverse) {

    final Map<BuildTarget, NativeLinkable> nativeLinkables = new HashMap<>();
    for (NativeLinkable nativeLinkable : inputs) {
        nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
    }

    final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
    AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(
            nativeLinkables.keySet()) {
        @Override
        public ImmutableSet<BuildTarget> visit(BuildTarget target) {
            NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
            graph.addNode(target);

            // We always traverse a rule's exported native linkables.
            Iterable<? extends NativeLinkable> nativeLinkableDeps = nativeLinkable
                    .getNativeLinkableExportedDepsForPlatform(cxxPlatform);

            boolean shouldTraverse = true;
            switch (nativeLinkable.getPreferredLinkage(cxxPlatform)) {
            case ANY:
                shouldTraverse = linkStyle != Linker.LinkableDepType.SHARED;
                break;
            case SHARED:
                shouldTraverse = false;
                break;
            case STATIC:
                shouldTraverse = true;
                break;
            }

            // If we're linking this dependency statically, we also need to traverse its deps.
            if (shouldTraverse) {
                nativeLinkableDeps = Iterables.concat(nativeLinkableDeps,
                        nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform));
            }

            // Process all the traversable deps.
            ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
            for (NativeLinkable dep : nativeLinkableDeps) {
                if (traverse.apply(dep)) {
                    BuildTarget depTarget = dep.getBuildTarget();
                    graph.addEdge(target, depTarget);
                    deps.add(depTarget);
                    nativeLinkables.put(depTarget, dep);
                }
            }
            return deps.build();
        }
    };
    visitor.start();

    // Topologically sort the rules.
    Iterable<BuildTarget> ordered = TopologicalSort.sort(graph).reverse();

    // Return a map of of the results.
    ImmutableMap.Builder<BuildTarget, NativeLinkable> result = ImmutableMap.builder();
    for (BuildTarget target : ordered) {
        result.put(target, nativeLinkables.get(target));
    }
    return result.build();
}

From source file:brooklyn.rest.transform.LocationTransformer.java

private static Map<String, ?> copyConfig(Map<String, ?> entries, LocationDetailLevel level) {
    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
    if (level != LocationDetailLevel.NONE) {
        for (Map.Entry<String, ?> entry : entries.entrySet()) {
            if (level == LocationDetailLevel.FULL_INCLUDING_SECRET || !Entities.isSecret(entry.getKey())) {
                builder.put(entry.getKey(), WebResourceUtils.getValueForDisplay(entry.getValue(), true, false));
            }//from w  w w  . ja v a2 s.c  o  m
        }
    }
    return builder.build();
}

From source file:com.google.devtools.build.java.turbine.javac.JavacTurbine.java

private static ImmutableMap<String, JarOwner> parseJarsToTargets(ImmutableMap<String, String> input) {
    ImmutableMap.Builder<String, JarOwner> result = ImmutableMap.builder();
    for (Map.Entry<String, String> entry : input.entrySet()) {
        result.put(entry.getKey(), parseJarOwner(entry.getKey()));
    }// ww w  .j  a  va 2s .  c  om
    return result.build();
}

From source file:com.facebook.buck.apple.CxxPlatformXcodeConfigGenerator.java

private static void setCxxLibraryValue(List<String> notProcessedCxxFlags,
        Map<String, String> notProcessedAppendedConfig, ImmutableMap.Builder<String, String> configBuilder) {
    String clangCxxLibraryValue = getConfigValueForKey(CLANG_CXX_LIBRARY, notProcessedCxxFlags, "-stdlib=",
            Optional.<String>absent(), notProcessedAppendedConfig);
    if (clangCxxLibraryValue != null) {
        configBuilder.put(CLANG_CXX_LIBRARY, clangCxxLibraryValue);
        notProcessedAppendedConfig.remove(CLANG_CXX_LIBRARY);
    }/*from   w  ww.j  a  v  a  2 s. c  o  m*/
}

From source file:org.apache.gobblin.lineage.LineageInfo.java

/**
 * Retrieve all lineage information from different {@link State}s.
 * This requires the job id and dataset urn to be present in the state, under job.id and dataset.urn.
 * A global union operation is applied to combine all <K, V> pairs from the input {@link State}s. If multiple {@link State}s
 * share the same K, but have conflicting V, a {@link LineageException} is thrown.
 *
 * {@link Level} can control if a dataset level or branch level information should be used. When {@link Level#All} is
 * specified, all levels of information will be returned; otherwise only specified level of information will be returned.
 *
 * For instance, assume we have below input states:
 *    State[0]: gobblin.lineage.K1          ---> V1
 *              gobblin.lineage.K2          ---> V2
 *              gobblin.lineage.branch.1.K4 ---> V4
 *    State[1]: gobblin.lineage.K2          ---> V2
 *              gobblin.lineage.K3          ---> V3
 *              gobblin.lineage.branch.1.K4 ---> V4
 *              gobblin.lineage.branch.1.K5 ---> V5
 *              gobblin.lineage.branch.2.K6 ---> V6
 *
 *  (1) With {@link Level#DATASET} level, the output would be:
 *      LinieageInfo[0]:  K1 ---> V1// w  w w .  ja  va2  s  .  c  om
 *                        K2 ---> V2
 *                        K3 ---> V3
 *  (2) With {@link Level#All} level, the output would be: (because there are two branches, so there are two LineageInfo)
 *      LineageInfo[0]:   K1 ---> V1
 *                        K2 ---> V2
 *                        K3 ---> V3
 *                        K4 ---> V4
 *                        K5 ---> V5
 *
 *      LineageInfo[1]:   K1 ---> V1
 *                        K2 ---> V2
 *                        K3 ---> V3
 *                        K6 ---> V6
 *
 *   (3) With {@link Level#BRANCH} level, the output would be: (only branch level information was returned)
 *      LineageInfo[0]:   K4 ---> V4
 *                        K5 ---> V5
 *      LineageInfo[1]:   K6 ---> V6
 *
 * @param states All states which belong to the same dataset and share the same jobId.
 * @param level {@link Level#DATASET}  only load dataset level lineage attributes
 *              {@link Level#BRANCH}   only load branch level lineage attributes
 *              {@link Level#All}      load all lineage attributes
 * @return A collection of {@link LineageInfo}s per branch. When level is {@link Level#DATASET}, this list has only single element.
 *
 * @throws LineageException.LineageConflictAttributeException if two states have same key but not the same value.
 */
public static Collection<LineageInfo> load(Collection<? extends State> states, Level level)
        throws LineageException {
    Preconditions.checkArgument(states != null && !states.isEmpty());
    Map<String, String> datasetMetaData = new HashMap<>();
    Map<String, Map<String, String>> branchAggregate = new HashMap<>();

    State anyOne = states.iterator().next();
    String jobId = anyOne.getProp(ConfigurationKeys.JOB_ID_KEY, "");
    String urn = anyOne.getProp(ConfigurationKeys.DATASET_URN_KEY, ConfigurationKeys.DEFAULT_DATASET_URN);

    for (State state : states) {
        for (Map.Entry<Object, Object> entry : state.getProperties().entrySet()) {
            if (entry.getKey() instanceof String && ((String) entry.getKey()).startsWith(LINEAGE_NAME_SPACE)) {

                String lineageKey = ((String) entry.getKey());
                String lineageValue = (String) entry.getValue();

                if (lineageKey.startsWith(BRANCH_PREFIX)) {
                    String branchPrefixStrip = lineageKey.substring(BRANCH_PREFIX.length());
                    String branchId = branchPrefixStrip.substring(0, branchPrefixStrip.indexOf("."));
                    String key = branchPrefixStrip.substring(branchPrefixStrip.indexOf(".") + 1);

                    if (level == Level.BRANCH || level == Level.All) {
                        if (!branchAggregate.containsKey(branchId)) {
                            branchAggregate.put(branchId, new HashMap<>());
                        }
                        Map<String, String> branchMetaData = branchAggregate.get(branchId);
                        String prev = branchMetaData.put(key, lineageValue);
                        if (prev != null && !prev.equals(lineageValue)) {
                            throw new LineageException.LineageConflictAttributeException(lineageKey, prev,
                                    lineageValue);
                        }
                    }
                } else if (lineageKey.startsWith(DATASET_PREFIX)) {
                    if (level == Level.DATASET || level == Level.All) {
                        String prev = datasetMetaData.put(lineageKey.substring(DATASET_PREFIX.length()),
                                lineageValue);
                        if (prev != null && !prev.equals(lineageValue)) {
                            throw new LineageException.LineageConflictAttributeException(lineageKey, prev,
                                    lineageValue);
                        }
                    }
                }
            }
        }
    }

    Collection<LineageInfo> collection = Sets.newHashSet();

    if (level == Level.DATASET) {
        ImmutableMap<String, String> metaData = ImmutableMap.<String, String>builder().putAll(datasetMetaData)
                .build();
        collection.add(new LineageInfo(urn, jobId, metaData));
        return collection;
    } else if (level == Level.BRANCH || level == Level.All) {
        if (branchAggregate.isEmpty()) {
            if (level == Level.All) {
                collection.add(new LineageInfo(urn, jobId,
                        ImmutableMap.<String, String>builder().putAll(datasetMetaData).build()));
            }
            return collection;
        }
        for (Map.Entry<String, Map<String, String>> branchMetaDataEntry : branchAggregate.entrySet()) {
            String branchId = branchMetaDataEntry.getKey();
            Map<String, String> branchMetaData = branchMetaDataEntry.getValue();
            ImmutableMap.Builder<String, String> metaDataBuilder = ImmutableMap.builder();
            if (level == Level.All) {
                metaDataBuilder.putAll(datasetMetaData);
            }
            metaDataBuilder.putAll(branchMetaData).put(BRANCH_ID_METADATA_KEY, branchId);
            collection.add(new LineageInfo(urn, jobId, metaDataBuilder.build()));
        }

        return collection;
    } else {
        throw new LineageException.LineageUnsupportedLevelException(level);
    }
}

From source file:edu.mit.streamjit.impl.compiler.Schedule.java

private static <T> Schedule<T> schedule(ImmutableSet<T> things,
        ImmutableSet<ExecutionConstraint<T>> executionConstraints,
        ImmutableSet<BufferingConstraint<T>> bufferingConstraints, int multiplier, int fireCost,
        int excessBufferCost) {
    ILPSolver solver = new ILPSolver();
    //There's one variable for each thing, which represents the number of
    //times it fires.  This uses the default bounds.  (TODO: perhaps a bound
    //at 1 if we're steady-state scheduling, maybe by marking things as
    //must-fire and marking the bottommost thing?)
    ImmutableMap.Builder<T, ILPSolver.Variable> variablesBuilder = ImmutableMap.builder();
    for (T thing : things)
        variablesBuilder.put(thing, solver.newVariable(thing.toString()));
    ImmutableMap<T, ILPSolver.Variable> variables = variablesBuilder.build();

    for (ExecutionConstraint<T> constraint : executionConstraints)
        solver.constrainAtLeast(variables.get(constraint.thing).asLinearExpr(1), constraint.minExecutions);

    HashMap<ILPSolver.Variable, Integer> sumOfConstraints = new HashMap<>();
    for (ILPSolver.Variable v : variables.values())
        sumOfConstraints.put(v, 0);//from  ww  w  . j  av a 2  s.  co m
    for (BufferingConstraint<T> constraint : bufferingConstraints) {
        ILPSolver.Variable upstreamVar = variables.get(constraint.upstream),
                downstreamVar = variables.get(constraint.downstream);
        ILPSolver.LinearExpr expr = upstreamVar.asLinearExpr(constraint.pushRate).minus(constraint.popRate,
                downstreamVar);
        switch (constraint.condition) {
        case LESS_THAN_EQUAL:
            solver.constrainAtMost(expr, constraint.bufferDelta);
            break;
        case EQUAL:
            solver.constrainEquals(expr, constraint.bufferDelta);
            break;
        case GREATER_THAN_EQUAL:
            solver.constrainAtLeast(expr, constraint.bufferDelta);
            break;
        default:
            throw new AssertionError(constraint.condition);
        }

        sumOfConstraints.put(upstreamVar, sumOfConstraints.get(upstreamVar) + constraint.pushRate);
        sumOfConstraints.put(downstreamVar, sumOfConstraints.get(downstreamVar) - constraint.popRate);
    }

    //Add a special constraint to ensure at least one filter fires.
    //TODO: in init schedules we might not always need this...
    Iterator<ILPSolver.Variable> variablesIter = variables.values().iterator();
    ILPSolver.LinearExpr totalFirings = variablesIter.next().asLinearExpr(1);
    while (variablesIter.hasNext())
        totalFirings = totalFirings.plus(1, variablesIter.next());
    solver.constrainAtLeast(totalFirings, 1);

    for (ILPSolver.Variable v : variables.values())
        sumOfConstraints.put(v, sumOfConstraints.get(v) * excessBufferCost + fireCost);
    ILPSolver.ObjectiveFunction objFn = solver.minimize(
            solver.newLinearExpr(Maps.filterValues(sumOfConstraints, Predicates.not(Predicates.equalTo(0)))));

    try {
        solver.solve();
    } catch (SolverException ex) {
        throw new ScheduleException(ex);
    }

    ImmutableMap.Builder<T, Integer> schedule = ImmutableMap.builder();
    for (Map.Entry<T, ILPSolver.Variable> e : variables.entrySet())
        schedule.put(e.getKey(), e.getValue().value() * multiplier);
    return new Schedule<>(things, bufferingConstraints, schedule.build());
}

From source file:com.facebook.buck.cxx.NativeLinkables.java

/**
 * Extract from the dependency graph all the libraries which must be considered for linking.
 *
 * Traversal proceeds depending on whether each dependency is to be statically or dynamically
 * linked./*  w ww.  ja  v  a  2s  . c  om*/
 *
 * @param linkStyle how dependencies should be linked, if their preferred_linkage is
 *                  {@code NativeLinkable.Linkage.ANY}.
 */
public static ImmutableMap<BuildTarget, NativeLinkable> getNativeLinkables(final CxxPlatform cxxPlatform,
        Iterable<? extends NativeLinkable> inputs, final Linker.LinkableDepType linkStyle,
        final Predicate<? super NativeLinkable> traverse) {

    final Map<BuildTarget, NativeLinkable> nativeLinkables = Maps.newHashMap();
    for (NativeLinkable nativeLinkable : inputs) {
        nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
    }

    final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
    AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(
            nativeLinkables.keySet()) {
        @Override
        public ImmutableSet<BuildTarget> visit(BuildTarget target) {
            NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
            graph.addNode(target);

            // We always traverse a rule's exported native linkables.
            Iterable<? extends NativeLinkable> nativeLinkableDeps = nativeLinkable
                    .getNativeLinkableExportedDepsForPlatform(cxxPlatform);

            boolean shouldTraverse = true;
            switch (nativeLinkable.getPreferredLinkage(cxxPlatform)) {
            case ANY:
                shouldTraverse = linkStyle != Linker.LinkableDepType.SHARED;
                break;
            case SHARED:
                shouldTraverse = false;
                break;
            case STATIC:
                shouldTraverse = true;
                break;
            }

            // If we're linking this dependency statically, we also need to traverse its deps.
            if (shouldTraverse) {
                nativeLinkableDeps = Iterables.concat(nativeLinkableDeps,
                        nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform));
            }

            // Process all the traversable deps.
            ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
            for (NativeLinkable dep : nativeLinkableDeps) {
                if (traverse.apply(dep)) {
                    BuildTarget depTarget = dep.getBuildTarget();
                    graph.addEdge(target, depTarget);
                    deps.add(depTarget);
                    nativeLinkables.put(depTarget, dep);
                }
            }
            return deps.build();
        }
    };
    visitor.start();

    // Topologically sort the rules.
    Iterable<BuildTarget> ordered = TopologicalSort.sort(graph).reverse();

    // Return a map of of the results.
    ImmutableMap.Builder<BuildTarget, NativeLinkable> result = ImmutableMap.builder();
    for (BuildTarget target : ordered) {
        result.put(target, nativeLinkables.get(target));
    }
    return result.build();
}

From source file:com.google.devtools.build.lib.packages.RuleFactory.java

/**
 * If the rule was created by a macro, this method sets the appropriate values for the
 * attributes generator_{name, function, location} and returns all attributes.
 *
 * <p>Otherwise, it returns the given attributes without any changes.
 *//*from   w ww  .  j  a va2s.c  o m*/
private static AttributesAndLocation generatorAttributesForMacros(BuildLangTypedAttributeValuesMap args,
        @Nullable Environment env, Location location, Label label) {
    // Returns the original arguments if a) there is only the rule itself on the stack
    // trace (=> no macro) or b) the attributes have already been set by Python pre-processing.
    if (env == null) {
        return new AttributesAndLocation(args, location);
    }
    boolean hasName = args.containsAttributeNamed("generator_name");
    boolean hasFunc = args.containsAttributeNamed("generator_function");
    // TODO(bazel-team): resolve cases in our code where hasName && !hasFunc, or hasFunc && !hasName
    if (hasName || hasFunc) {
        return new AttributesAndLocation(args, location);
    }
    Pair<FuncallExpression, BaseFunction> topCall = env.getTopCall();
    if (topCall == null || !(topCall.second instanceof UserDefinedFunction)) {
        return new AttributesAndLocation(args, location);
    }

    FuncallExpression generator = topCall.first;
    BaseFunction function = topCall.second;
    String name = generator.getNameArg();

    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
    for (String attributeName : args.getAttributeNames()) {
        builder.put(attributeName, args.getAttributeValue(attributeName));
    }
    builder.put("generator_name", (name == null) ? args.getAttributeValue("name") : name);
    builder.put("generator_function", function.getName());

    if (generator.getLocation() != null) {
        location = generator.getLocation();
    }
    String relativePath = maybeGetRelativeLocation(location, label);
    if (relativePath != null) {
        builder.put("generator_location", relativePath);
    }

    try {
        return new AttributesAndLocation(new BuildLangTypedAttributeValuesMap(builder.build()), location);
    } catch (IllegalArgumentException ex) {
        // We just fall back to the default case and swallow any messages.
        return new AttributesAndLocation(args, location);
    }
}

From source file:com.facebook.presto.hive.parquet.predicate.ParquetPredicateUtils.java

private static Map<Integer, Statistics<?>> getStatisticsByColumnOrdinal(BlockMetaData blockMetadata) {
    ImmutableMap.Builder<Integer, Statistics<?>> statistics = ImmutableMap.builder();
    for (int ordinal = 0; ordinal < blockMetadata.getColumns().size(); ordinal++) {
        Statistics<?> columnStatistics = blockMetadata.getColumns().get(ordinal).getStatistics();
        if (columnStatistics != null) {
            statistics.put(ordinal, columnStatistics);
        }/*from ww w . ja  v  a  2 s  .  c  o  m*/
    }
    return statistics.build();
}

From source file:org.smartdeveloperhub.vocabulary.util.Vocabularies.java

static Vocabularies create(final Catalog catalog) {
    final Map<Namespace, Vocabulary.Builder> builders = Maps.newLinkedHashMap();
    for (final String moduleId : catalog.modules()) {
        final Module module = catalog.get(moduleId);
        final Version version = Version.create(module);
        Vocabulary.Builder builder = builders.get(version.vocabulary());
        if (builder == null) {
            builder = Vocabulary.create(version.vocabulary());
            builders.put(version.vocabulary(), builder);
        }/* w  w w  . ja  v a2  s.co m*/
        builder.addVersion(version);
    }
    final ImmutableMap.Builder<Namespace, Vocabulary> vocabularies = ImmutableMap.builder();
    final ImmutableMap.Builder<Namespace, Namespace> versionIndex = ImmutableMap.builder();
    for (final Vocabulary.Builder vBuilder : builders.values()) {
        final Vocabulary vocabulary = vBuilder.build();
        final Namespace namespace = vocabulary.namespace();
        vocabularies.put(namespace, vocabulary);
        for (final Namespace version : vocabulary.versions()) {
            versionIndex.put(version, namespace);
        }
    }
    return new Vocabularies(vocabularies.build(), versionIndex.build());
}