Example usage for com.google.common.collect ImmutableMap containsKey

List of usage examples for com.google.common.collect ImmutableMap containsKey

Introduction

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

Prototype

@Override
    public boolean containsKey(@Nullable Object key) 

Source Link

Usage

From source file:com.google.polymer.JsRenamer.java

/**
 * Renames path expressions without using the Closure Compiler for parsing.
 * @param renameMap A mapping from symbol to renamed symbol.
 * @param pathExpression The path expression to rename.
 * @return The renamed path expression.//  www  .j a  v  a 2  s.c o m
 */
private static String renamePolymerPathExpression(ImmutableMap<String, String> renameMap,
        String pathExpression) {
    if (renameMap.containsKey(pathExpression)) {
        return renameMap.get(pathExpression);
    } else if (pathExpression.contains(".")) {
        String[] components = PROPERTY_EXPRESSION_SPLITTER.splitToList(pathExpression).toArray(new String[0]);
        for (int i = 0; i < components.length; i++) {
            components[i] = renamePolymerPathExpression(renameMap, components[i]);
        }
        return PROPERTY_EXPRESSION_JOINER.join(components);
    }
    return pathExpression;
}

From source file:com.google.template.soy.shared.internal.ModuleUtils.java

/**
 * Given the set of all Soy function implementations, a specific Soy function type (subtype of
 * SoyFunction) to look for, another Soy function type to look for that is an equivalent
 * deprecated version of the specific Soy function type, and an adapt function for adapting the
 * deprecated type to the specific type, finds the Soy functions that implement either type and
 * returns them in the form of a map from function name to function, where the functions with
 * the deprecated type have been adapted using the adapt function.
 *
 * @param <T> The specific Soy function type to look for.
 * @param <D> The equivalent deprecated Soy function type to also look for.
 * @param soyFunctionsSet The set of all Soy functions.
 * @param specificSoyFunctionType The class of the specific Soy function type to look for.
 * @param equivDeprecatedSoyFunctionType The class of the equivalent deprecated Soy function
 *     type to also look for.//ww w .ja v  a 2  s. c  om
 * @param adaptFn The adapt function that adapts the deprecated type to the specific type.
 * @return A map of the relevant specific Soy functions (name to function).
 */
public static <T extends SoyFunction, D extends SoyFunction> ImmutableMap<String, T> buildSpecificSoyFunctionsMapWithAdaptation(
        Set<SoyFunction> soyFunctionsSet, Class<T> specificSoyFunctionType,
        Class<D> equivDeprecatedSoyFunctionType, Function<D, T> adaptFn) {

    ImmutableMap<String, T> tMap = buildSpecificSoyFunctionsMap(soyFunctionsSet, specificSoyFunctionType);
    ImmutableMap<String, D> dMap = buildSpecificSoyFunctionsMap(soyFunctionsSet,
            equivDeprecatedSoyFunctionType);

    ImmutableMap.Builder<String, T> resultMapBuilder = ImmutableMap.builder();
    resultMapBuilder.putAll(tMap);
    for (String functionName : dMap.keySet()) {
        if (tMap.containsKey(functionName)) {
            if (tMap.get(functionName).equals(dMap.get(functionName))) {
                throw new IllegalStateException(String.format(
                        "Found function named '%s' that implements both %s and"
                                + " %s -- please remove the latter deprecated interface.",
                        functionName, specificSoyFunctionType.getSimpleName(),
                        equivDeprecatedSoyFunctionType.getSimpleName()));
            } else {
                throw new IllegalStateException(String.format(
                        "Found two functions with the same name '%s', one implementing %s and the"
                                + " other implementing %s",
                        functionName, specificSoyFunctionType.getSimpleName(),
                        equivDeprecatedSoyFunctionType.getSimpleName()));
            }
        }
        resultMapBuilder.put(functionName, adaptFn.apply(dMap.get(functionName)));
    }
    return resultMapBuilder.build();
}

From source file:com.google.template.soy.shared.internal.ModuleUtils.java

