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

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

Introduction

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

Prototype

public ImmutableSet<K> keySet() 

Source Link

Usage

From source file:com.google.devtools.build.lib.remote.CachedLocalSpawnRunner.java

private static Command buildCommand(List<String> arguments, ImmutableMap<String, String> environment) {
    Command.Builder command = Command.newBuilder();
    command.addAllArgv(arguments);//from   w w  w .  j a v a 2s  .  co  m
    // Sorting the environment pairs by variable name.
    TreeSet<String> variables = new TreeSet<>(environment.keySet());
    for (String var : variables) {
        command.addEnvironmentBuilder().setVariable(var).setValue(environment.get(var));
    }
    return command.build();
}

From source file:com.telefonica.iot.cygnus.management.GUIHandlers.java

/**
 * Handles GET /points.//from   w ww. j  a  v a 2  s . c  o  m
 * @param response
 * @param channels
 * @throws IOException
 */
public static void getPoints(HttpServletResponse response, ImmutableMap<String, Channel> channels)
        throws IOException {
    // add a new source row
    String sourceColumns = "";

    // add a new channel row
    String channelColumns = "\"count\"";
    String point = "[" + numPoints;

    for (String key : channels.keySet()) {
        Channel channel = channels.get(key);
        channelColumns += ",\"" + channel.getName() + "\"";

        if (channel instanceof CygnusChannel) {
            CygnusChannel cygnusChannel = (CygnusChannel) channel;
            point += "," + cygnusChannel.getNumEvents();
        } // if12
    } // for

    point += "]";

    if (channelRows.length() == 0) {
        channelRows += point;
    } else {
        channelRows += "," + point;
    } // if else

    // add a new sink row
    String sinkColumns = "";

    // increase the points counter
    numPoints++;

    // return the points
    response.setContentType("application/json; charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter()
            .println("{\"source_points\":{\"columns\":[" + sourceColumns + "],\"rows\":[" + SOURCE_ROWS + "]},"
                    + "\"channel_points\":{\"columns\":[" + channelColumns + "],\"rows\":[" + channelRows
                    + "]}," + "\"sink_points\":{\"columns\":[" + sinkColumns + "],\"rows\":[" + SINK_ROWS
                    + "]}}");
}

From source file:com.sourceclear.headlines.util.HeaderBuilder.java

public static String formatDirectives(ImmutableMap<String, ImmutableList<String>> directives) {

    // In the case of an empty map return the empty string:
    if (directives.isEmpty()) {
        return "";
    }/*from   w  ww. j a v  a 2 s  .c  o m*/

    StringBuilder sb = new StringBuilder();

    // Outer loop - loop through each directive
    for (String directive : directives.keySet()) {

        // Don't add a directive if it has zero elements
        if (directives.get(directive).size() == 0) {
            continue;
        }

        StringBuilder elements = new StringBuilder();

        // Inner loop = for each directive build up the element String
        for (String element : directives.get(directive)) {
            elements.append(element).append(" ");
        }

        if (sb.length() > 0) {
            sb.append("; ");
        }

        sb.append(directive).append(" ").append(elements.append(" ").toString());
    }

    return sb.toString().trim();
}

From source file:com.android.builder.files.IncrementalRelativeFileSets.java

/**
 * Counts how many different base directories are there in a relative file set. This method will
 * look at the base of every {@link RelativeFile} and count how many distinct bases are that
 * that are directories./*ww  w  .j a va 2  s.  c  o m*/
 *
 * @param set the file set
 * @return the number of distinct base directories
 */
public static int getBaseDirectoryCount(@NonNull ImmutableMap<RelativeFile, FileStatus> set) {
    return Sets.newHashSet(
            Iterables.filter(Iterables.transform(set.keySet(), RelativeFile.EXTRACT_BASE), Files.isDirectory()))
            .size();
}

From source file:com.telefonica.iot.cygnus.management.MetricsHandlers.java

/**
 * Merges metrics from all the given sources and sinks. It is protected in order it can be tested.
 * @param sources//from w w  w .j  a va2s  .c o m
 * @param sinks
 * @return
 */
protected static CygnusMetrics mergeMetrics(ImmutableMap<String, SourceRunner> sources,
        ImmutableMap<String, SinkRunner> sinks) {
    CygnusMetrics mergedMetrics = new CygnusMetrics();

    if (sources != null) {
        for (String key : sources.keySet()) {
            Source source;
            HTTPSourceHandler handler;

            try {
                SourceRunner sr = sources.get(key);
                source = sr.getSource();
                Field f = source.getClass().getDeclaredField("handler");
                f.setAccessible(true);
                handler = (HTTPSourceHandler) f.get(source);
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
                    | SecurityException e) {
                LOGGER.error("There was a problem when getting a sink. Details: " + e.getMessage());
                continue;
            } // try catch

            if (handler instanceof CygnusHandler) {
                CygnusHandler ch = (CygnusHandler) handler;
                CygnusMetrics sourceMetrics = ch.getServiceMetrics();
                mergedMetrics.merge(sourceMetrics);
            } // if
        } // for
    } // if

    if (sinks != null) {
        for (String key : sinks.keySet()) {
            Sink sink;

            try {
                SinkRunner sr = sinks.get(key);
                SinkProcessor sp = sr.getPolicy();
                Field f = sp.getClass().getDeclaredField("sink");
                f.setAccessible(true);
                sink = (Sink) f.get(sp);
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
                    | SecurityException e) {
                LOGGER.error("There was a problem when getting a sink. Details: " + e.getMessage());
                continue;
            } // try catch

            if (sink instanceof CygnusSink) {
                CygnusSink cs = (CygnusSink) sink;
                CygnusMetrics sinkMetrics = cs.getServiceMetrics();
                mergedMetrics.merge(sinkMetrics);
            } // if
        } // for
    } // if

    return mergedMetrics;
}

From source file:org.chaston.oakfunds.storage.RecordType.java

private static ImmutableMap<String, AttributeType> buildAttributes(Class<?> recordTypeClass,
        String columnNamespace, @Nullable Class<?> parentClass) {
    Map<String, AttributeType> attributes = new HashMap<>();
    extractAttributesFromMethods(attributes, recordTypeClass, columnNamespace);
    boolean seenParentClass = false;
    for (Class<?> interfaceClass : recordTypeClass.getInterfaces()) {
        if (interfaceClass.equals(parentClass)) {
            seenParentClass = true;//from   w w w  .  j  a  v a 2  s.co  m
            continue;
        }
        ImmutableMap<String, AttributeType> superInterfaceAttributes = buildAttributes(interfaceClass,
                columnNamespace, null);
        for (String otherAttribute : superInterfaceAttributes.keySet()) {
            if (attributes.containsKey(otherAttribute)) {
                throw new IllegalStateException(
                        "Attribute " + otherAttribute + " is declared more than once for type "
                                + recordTypeClass.getName() + " via super types.");
            }
        }
        attributes.putAll(superInterfaceAttributes);
    }
    if (parentClass != null && !seenParentClass) {
        throw new IllegalStateException(
                "Parent class " + parentClass.getName() + " was not seen while extracting attributes for "
                        + recordTypeClass.getName() + ".  Was the parent type set correctly?");
    }
    return ImmutableMap.copyOf(attributes);
}

From source file:com.google.devtools.build.lib.skylarkdebug.server.DebugEventHelper.java

private static ImmutableList<Scope> getScopes(DebugFrame frame) {
    ImmutableMap<String, Object> localVars = frame.lexicalFrameBindings();
    if (localVars.isEmpty()) {
        return ImmutableList.of(getScope("global", frame.globalBindings()));
    }/*w ww.  j  a  v a2 s  . com*/
    Map<String, Object> globalVars = new LinkedHashMap<>(frame.globalBindings());
    // remove shadowed bindings
    localVars.keySet().forEach(globalVars::remove);

    return ImmutableList.of(getScope("local", localVars), getScope("global", globalVars));
}

From source file:org.apache.james.jmap.model.Message.java

protected static boolean areAttachedMessagesKeysInAttachments(ImmutableList<Attachment> attachments,
        ImmutableMap<BlobId, SubMessage> attachedMessages) {
    return attachedMessages.isEmpty()
            || attachedMessages.keySet().stream().anyMatch(inAttachments(attachments));
}

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./*from w w w. java 2 s  .  c  o  m*/
 * @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.//from  w w  w. j ava 2  s.co  m
 * @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();
}