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

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

Introduction

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

Prototype

public static <K, V> Builder<K, V> builder() 

Source Link

Usage

From source file:io.prestosql.sql.gen.IsolatedClass.java

public static <T> Class<? extends T> isolateClass(DynamicClassLoader dynamicClassLoader,
        Class<T> publicBaseClass, Class<? extends T> implementationClass, Class<?>... additionalClasses) {
    ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
    builder.put(implementationClass.getName(), getBytecode(implementationClass));
    for (Class<?> additionalClass : additionalClasses) {
        builder.put(additionalClass.getName(), getBytecode(additionalClass));
    }//  w  ww .j a  v  a2s. co  m

    // load classes into a private class loader
    Map<String, Class<?>> isolatedClasses = dynamicClassLoader.defineClasses(builder.build());
    Class<?> isolatedClass = isolatedClasses.get(implementationClass.getName());

    // verify the isolated class
    checkArgument(isolatedClass != null, "Could load class %s", implementationClass.getName());
    checkArgument(publicBaseClass.isAssignableFrom(isolatedClass),
            "Error isolating class %s, newly loaded class is not a sub type of %s",
            implementationClass.getName(), publicBaseClass.getName());
    checkState(isolatedClass != implementationClass, "Isolation failed");

    return isolatedClass.asSubclass(publicBaseClass);
}

From source file:com.facebook.buck.testutil.FakeFileHashCache.java

public static FakeFileHashCache createFromStrings(Map<String, String> pathsToHashes) {
    ImmutableMap.Builder<Path, HashCode> builder = ImmutableMap.builder();
    for (Map.Entry<String, String> entry : pathsToHashes.entrySet()) {
        builder.put(Paths.get(entry.getKey()), HashCode.fromString(entry.getValue()));
    }/*  w w  w .  j  a  va2 s. c  o  m*/
    return new FakeFileHashCache(builder.build());
}

From source file:com.facebook.buck.config.Inis.java

public static ImmutableMap<String, ImmutableMap<String, String>> read(Reader reader) throws IOException {
    Ini ini = new Ini();
    Config config = ini.getConfig();// w ww.  ja  v  a  2 s.  com
    config.setEscape(false);
    config.setEscapeNewline(true);
    ini.load(reader);
    validateIni(ini);

    ImmutableMap.Builder<String, ImmutableMap<String, String>> sectionsToEntries = ImmutableMap.builder();
    for (String sectionName : ini.keySet()) {
        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        Profile.Section section = ini.get(sectionName);
        for (String propertyName : section.keySet()) {
            String propertyValue = section.get(propertyName);
            builder.put(propertyName, propertyValue);
        }

        ImmutableMap<String, String> sectionToEntries = builder.build();
        sectionsToEntries.put(sectionName, sectionToEntries);
    }

    return sectionsToEntries.build();

}

From source file:com.facebook.presto.sql.gen.IsolatedClass.java

public static <T> Class<? extends T> isolateClass(DynamicClassLoader dynamicClassLoader,
        Class<T> publicBaseClass, Class<? extends T> implementationClass, Class<?>... additionalClasses) {
    ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
    builder.put(implementationClass.getName(), getByteCode(implementationClass));
    for (Class<?> additionalClass : additionalClasses) {
        builder.put(additionalClass.getName(), getByteCode(additionalClass));
    }/*from   w  w w.  jav a2 s .  com*/

    // load classes into a private class loader
    Map<String, Class<?>> isolatedClasses = dynamicClassLoader.defineClasses(builder.build());
    Class<?> isolatedClass = isolatedClasses.get(implementationClass.getName());

    // verify the isolated class
    checkArgument(isolatedClass != null, "Could load class %s", implementationClass.getName());
    checkArgument(publicBaseClass.isAssignableFrom(isolatedClass),
            "Error isolating class %s, newly loaded class is not a sub type of %s",
            implementationClass.getName(), publicBaseClass.getName());
    checkState(isolatedClass != implementationClass, "Isolation failed");

    return isolatedClass.asSubclass(publicBaseClass);
}

From source file:com.facebook.buck.jvm.java.JavacPluginJsr199FieldsSerializer.java

public static ImmutableMap<String, Object> serialize(JavacPluginJsr199Fields fields) {
    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();

    builder.put(CAN_REUSE_CLASSPATH, fields.getCanReuseClassLoader());
    builder.put(PROCESSOR_NAMES, fields.getProcessorNames().asList());
    builder.put(CLASSPATH, fields.getClasspath().stream().map(URL::toString).collect(Collectors.toList()));

    return builder.build();
}

