PageRenderTime 39ms CodeModel.GetById 35ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-mail/src/main/java/com/google/sitebricks/mail/imap/ListFoldersExtractor.java

http://github.com/dhanji/sitebricks
Java | 40 lines | 27 code | 8 blank | 5 comment | 4 complexity | 23f38ab1e3deaedbf1f25612b1065bda MD5 | raw file
 1package com.google.sitebricks.mail.imap;
 2
 3import com.google.common.collect.ImmutableList;
 4
 5import java.util.List;
 6import java.util.regex.Matcher;
 7import java.util.regex.Pattern;
 8
 9/**
10 * @author dhanji@gmail.com (Dhanji R. Prasanna)
11 */
12class ListFoldersExtractor implements Extractor<List<String>> {
13  private static final Pattern QUOTES = Pattern.compile("(\".*\")");
14  private static final String ROOT_PREFIX = "\"/\"";
15
16  @Override
17  public List<String> extract(List<String> messages) {
18    ImmutableList.Builder<String> builder = ImmutableList.builder();
19    for (String message : messages) {
20      Matcher matcher = QUOTES.matcher(message);
21      if (matcher.find()) {
22        String group = matcher.group(1);
23
24        if (group.startsWith(ROOT_PREFIX)) {
25          group = group.substring(ROOT_PREFIX.length()).trim();
26        }
27
28        // Strip quotes.
29        if (group.startsWith("\"")) {
30          group = group.substring(1, group.length() - 1);
31        }
32
33        // Generally remove leading "/" and stripquotes
34        builder.add(group);
35      }
36    }
37
38    return builder.build();
39  }
40}