KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)UIDSet.java 1.1 05/11/17
3  *
4  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
7  *
8  */

9 package com.sun.mail.imap.protocol;
10
11 import java.util.Vector JavaDoc;
12
13 /**
14  * This class holds the 'start' and 'end' for a range of UIDs.
15  * Just like MessageSet except using long instead of int.
16  */

17 public class UIDSet {
18
19     public long start;
20     public long end;
21
22     public UIDSet() { }
23
24     public UIDSet(long start, long end) {
25     this.start = start;
26     this.end = end;
27     }
28
29     /**
30      * Count the total number of elements in a UIDSet
31      **/

32     public long size() {
33     return end - start + 1;
34     }
35
36     /*
37      * Convert an array of longs into an array of UIDSets
38      */

39     public static UIDSet[] createUIDSets(long[] msgs) {
40     Vector JavaDoc v = new Vector JavaDoc();
41     int i,j;
42
43     for (i=0; i < msgs.length; i++) {
44         UIDSet ms = new UIDSet();
45         ms.start = msgs[i];
46
47         // Look for contiguous elements
48
for (j=i+1; j < msgs.length; j++) {
49         if (msgs[j] != msgs[j-1] +1)
50             break;
51         }
52         ms.end = msgs[j-1];
53         v.addElement(ms);
54         i = j-1; // i gets incremented @ top of the loop
55
}
56     UIDSet[] msgsets = new UIDSet[v.size()];
57     v.copyInto(msgsets);
58     return msgsets;
59     }
60
61     /**
62      * Convert an array of UIDSets into an IMAP sequence range
63      */

64     public static String JavaDoc toString(UIDSet[] msgsets) {
65     if (msgsets == null || msgsets.length == 0) // Empty msgset
66
return null;
67
68     int i = 0; // msgset index
69
StringBuffer JavaDoc s = new StringBuffer JavaDoc();
70     int size = msgsets.length;
71     long start, end;
72
73     for (;;) {
74         start = msgsets[i].start;
75         end = msgsets[i].end;
76
77         if (end > start)
78         s.append(start).append(':').append(end);
79         else // end == start means only one element
80
s.append(start);
81     
82         i++; // Next UIDSet
83
if (i >= size) // No more UIDSets
84
break;
85         else
86         s.append(',');
87     }
88     return s.toString();
89     }
90
91     
92     /*
93      * Count the total number of elements in an array of UIDSets
94      */

95     public static long size(UIDSet[] msgsets) {
96     long count = 0;
97
98     if (msgsets == null) // Null msgset
99
return 0;
100
101     for (int i=0; i < msgsets.length; i++)
102         count += msgsets[i].size();
103     
104     return count;
105     }
106 }
107
Popular Tags