Change ImapUtility to use 'long' for the values of sequence sets

This commit is contained in:
cketti 2012-07-07 16:21:07 +02:00
parent a37c95b456
commit 2ad748fad7
2 changed files with 55 additions and 13 deletions

View file

@ -54,11 +54,8 @@ public class ImapUtility {
for (String item : setItems) {
if (item.indexOf(':') == -1) {
// simple item
try {
Integer.parseInt(item); // Don't need the value; just ensure it's valid
if (isNumberValid(item)) {
list.add(item);
} catch (NumberFormatException e) {
Log.d(K9.LOG_TAG, "Invalid UID value", e);
}
} else {
// range
@ -91,23 +88,46 @@ public class ImapUtility {
if (range != null) {
int colonPos = range.indexOf(':');
if (colonPos > 0) {
int first = Integer.parseInt(range.substring(0, colonPos));
int second = Integer.parseInt(range.substring(colonPos + 1));
if (first < second) {
for (int i = first; i <= second; i++) {
list.add(Integer.toString(i));
long first = Long.parseLong(range.substring(0, colonPos));
long second = Long.parseLong(range.substring(colonPos + 1));
if (is32bitValue(first) && is32bitValue(second)) {
if (first < second) {
for (long i = first; i <= second; i++) {
list.add(Long.toString(i));
}
} else {
for (long i = first; i >= second; i--) {
list.add(Long.toString(i));
}
}
} else {
for (int i = first; i >= second; i--) {
list.add(Integer.toString(i));
}
Log.d(K9.LOG_TAG, "Invalid range: " + range);
}
}
}
} catch (NumberFormatException e) {
Log.d(K9.LOG_TAG, "Invalid range value", e);
Log.d(K9.LOG_TAG, "Invalid range value: " + range, e);
}
return list;
}
private static boolean isNumberValid(String number) {
try {
long value = Long.parseLong(number);
if (is32bitValue(value)) {
return true;
}
} catch (NumberFormatException e) {
// do nothing
}
Log.d(K9.LOG_TAG, "Invalid UID value: " + number);
return false;
}
private static boolean is32bitValue(long value) {
return ((value & ~0xFFFFFFFFL) == 0L);
}
}

View file

@ -34,6 +34,14 @@ public class ImapUtilityTest extends TestCase {
actual = ImapUtility.getImapSequenceValues("1");
MoreAsserts.assertEquals(expected, actual.toArray());
expected = new String[] {"2147483648"}; // Integer.MAX_VALUE + 1
actual = ImapUtility.getImapSequenceValues("2147483648");
MoreAsserts.assertEquals(expected, actual.toArray());
expected = new String[] {"4294967295"}; // 2^32 - 1
actual = ImapUtility.getImapSequenceValues("4294967295");
MoreAsserts.assertEquals(expected, actual.toArray());
expected = new String[] {"1", "3", "2"};
actual = ImapUtility.getImapSequenceValues("1,3,2");
MoreAsserts.assertEquals(expected, actual.toArray());
@ -50,6 +58,11 @@ public class ImapUtilityTest extends TestCase {
actual = ImapUtility.getImapSequenceValues("1,2:4,9:7");
MoreAsserts.assertEquals(expected, actual.toArray());
// Test numbers larger than Integer.MAX_VALUE (2147483647)
expected = new String[] {"2147483646", "2147483647", "2147483648"};
actual = ImapUtility.getImapSequenceValues("2147483646:2147483648");
MoreAsserts.assertEquals(expected, actual.toArray());
// Test partially invalid sets
expected = new String[] { "1", "5" };
actual = ImapUtility.getImapSequenceValues("1,x,5");
@ -75,6 +88,15 @@ public class ImapUtilityTest extends TestCase {
expected = new String[0];
actual = ImapUtility.getImapSequenceValues("1:x");
MoreAsserts.assertEquals(expected, actual.toArray());
// Test values larger than 2^32 - 1
expected = new String[0];
actual = ImapUtility.getImapSequenceValues("4294967296:4294967297");
MoreAsserts.assertEquals(expected, actual.toArray());
expected = new String[0];
actual = ImapUtility.getImapSequenceValues("4294967296"); // 2^32
MoreAsserts.assertEquals(expected, actual.toArray());
}
/**