Search in sources :

Example 26 with Splitter

use of com.google.common.base.Splitter in project symja_android_library by axkr.

the class StringMapFunctions method tokenizeAndSort.

/**
 * Splits on Whitespace and returns the lexicographically sorted result.
 *
 * @return a {@link StringColumn}
 */
default StringColumn tokenizeAndSort() {
    StringColumn newColumn = StringColumn.create(name() + "[sorted]", this.size());
    for (int r = 0; r < size(); r++) {
        String value = getString(r);
        Splitter splitter = Splitter.on(CharMatcher.whitespace());
        splitter = splitter.trimResults();
        splitter = splitter.omitEmptyStrings();
        List<String> tokens = new ArrayList<>(splitter.splitToList(value));
        Collections.sort(tokens);
        value = String.join(" ", tokens);
        newColumn.set(r, value);
    }
    return newColumn;
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Splitter(com.google.common.base.Splitter) ArrayList(java.util.ArrayList)

Example 27 with Splitter

use of com.google.common.base.Splitter in project atlas by alibaba.

the class AtlasSymbolIo method exportToJava.

/**
 * Exports a symbol table to a java {@code R} class source. This method will create the source
 * file and any necessary directories. For example, if the package is {@code a.b} and the class
 * name is {@code RR}, this method will generate a file called {@code RR.java} in directory
 * {@code directory/a/b} creating directories {@code a} and {@code b} if necessary.
 *
 * @param table the table to export
 * @param directory the directory where the R source should be generated
 * @param finalIds should the generated IDs be final?
 * @return the generated file
 * @throws UncheckedIOException failed to generate the source
 */
@NonNull
public static File exportToJava(@NonNull SymbolTable table, @NonNull File directory, boolean finalIds) {
    Preconditions.checkArgument(directory.isDirectory());
    /*
         * Build the path to the class file, creating any needed directories.
         */
    Splitter splitter = Splitter.on('.');
    Iterable<String> directories = splitter.split(table.getTablePackage());
    File file = directory;
    for (String d : directories) {
        file = new File(file, d);
    }
    FileUtils.mkdirs(file);
    file = new File(file, SdkConstants.R_CLASS + SdkConstants.DOT_JAVA);
    String idModifiers = finalIds ? "public static final" : "public static";
    try (PrintWriter pw = new PrintWriter(new BufferedOutputStream(Files.newOutputStream(file.toPath())))) {
        pw.println("/* AUTO-GENERATED FILE.  DO NOT MODIFY.");
        pw.println(" *");
        pw.println(" * This class was automatically generated by the");
        pw.println(" * gradle plugin from the resource data it found. It");
        pw.println(" * should not be modified by hand.");
        pw.println(" */");
        if (!table.getTablePackage().isEmpty()) {
            pw.print("package ");
            pw.print(table.getTablePackage());
            pw.print(';');
            pw.println();
        }
        pw.println();
        pw.println("public final class R {");
        final String typeName = SymbolJavaType.INT.getTypeName();
        // loop on the resource types so that the order is always the same
        for (ResourceType resType : ResourceType.values()) {
            List<Symbol> symbols = getSymbolByResourceType(table, resType);
            if (symbols.isEmpty()) {
                continue;
            }
            pw.print("    public static final class ");
            pw.print(resType.getName());
            pw.print(" {");
            pw.println();
            for (Symbol s : symbols) {
                final String name = s.getName();
                pw.print("        ");
                pw.print(idModifiers);
                pw.print(' ');
                pw.print(s.getJavaType().getTypeName());
                pw.print(' ');
                pw.print(name);
                pw.print(" = ");
                pw.print(s.getValue());
                pw.print(';');
                pw.println();
                // listed in the children list.
                if (s.getJavaType() == SymbolJavaType.INT_LIST) {
                    Preconditions.checkArgument(s.getResourceType() == ResourceType.STYLEABLE, "Only resource type 'styleable'" + " is allowed to have java type 'int[]'");
                    List<String> children = s.getChildren();
                    for (int i = 0; i < children.size(); ++i) {
                        pw.print("        ");
                        pw.print(idModifiers);
                        pw.print(' ');
                        pw.print(typeName);
                        pw.print(' ');
                        pw.print(name);
                        pw.print('_');
                        pw.print(SymbolUtils.canonicalizeValueResourceName(children.get(i)));
                        pw.print(" = ");
                        pw.print(i);
                        pw.print(';');
                        pw.println();
                    }
                }
            }
            pw.print("    }");
            pw.println();
        }
        pw.print('}');
        pw.println();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return file;
}
Also used : Splitter(com.google.common.base.Splitter) ResourceType(com.android.resources.ResourceType) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter) NonNull(com.android.annotations.NonNull)

Example 28 with Splitter

use of com.google.common.base.Splitter in project guava by google.

the class FilesSimplifyPathTest method doExtensiveTest.

private void doExtensiveTest(String resourceName) throws IOException {
    Splitter splitter = Splitter.on(CharMatcher.whitespace());
    URL url = getClass().getResource(resourceName);
    for (String line : Resources.readLines(url, UTF_8)) {
        Iterator<String> iterator = splitter.split(line).iterator();
        String input = iterator.next();
        String expectedOutput = iterator.next();
        assertFalse(iterator.hasNext());
        assertEquals(expectedOutput, simplifyPath(input));
    }
}
Also used : Splitter(com.google.common.base.Splitter) URL(java.net.URL)

Example 29 with Splitter

use of com.google.common.base.Splitter in project grpc-java by grpc.

the class XdsClient method canonifyResourceName.

static String canonifyResourceName(String resourceName) {
    checkNotNull(resourceName, "resourceName");
    if (!resourceName.startsWith(XDSTP_SCHEME)) {
        return resourceName;
    }
    URI uri = URI.create(resourceName);
    String rawQuery = uri.getRawQuery();
    Splitter ampSplitter = Splitter.on('&').omitEmptyStrings();
    if (rawQuery == null) {
        return resourceName;
    }
    List<String> queries = ampSplitter.splitToList(rawQuery);
    if (queries.size() < 2) {
        return resourceName;
    }
    List<String> canonicalContextParams = new ArrayList<>(queries.size());
    for (String query : queries) {
        canonicalContextParams.add(query);
    }
    Collections.sort(canonicalContextParams);
    String canonifiedQuery = Joiner.on('&').join(canonicalContextParams);
    return resourceName.replace(rawQuery, canonifiedQuery);
}
Also used : Splitter(com.google.common.base.Splitter) ArrayList(java.util.ArrayList) URI(java.net.URI)

Example 30 with Splitter

use of com.google.common.base.Splitter in project zm-mailbox by Zimbra.

the class SmtpConnection method readHelloReplies.

/**
 * Reads replies to {@code EHLO} or {@code HELO}.
 * <p>
 * Returns the reply code.  Handles multiple {@code 250} responses.
 *
 * @param command the command name
 * @param firstReply the first reply line returned from sending {@code EHLO} or {@code HELO}
 */
private int readHelloReplies(String command, Reply firstReply) throws IOException {
    Reply reply = firstReply;
    int line = 1;
    serverExtensions.clear();
    serverAuthMechanisms.clear();
    while (true) {
        if (reply.text == null) {
            throw new CommandFailedException(command, "Invalid server response at line " + line + ": " + reply);
        }
        if (reply.code != 250) {
            return reply.code;
        }
        if (line > 1) {
            // Parse server extensions.
            Matcher matcher = PAT_EXTENSION.matcher(reply.text);
            if (matcher.matches()) {
                String extName = matcher.group(1).toUpperCase();
                serverExtensions.add(extName);
                if (extName.equals(AUTH)) {
                    // Parse auth mechanisms.
                    Splitter splitter = Splitter.on(CharMatcher.whitespace()).trimResults().omitEmptyStrings();
                    for (String mechanism : splitter.split(matcher.group(2))) {
                        serverAuthMechanisms.add(mechanism.toUpperCase());
                    }
                }
            }
        }
        if (reply.last) {
            // Last 250 response.
            mailIn.trace();
            return 250;
        } else {
            // Multiple response lines.
            reply = Reply.parse(mailIn.readLine());
        }
        line++;
    }
}
Also used : Splitter(com.google.common.base.Splitter) Matcher(java.util.regex.Matcher) CharMatcher(com.google.common.base.CharMatcher) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException)

Aggregations

Splitter (com.google.common.base.Splitter)86 ArrayList (java.util.ArrayList)19 IOException (java.io.IOException)11 HashSet (java.util.HashSet)10 File (java.io.File)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)5 BufferedReader (java.io.BufferedReader)4 NonNull (com.android.annotations.NonNull)3 URI (java.net.URI)3 URL (java.net.URL)3 ItemStack (net.minecraft.item.ItemStack)3 StringColumn (tech.tablesaw.api.StringColumn)3 CharMatcher (com.google.common.base.CharMatcher)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 CharSource (com.google.common.io.CharSource)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2