/**
 * Given the set of all Soy directive implementations, a specific Soy directive type (subtype of
 * SoyPrintDirective) to look for, another Soy directive type to look for that is an equivalent
 * deprecated version of the specific Soy directive type, and an adapt function for adapting the
 * deprecated type to the specific type, finds the Soy directives that implement either type and
 * returns them in the form of a map from directive name to directive, where the directives with
 * the deprecated type have been adapted using the adapt function.
 *
 * @param <T> The specific Soy directive type to look for.
 * @param <D> The equivalent deprecated Soy directive type to also look for.
 * @param soyDirectivesSet The set of all Soy directives.
 * @param specificSoyDirectiveType The class of the specific Soy directive type to look for.
 * @param equivDeprecatedSoyDirectiveType The class of the equivalent deprecated Soy directive
 *     type to also look for.//ww  w  . j  a va 2 s.  c  om
 * @param adaptFn The adapt function that adapts the deprecated type to the specific type.
 * @return A map of the relevant specific Soy directives (name to directive).
 */
public static <T extends SoyPrintDirective, D extends SoyPrintDirective> ImmutableMap<String, T> buildSpecificSoyDirectivesMapWithAdaptation(
        Set<SoyPrintDirective> soyDirectivesSet, Class<T> specificSoyDirectiveType,
        Class<D> equivDeprecatedSoyDirectiveType, Function<D, T> adaptFn) {

    ImmutableMap<String, T> tMap = buildSpecificSoyDirectivesMap(soyDirectivesSet, specificSoyDirectiveType);
    ImmutableMap<String, D> dMap = buildSpecificSoyDirectivesMap(soyDirectivesSet,
            equivDeprecatedSoyDirectiveType);

    ImmutableMap.Builder<String, T> resultMapBuilder = ImmutableMap.builder();
    resultMapBuilder.putAll(tMap);
    for (String directiveName : dMap.keySet()) {
        if (tMap.containsKey(directiveName)) {
            if (tMap.get(directiveName).equals(dMap.get(directiveName))) {
                throw new IllegalStateException(String.format(
                        "Found print directive named '%s' that implements both %s and"
                                + " %s -- please remove the latter deprecated interface.",
                        directiveName, specificSoyDirectiveType.getSimpleName(),
                        equivDeprecatedSoyDirectiveType.getSimpleName()));
            } else {
                throw new IllegalStateException(String.format(
                        "Found two print directives with the same name '%s', one implementing %s and the"
                                + " other implementing %s",
                        directiveName, specificSoyDirectiveType.getSimpleName(),
                        equivDeprecatedSoyDirectiveType.getSimpleName()));
            }
        }
        resultMapBuilder.put(directiveName, adaptFn.apply(dMap.get(directiveName)));
    }
    return resultMapBuilder.build();
}

From source file:org.elasticsearch.index.analysis.Analysis.java

private static CharArraySet resolveNamedWords(Collection<String> words, ImmutableMap<String, Set<?>> namedWords,
        Version version, boolean ignoreCase) {
    if (namedWords == null) {
        return new CharArraySet(version, words, ignoreCase);
    }/*from w  w w .j a  v a  2s  . c o m*/
    CharArraySet setWords = new CharArraySet(version, words.size(), ignoreCase);
    for (String word : words) {
        if (namedWords.containsKey(word)) {
            setWords.addAll(namedWords.get(word));
        } else {
            setWords.add(word);
        }
    }
    return setWords;
}

From source file:com.google.polymer.JsRenamer.java

/**
 * Renames Polymer property changed object property identifiers (*Changed properties).
 * @param renameMap A mapping from symbol to renamed symbol.
 * @param node The string node containing the property changed identifier.
 *///from www . j a v a2  s.c  o m
