KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > imap > MessageSetTokenizer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.imap;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23
24 /**
25  * Cuts a list/array of uids in pieces of certain size.
26  *
27  * @author fdietz
28  */

29 public class MessageSetTokenizer implements Iterator JavaDoc {
30     Object JavaDoc[] uids;
31     List JavaDoc list;
32     int index;
33     int sizeOfPieces;
34     int count;
35     int rest;
36
37     public MessageSetTokenizer(List JavaDoc l, int sizeOfPieces) {
38         this.uids = l.toArray();
39
40         this.sizeOfPieces = sizeOfPieces;
41
42         index = 0;
43
44         count = uids.length / sizeOfPieces;
45         rest = uids.length % sizeOfPieces;
46
47         list = new ArrayList JavaDoc(Arrays.asList(uids));
48     }
49
50     public MessageSetTokenizer(Object JavaDoc[] uids, int sizeOfPieces) {
51         this.uids = uids;
52         this.sizeOfPieces = sizeOfPieces;
53
54         index = 0;
55
56         count = uids.length / sizeOfPieces;
57         rest = uids.length % sizeOfPieces;
58
59         list = new ArrayList JavaDoc(Arrays.asList(uids));
60     }
61
62     /* (non-Javadoc)
63  * @see java.util.Iterator#hasNext()
64  */

65     public boolean hasNext() {
66         if (index < uids.length) {
67             return true;
68         }
69
70         return false;
71     }
72
73     /* (non-Javadoc)
74  * @see java.util.Iterator#next()
75  */

76     public Object JavaDoc next() {
77         int i = sizeOfPieces;
78
79         // calculate rest
80
if ((index + sizeOfPieces) > uids.length) {
81             i = uids.length - index;
82         }
83
84         List JavaDoc sublist = list.subList(index, index + i);
85
86         index = index + i;
87
88         return sublist;
89     }
90
91     /* (non-Javadoc)
92  * @see java.util.Iterator#remove()
93  */

94     public void remove() {
95         // not needed
96
}
97 }
98
Popular Tags