KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > MessageSet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)MessageSet.java 1.4 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27 package com.sun.mail.imap.protocol;
28
29 import java.util.Vector JavaDoc;
30
31 /**
32  * This class holds the 'start' and 'end' for a range of messages
33  */

34 public class MessageSet {
35
36     public int start;
37     public int end;
38
39     public MessageSet() { }
40
41     public MessageSet(int start, int end) {
42     this.start = start;
43     this.end = end;
44     }
45
46     /**
47      * Count the total number of elements in a MessageSet
48      **/

49     public int size() {
50     return end - start + 1;
51     }
52
53     /*
54      * Convert an array of integers into an array of MessageSets
55      */

56     public static MessageSet[] createMessageSets(int[] msgs) {
57     Vector JavaDoc v = new Vector JavaDoc();
58     int i,j;
59
60     for (i=0; i < msgs.length; i++) {
61         MessageSet ms = new MessageSet();
62         ms.start = msgs[i];
63
64         // Look for contiguous elements
65
for (j=i+1; j < msgs.length; j++) {
66         if (msgs[j] != msgs[j-1] +1)
67             break;
68         }
69         ms.end = msgs[j-1];
70         v.addElement(ms);
71         i = j-1; // i gets incremented @ top of the loop
72
}
73     MessageSet[] msgsets = new MessageSet[v.size()];
74     v.copyInto(msgsets);
75     return msgsets;
76     }
77
78     /**
79      * Convert an array of MessageSets into an IMAP sequence range
80      */

81     public static String JavaDoc toString(MessageSet[] msgsets) {
82     if (msgsets == null || msgsets.length == 0) // Empty msgset
83
return null;
84
85     int i = 0; // msgset index
86
StringBuffer JavaDoc s = new StringBuffer JavaDoc();
87     int size = msgsets.length;
88     int start, end;
89
90     for (;;) {
91         start = msgsets[i].start;
92         end = msgsets[i].end;
93
94         if (end > start)
95         s.append(start).append(':').append(end);
96         else // end == start means only one element
97
s.append(start);
98     
99         i++; // Next MessageSet
100
if (i >= size) // No more MessageSets
101
break;
102         else
103         s.append(',');
104     }
105     return s.toString();
106     }
107
108     
109     /*
110      * Count the total number of elements in an array of MessageSets
111      */

112     public static int size(MessageSet[] msgsets) {
113     int count = 0;
114
115     if (msgsets == null) // Null msgset
116
return 0;
117
118     for (int i=0; i < msgsets.length; i++)
119         count += msgsets[i].size();
120     
121     return count;
122     }
123 }
124
Popular Tags