private static void renamePolymerPropertyStringNode(ImmutableMap<String, String> renameMap, Node node) {
    String name = node.getString();
    if (renameMap.containsKey(name)) {
        node.setString(renameMap.get(name));
    } else if (name.endsWith(CHANGED_SUFFIX)) {
        String basename = name.substring(0, name.length() - CHANGED_SUFFIX.length());
        if (renameMap.containsKey(basename)) {
            node.setString(renameMap.get(basename) + CHANGED_SUFFIX);
        }
    }
}

From source file:com.google.polymer.JsRenamer.java

/**
 * Renames all object literals that are standalone or contained in a Polymer v0.8 style call.
 * This allows behaviors coverage, which are indistinguishable from regular JavaScript objects.
 * @param renameMap A mapping from symbol to renamed symbol.
 * @param objectLit Object literal node.
 *//*  w  w w .  j  a va  2  s.co m*/
private static void renameObjectLiteral(ImmutableMap<String, String> renameMap, Node objectLit) {
    ImmutableMap<String, Node> objectMap = convertObjectLitNodeToMap(objectLit);
    if (isInPolymerCall(objectLit) && !objectMap.containsKey("is")) {
        // This object map is not in a non-Polymer v0.8 or newer call.
        return;
    }
    renameObjectMap(renameMap, objectMap);
}

From source file:com.google.devtools.build.lib.standalone.StandaloneSpawnStrategy.java

/**
 * Adds to the given environment all variables that are dependent on system state of the host
 * machine./*from  w  w  w. java  2  s  . com*/
 *
 * <p>Admittedly, hermeticity is "best effort" in such cases; these environment values should be
 * as tied to configuration parameters as possible.
 *
 * <p>For example, underlying iOS toolchains require that SDKROOT resolve to an absolute system
 * path, but, when selecting which SDK to resolve, the version number comes from build
 * configuration.
 *
 * @return the new environment, comprised of the old environment plus any new variables
 * @throws UserExecException if any variables dependent on system state could not be resolved
 */
public static ImmutableMap<String, String> locallyDeterminedEnv(Path execRoot, String productName,
        ImmutableMap<String, String> env) throws UserExecException {
    // TODO(bazel-team): Remove apple-specific logic from this class.
    ImmutableMap.Builder<String, String> newEnvBuilder = ImmutableMap.builder();
    newEnvBuilder.putAll(env);
    // Empty developer dir indicates to use the system default.
    // TODO(bazel-team): Bazel's view of the xcode version and developer dir
    // should be explicitly set for build hermiticity.
    String developerDir = "";
    if (env.containsKey(AppleConfiguration.XCODE_VERSION_ENV_NAME)) {
        developerDir = getDeveloperDir(execRoot, productName,
                env.get(AppleConfiguration.XCODE_VERSION_ENV_NAME));
        newEnvBuilder.put("DEVELOPER_DIR", developerDir);
    }
    if (env.containsKey(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME)) {
        // The Apple platform is needed to select the appropriate SDK.
        if (!env.containsKey(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME)) {
            throw new UserExecException("Could not resolve apple platform for determining SDK");
        }
        String iosSdkVersion = env.get(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME);
        String appleSdkPlatform = env.get(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME);
        newEnvBuilder.put("SDKROOT",
                getSdkRootEnv(execRoot, productName, developerDir, iosSdkVersion, appleSdkPlatform));
    }
    return newEnvBuilder.build();
}

From source file:com.google.polymer.JsRenamer.java

/**
 * Renames a string node as if the entire string contained the symbol.
 * @param renameMap A mapping from symbol to renamed symbol.
 * @param node String node to rename in entirety. Can be null. Will not attempt a rename if the
 *     node is not a string node.//from  w  w w  .j a  va2s. co  m
 */
private static void renameStringNode(ImmutableMap<String, String> renameMap, Node node) {
    if (node == null || !node.isString()) {
        return;
    }

    String symbolName = node.getString();
    if (renameMap.containsKey(symbolName)) {
        node.setString(renameMap.get(symbolName));
    }
}

From source file:ome.services.blitz.repo.path.FilePathRestrictions.java

