PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/dhanji/sitebricks
Java | 44 lines | 34 code | 5 blank | 5 comment | 12 complexity | b4a980affcfa3a130a5bb8788aaa2217 MD5 | raw file
 1package com.google.sitebricks.mail.imap;
 2
 3import java.util.List;
 4import java.util.regex.Matcher;
 5import java.util.regex.Pattern;
 6
 7/**
 8 * @author dhanji@gmail.com (Dhanji R. Prasanna)
 9 */
10class FolderStatusExtractor implements Extractor<FolderStatus> {
11  private static final Pattern PARENS = Pattern.compile("([(].*[)])");
12
13  @Override
14  public FolderStatus extract(List<String> messages) {
15    FolderStatus status = new FolderStatus();
16
17    // There should generally only be 1.
18    for (String message : messages) {
19      Matcher matcher = PARENS.matcher(message);
20      if (matcher.find()) {
21        String group = matcher.group(1);
22
23        // Strip parens.
24        group = group.substring(1, group.length() - 1);
25        String[] pieces = group.split("[ ]+");
26        for (int i = 0; i < pieces.length; i += 2) {
27          String piece = pieces[i].toUpperCase();
28          if ("MESSAGES".equals(piece)) {
29            status.setMessages(Integer.valueOf(pieces[i + 1]));
30          } else if ("UNSEEN".equals(piece)) {
31            status.setUnseen(Integer.valueOf(pieces[i + 1]));
32          } else if ("RECENT".equals(piece)) {
33            status.setRecent(Integer.valueOf(pieces[i + 1]));
34          } else if ("UIDNEXT".equals(piece)) {
35            status.setNextUid(Integer.valueOf(pieces[i + 1]));
36          } else if ("UIDVALIDITY".equals(piece)) {
37            status.setUidValidity(Integer.valueOf(pieces[i + 1]));
38          }
39        }
40      }
41    }
42    return status;
43  }
44}