james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1178048 - in /james/mailbox/trunk/api/src: main/java/org/apache/james/mailbox/MessageRange.java test/java/org/apache/james/mailbox/MessageRangeTest.java
Date Sat, 01 Oct 2011 17:40:40 GMT
Author: norman
Date: Sat Oct  1 17:40:40 2011
New Revision: 1178048

URL: http://svn.apache.org/viewvc?rev=1178048&view=rev
Log:
Add MessageRange.split(...) method which allows to split a MessageRange in a List of MessageRanges
with a max amount of items. See MAILBOX-146

Modified:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java
    james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/MessageRangeTest.java

Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java?rev=1178048&r1=1178047&r2=1178048&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java Sat Oct
 1 17:40:40 2011
@@ -43,7 +43,7 @@ public class MessageRange implements Ite
         RANGE
     }
 
-    private static final int NOT_A_UID = -1;
+    public static final int NOT_A_UID = -1;
 
 
     /**
@@ -261,4 +261,40 @@ public class MessageRange implements Ite
         }
         
     }
+    
+    
+    /**
+     * Tries to split the given {@link MessageRange} to a {@link List} of {@link MessageRange}'s
which 
+     * select only a max amount of items. This only work for {@link MessageRange}'s with
{@link Type} of 
+     * {@link Type#RANGE}.
+     * 
+     * @param range
+     * @param maxItems
+     * @return ranges
+     */
+    public List<MessageRange> split( int maxItems) {
+        List<MessageRange> ranges = new ArrayList<MessageRange>();
+        if (getType() == Type.RANGE) {
+            long from = getUidFrom();
+            long to = getUidTo();
+            long realTo = to;
+            while(from <= realTo) {
+                if (from + maxItems  -1 < realTo) {
+                    to = from + maxItems -1;
+                } else {
+                    to = realTo;
+                }
+                if (from == to) {
+                    ranges.add(MessageRange.one(from));
+                } else {
+                    ranges.add(MessageRange.range(from, to));
+                }
+                
+                from = to + 1;
+            }
+        } else {
+            ranges.add(this);
+        }
+        return ranges;
+    }
 }

Modified: james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/MessageRangeTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/MessageRangeTest.java?rev=1178048&r1=1178047&r2=1178048&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/MessageRangeTest.java (original)
+++ james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/MessageRangeTest.java Sat
Oct  1 17:40:40 2011
@@ -57,4 +57,37 @@ public class MessageRangeTest {
         assertEquals(from, range.getUidFrom());
         assertEquals(to, range.getUidTo());
     }
+    
+    @Test
+    public void testSplitOne() {
+        MessageRange one = MessageRange.one(1);
+        List<MessageRange> ranges = one.split(2);
+        assertEquals(1, ranges.size());
+        checkRange(1, 1, ranges.get(0));
+        assertEquals(MessageRange.Type.ONE, ranges.get(0).getType());
+    }
+    
+    @Test
+    public void testSplitFrom() {
+        MessageRange from = MessageRange.from(1);
+        List<MessageRange> ranges = from.split(2);
+        assertEquals(1, ranges.size());
+        checkRange(1, MessageRange.NOT_A_UID, ranges.get(0));
+        assertEquals(MessageRange.Type.FROM, ranges.get(0).getType());
+    }
+    
+    @Test
+    public void testSplitRange() {
+        MessageRange range = MessageRange.range(1,10);
+        List<MessageRange> ranges = range.split(3);
+        assertEquals(4, ranges.size());
+        checkRange(1, 3, ranges.get(0));
+        assertEquals(MessageRange.Type.RANGE, ranges.get(0).getType());
+        checkRange(4, 6, ranges.get(1));
+        assertEquals(MessageRange.Type.RANGE, ranges.get(1).getType());
+        checkRange(7, 9, ranges.get(2));
+        assertEquals(MessageRange.Type.RANGE, ranges.get(2).getType());
+        checkRange(10, 10, ranges.get(3));
+        assertEquals(MessageRange.Type.ONE, ranges.get(3).getType());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message