/**
 * Combine sets of rules to form a set that satisfies them all.
 * @param rules at least one set of rules
 * @return the intersection of the given rules
 *///from  www  . ja  v  a 2  s .  c  om
private static FilePathRestrictions combineRules(FilePathRestrictions... rules) {
    if (rules.length == 0) {
        throw new IllegalArgumentException("cannot combine an empty list of rules");
    }

    int index = 0;
    FilePathRestrictions product = rules[index++];

    while (index < rules.length) {
        final FilePathRestrictions toCombine = rules[index++];

        final Set<Character> safeCharacters = Sets.intersection(product.safeCharacters,
                toCombine.safeCharacters);

        if (safeCharacters.isEmpty()) {
            throw new IllegalArgumentException("cannot combine safe characters");
        }

        final Set<Integer> allKeys = Sets.union(product.transformationMatrix.keySet(),
                toCombine.transformationMatrix.keySet());
        final ImmutableMap<Integer, Collection<Integer>> productMatrixMap = product.transformationMatrix
                .asMap();
        final ImmutableMap<Integer, Collection<Integer>> toCombineMatrixMap = toCombine.transformationMatrix
                .asMap();
        final SetMultimap<Integer, Integer> newTransformationMatrix = HashMultimap.create();

        for (final Integer key : allKeys) {
            final Collection<Integer> values;
            if (!productMatrixMap.containsKey(key)) {
                values = toCombineMatrixMap.get(key);
            } else if (!toCombineMatrixMap.containsKey(key)) {
                values = productMatrixMap.get(key);
            } else {
                final Set<Integer> valuesSet = new HashSet<Integer>(productMatrixMap.get(key));
                valuesSet.retainAll(toCombineMatrixMap.get(key));
                if (valuesSet.isEmpty()) {
                    throw new IllegalArgumentException(
                            "cannot combine transformations for Unicode code point " + key);
                }
                values = valuesSet;
            }
            for (final Integer value : values) {
                newTransformationMatrix.put(key, value);
            }
        }

        final SetMultimap<Integer, Integer> entriesRemoved = HashMultimap.create();
        boolean transitiveClosing;
        do {
            transitiveClosing = false;
            for (final Entry<Integer, Integer> transformation : newTransformationMatrix.entries()) {
                final int to = transformation.getValue();
                if (newTransformationMatrix.containsKey(to)) {
                    final int from = transformation.getKey();
                    if (!entriesRemoved.put(from, to)) {
                        throw new IllegalArgumentException(
                                "cyclic transformation involving Unicode code point " + from);
                    }
                    newTransformationMatrix.remove(from, to);
                    newTransformationMatrix.putAll(from, newTransformationMatrix.get(to));
                    transitiveClosing = true;
                    break;
                }
            }
        } while (transitiveClosing);

        product = new FilePathRestrictions(newTransformationMatrix,
                Sets.union(product.unsafePrefixes, toCombine.unsafePrefixes),
                Sets.union(product.unsafeSuffixes, toCombine.unsafeSuffixes),
                Sets.union(product.unsafeNames, toCombine.unsafeNames), safeCharacters);
    }
    return product;
}

From source file:com.facebook.buck.features.python.PythonBinaryDescription.java

public static ImmutableMap<Path, SourcePath> addMissingInitModules(ImmutableMap<Path, SourcePath> modules,
        SourcePath emptyInit) {/*from  ww w. ja v a2 s  . co m*/

    Map<Path, SourcePath> initModules = new LinkedHashMap<>();

    // Insert missing `__init__.py` modules.
    Set<Path> packages = new HashSet<>();
    for (Path module : modules.keySet()) {
        Path pkg = module;
        while ((pkg = pkg.getParent()) != null && !packages.contains(pkg)) {
            Path init = pkg.resolve("__init__.py");
            if (!modules.containsKey(init)) {
                initModules.put(init, emptyInit);
            }
            packages.add(pkg);
        }
    }

    return ImmutableMap.<Path, SourcePath>builder().putAll(modules).putAll(initModules).build();
}