KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > Utility


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  * @(#)Utility.java 1.6 05/11/17
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap;
29
30 import java.util.Vector JavaDoc;
31
32 import javax.mail.*;
33
34 import com.sun.mail.util.*;
35 import com.sun.mail.imap.protocol.MessageSet;
36 import com.sun.mail.imap.protocol.UIDSet;
37
38 /**
39  * Holder for some static utility methods.
40  *
41  * @version 1.6, 05/11/17
42  * @author John Mani
43  * @author Bill Shannon
44  */

45
46 public final class Utility {
47
48     // Cannot be initialized
49
private Utility() { }
50
51     /**
52      * Run thru the given array of messages, apply the given
53      * Condition on each message and generate sets of contiguous
54      * sequence-numbers for the successful messages. If a message
55      * in the given array is found to be expunged, it is ignored.
56      *
57      * ASSERT: Since this method uses and returns message sequence
58      * numbers, you should use this method only when holding the
59      * messageCacheLock.
60      */

61     public static
62     MessageSet[] toMessageSet(Message[] msgs, Condition cond) {
63     Vector JavaDoc v = new Vector JavaDoc(1);
64     int current, next;
65
66     IMAPMessage msg;
67     for (int i = 0; i < msgs.length; i++) {
68         msg = (IMAPMessage)msgs[i];
69         if (msg.isExpunged()) // expunged message, skip it
70
continue;
71
72         current = msg.getSequenceNumber();
73         // Apply the condition. If it fails, skip it.
74
if ((cond != null) && !cond.test(msg))
75         continue;
76         
77         MessageSet set = new MessageSet();
78         set.start = current;
79
80         // Look for contiguous sequence numbers
81
for (++i; i < msgs.length; i++) {
82         // get next message
83
msg = (IMAPMessage)msgs[i];
84
85         if (msg.isExpunged()) // expunged message, skip it
86
continue;
87         next = msg.getSequenceNumber();
88
89         // Does this message match our condition ?
90
if ((cond != null) && !cond.test(msg))
91             continue;
92         
93         if (next == current+1)
94             current = next;
95         else { // break in sequence
96
// We need to reexamine this message at the top of
97
// the outer loop, so decrement 'i' to cancel the
98
// outer loop's autoincrement
99
i--;
100             break;
101         }
102         }
103         set.end = current;
104         v.addElement(set);
105     }
106     
107     if (v.isEmpty()) // No valid messages
108
return null;
109     else {
110         MessageSet[] sets = new MessageSet[v.size()];
111         v.copyInto(sets);
112         return sets;
113     }
114     }
115
116     /**
117      * Return UIDSets for the messages. Note that the UIDs
118      * must have already been fetched for the messages.
119      */

120     public static UIDSet[] toUIDSet(Message[] msgs) {
121     Vector JavaDoc v = new Vector JavaDoc(1);
122     long current, next;
123
124     IMAPMessage msg;
125     for (int i = 0; i < msgs.length; i++) {
126         msg = (IMAPMessage)msgs[i];
127         if (msg.isExpunged()) // expunged message, skip it
128
continue;
129
130         current = msg.getUID();
131  
132         UIDSet set = new UIDSet();
133         set.start = current;
134
135         // Look for contiguous UIDs
136
for (++i; i < msgs.length; i++) {
137         // get next message
138
msg = (IMAPMessage)msgs[i];
139
140         if (msg.isExpunged()) // expunged message, skip it
141
continue;
142         next = msg.getUID();
143
144         if (next == current+1)
145             current = next;
146         else { // break in sequence
147
// We need to reexamine this message at the top of
148
// the outer loop, so decrement 'i' to cancel the
149
// outer loop's autoincrement
150
i--;
151             break;
152         }
153         }
154         set.end = current;
155         v.addElement(set);
156     }
157
158     if (v.isEmpty()) // No valid messages
159
return null;
160     else {
161         UIDSet[] sets = new UIDSet[v.size()];
162         v.copyInto(sets);
163         return sets;
164     }
165     }
166
167     /**
168      * This interface defines the test to be executed in
169      * <code>toMessageSet()</code>.
170      */

171     public static interface Condition {
172     public boolean test(IMAPMessage message);
173     }
174 }
175
Popular Tags