From source file:com.jgaap.generics.NumericTransformationEventDriver.java

protected static ImmutableMap<String, String> getTransformationMap(String filename) {
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(NumericTransformationEventDriver.class
            .getResourceAsStream(JGAAPConstants.JGAAP_RESOURCE_PACKAGE + filename)));
    String current;/*from  w ww  . j  a  v a2s . co m*/
    try {
        while ((current = reader.readLine()) != null) {
            if (current.startsWith("/")) {
                String[] pair = current.split("/");
                builder.put(pair[1], pair[2]);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.build();
}

From source file:com.google.api.tools.framework.importers.swagger.SwaggerFileWriter.java

/** Saves the file contents on the disk and returns the saved file paths. */
public static Map<String, FileWrapper> saveFilesOnDisk(List<FileWrapper> inputFiles) {

    ImmutableMap.Builder<String, FileWrapper> savedFiles = ImmutableMap.builder();
    File tempDir = Files.createTempDir();
    String tmpDirLocation = tempDir.getAbsolutePath();
    for (FileWrapper inputFile : inputFiles) {
        String filePath = inputFile.getFilename().replaceAll("[\\\\/:]", "_");

        Preconditions.checkState(!Strings.isNullOrEmpty(inputFile.getFileContents().toString()),
                "swagger spec file contents empty");
        Preconditions.checkState(!Strings.isNullOrEmpty(filePath), "swagger spec file path not provided");

        String filePathToSave = File.separator + tmpDirLocation + File.separator + "swagger_spec_files"
                + File.separator + filePath;
        FileWrapper fileToSave = FileWrapper.create(filePathToSave, inputFile.getFileContents());
        try {//from   w  w  w  . j av  a 2 s  . c  o m
            saveFileOnDisk(fileToSave);
            savedFiles.put(inputFile.getFilename(), fileToSave);
        } catch (IOException ex) {
            throw new IllegalStateException(
                    String.format("Unable to save the swagger spec contents on the disk at %s", filePathToSave),
                    ex);
        }
    }
    return savedFiles.build();
}

From source file:com.google.googlejavaformat.intellij.FormatterUtil.java

static Map<TextRange, String> getReplacements(Formatter formatter, String text, Collection<TextRange> ranges) {
    try {/*from  w w w .ja va2s . com*/
        ImmutableMap.Builder<TextRange, String> replacements = ImmutableMap.builder();
        formatter.getFormatReplacements(text, toRanges(ranges)).forEach(replacement -> replacements
                .put(toTextRange(replacement.getReplaceRange()), replacement.getReplacementString()));
        return replacements.build();
    } catch (FormatterException e) {
        return ImmutableMap.of();
    }
}

From source file:nextmethod.web.razor.tokenizer.JavaKeywordDetector.java

private static Map<String, JavaKeyword> createKeywordsMap() {
    final ImmutableMap.Builder<String, JavaKeyword> builder = ImmutableMap.<String, JavaKeyword>builder();

    for (JavaKeyword keyword : JavaKeyword.values()) {
        builder.put(keyword.keyword(), keyword);
    }// ww  w  .  ja v a2  s  .c  o m

    return builder.build();
}

From source file:com.preferanser.shared.util.GameUtils.java

public static Map<Hand, Set<Card>> copyDefensive(Multimap<Hand, Card> handCardMultimap) {
    ImmutableMap.Builder<Hand, Set<Card>> builder = ImmutableMap.builder();

    if (handCardMultimap.containsKey(Hand.EAST))
        builder.put(Hand.EAST, ImmutableSet.copyOf(handCardMultimap.get(Hand.EAST)));
    else//  w w w  . j a  va 2  s.  c  o  m
        builder.put(Hand.EAST, ImmutableSet.<Card>of());

    if (handCardMultimap.containsKey(Hand.SOUTH))
        builder.put(Hand.SOUTH, ImmutableSet.copyOf(handCardMultimap.get(Hand.SOUTH)));
    else
        builder.put(Hand.SOUTH, ImmutableSet.<Card>of());

    if (handCardMultimap.containsKey(Hand.WEST))
        builder.put(Hand.WEST, ImmutableSet.copyOf(handCardMultimap.get(Hand.WEST)));
    else
        builder.put(Hand.WEST, ImmutableSet.<Card>of());

    return builder.build();
}