forked from thunderbird/thunderbird-android
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request thunderbird#2924 from k9mail/imap_refactoring_refa…
…ctored 'Refactor IMAP commands' refactored
- Loading branch information
Showing
18 changed files
with
884 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/IdGrouper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.fsck.k9.mail.store.imap; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
|
||
class IdGrouper { | ||
static GroupedIds groupIds(Set<Long> ids) { | ||
if (ids == null || ids.isEmpty()) { | ||
throw new IllegalArgumentException("groupId() must be called with non-empty set of ids"); | ||
} | ||
|
||
if (ids.size() < 2) { | ||
return new GroupedIds(ids, Collections.<ContiguousIdGroup>emptyList()); | ||
} | ||
|
||
TreeSet<Long> orderedIds = new TreeSet<Long>(ids); | ||
Iterator<Long> orderedIdIterator = orderedIds.iterator(); | ||
Long previousId = orderedIdIterator.next(); | ||
|
||
TreeSet<Long> remainingIds = new TreeSet<Long>(); | ||
remainingIds.add(previousId); | ||
List<ContiguousIdGroup> idGroups = new ArrayList<>(); | ||
long currentIdGroupStart = -1L; | ||
long currentIdGroupEnd = -1L; | ||
while (orderedIdIterator.hasNext()) { | ||
Long currentId = orderedIdIterator.next(); | ||
if (previousId + 1L == currentId) { | ||
if (currentIdGroupStart == -1L) { | ||
remainingIds.remove(previousId); | ||
currentIdGroupStart = previousId; | ||
currentIdGroupEnd = currentId; | ||
} else { | ||
currentIdGroupEnd = currentId; | ||
} | ||
} else { | ||
if (currentIdGroupStart != -1L) { | ||
idGroups.add(new ContiguousIdGroup(currentIdGroupStart, currentIdGroupEnd)); | ||
currentIdGroupStart = -1L; | ||
} | ||
remainingIds.add(currentId); | ||
} | ||
|
||
previousId = currentId; | ||
} | ||
|
||
if (currentIdGroupStart != -1L) { | ||
idGroups.add(new ContiguousIdGroup(currentIdGroupStart, currentIdGroupEnd)); | ||
} | ||
|
||
return new GroupedIds(remainingIds, idGroups); | ||
} | ||
|
||
|
||
static class GroupedIds { | ||
public final Set<Long> ids; | ||
public final List<ContiguousIdGroup> idGroups; | ||
|
||
|
||
GroupedIds(Set<Long> ids, List<ContiguousIdGroup> idGroups) { | ||
if (ids.isEmpty() && idGroups.isEmpty()) { | ||
throw new IllegalArgumentException("Must have at least one id"); | ||
} | ||
|
||
this.ids = ids; | ||
this.idGroups = idGroups; | ||
} | ||
} | ||
|
||
static class ContiguousIdGroup { | ||
public final long start; | ||
public final long end; | ||
|
||
|
||
ContiguousIdGroup(long start, long end) { | ||
if (start >= end) { | ||
throw new IllegalArgumentException("start >= end"); | ||
} | ||
|
||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return start + ":" + end; | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapCommandSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.fsck.k9.mail.store.imap; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
import com.fsck.k9.mail.store.imap.IdGrouper.ContiguousIdGroup; | ||
import com.fsck.k9.mail.store.imap.IdGrouper.GroupedIds; | ||
|
||
|
||
class ImapCommandSplitter { | ||
static List<String> splitCommand(String prefix, String suffix, GroupedIds groupedIds, int lengthLimit) { | ||
List<String> commands = new ArrayList<>(); | ||
Set<Long> workingIdSet = new TreeSet<>(groupedIds.ids); | ||
List<ContiguousIdGroup> workingIdGroups = new ArrayList<>(groupedIds.idGroups); | ||
|
||
int suffixLength = suffix.length(); | ||
int staticCommandLength = prefix.length() + suffixLength + 2; | ||
while (!workingIdSet.isEmpty() || !workingIdGroups.isEmpty()) { | ||
StringBuilder commandBuilder = new StringBuilder(prefix).append(' '); | ||
int length = staticCommandLength; | ||
while (length < lengthLimit) { | ||
if (!workingIdSet.isEmpty()) { | ||
Long id = workingIdSet.iterator().next(); | ||
String idString = Long.toString(id); | ||
|
||
length += idString.length() + 1; | ||
if (length >= lengthLimit) { | ||
break; | ||
} | ||
|
||
commandBuilder.append(idString).append(','); | ||
workingIdSet.remove(id); | ||
} else if (!workingIdGroups.isEmpty()) { | ||
ContiguousIdGroup idGroup = workingIdGroups.iterator().next(); | ||
String idGroupString = idGroup.toString(); | ||
|
||
length += idGroupString.length() + 1; | ||
if (length >= lengthLimit) { | ||
break; | ||
} | ||
|
||
commandBuilder.append(idGroupString).append(','); | ||
workingIdGroups.remove(idGroup); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if (suffixLength != 0) { | ||
// Replace the last comma with a space | ||
commandBuilder.setCharAt(commandBuilder.length() - 1, ' '); | ||
commandBuilder.append(suffix); | ||
} else { | ||
// Remove last comma | ||
commandBuilder.setLength(commandBuilder.length() - 1); | ||
} | ||
|
||
String command = commandBuilder.toString(); | ||
commands.add(command); | ||
} | ||
|
||
return commands; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.