Skip to content

Package: Utility$1

Utility$1

nameinstructionbranchcomplexitylinemethod
compare(Message, Message)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.imap;
18:
19: import jakarta.mail.Message;
20: import org.eclipse.angus.mail.imap.protocol.MessageSet;
21: import org.eclipse.angus.mail.imap.protocol.UIDSet;
22:
23: import java.util.ArrayList;
24: import java.util.Arrays;
25: import java.util.Comparator;
26: import java.util.List;
27:
28: /**
29: * Holder for some static utility methods.
30: *
31: * @author John Mani
32: * @author Bill Shannon
33: */
34:
35: public final class Utility {
36:
37: // Cannot be initialized
38: private Utility() {
39: }
40:
41: /**
42: * Run thru the given array of messages, apply the given
43: * Condition on each message and generate sets of contiguous
44: * sequence-numbers for the successful messages. If a message
45: * in the given array is found to be expunged, it is ignored.
46: *
47: * ASSERT: Since this method uses and returns message sequence
48: * numbers, you should use this method only when holding the
49: * messageCacheLock.
50: *
51: * @param msgs the messages
52: * @param cond the condition to check
53: * @return the MessageSet array
54: */
55: public static MessageSet[] toMessageSet(Message[] msgs, Condition cond) {
56: List<MessageSet> v = new ArrayList<>(1);
57: int current, next;
58:
59: IMAPMessage msg;
60: for (int i = 0; i < msgs.length; i++) {
61: msg = (IMAPMessage) msgs[i];
62: if (msg.isExpunged()) // expunged message, skip it
63: continue;
64:
65: current = msg.getSequenceNumber();
66: // Apply the condition. If it fails, skip it.
67: if ((cond != null) && !cond.test(msg))
68: continue;
69:
70: MessageSet set = new MessageSet();
71: set.start = current;
72:
73: // Look for contiguous sequence numbers
74: for (++i; i < msgs.length; i++) {
75: // get next message
76: msg = (IMAPMessage) msgs[i];
77:
78: if (msg.isExpunged()) // expunged message, skip it
79: continue;
80: next = msg.getSequenceNumber();
81:
82: // Does this message match our condition ?
83: if ((cond != null) && !cond.test(msg))
84: continue;
85:
86: if (next == current + 1)
87: current = next;
88: else { // break in sequence
89: // We need to reexamine this message at the top of
90: // the outer loop, so decrement 'i' to cancel the
91: // outer loop's autoincrement
92: i--;
93: break;
94: }
95: }
96: set.end = current;
97: v.add(set);
98: }
99:
100: if (v.isEmpty()) // No valid messages
101: return null;
102: else {
103: return v.toArray(new MessageSet[0]);
104: }
105: }
106:
107: /**
108: * Sort (a copy of) the given array of messages and then
109: * run thru the sorted array of messages, apply the given
110: * Condition on each message and generate sets of contiguous
111: * sequence-numbers for the successful messages. If a message
112: * in the given array is found to be expunged, it is ignored.
113: *
114: * ASSERT: Since this method uses and returns message sequence
115: * numbers, you should use this method only when holding the
116: * messageCacheLock.
117: *
118: * @param msgs the messages
119: * @param cond the condition to check
120: * @return the MessageSet array
121: * @since JavaMail 1.5.4
122: */
123: public static MessageSet[] toMessageSetSorted(Message[] msgs,
124: Condition cond) {
125: /*
126: * XXX - This is quick and dirty. A more efficient strategy would be
127: * to generate an array of message numbers by applying the condition
128: * (with zero indicating the message doesn't satisfy the condition),
129: * sort it, and then convert it to a MessageSet skipping all the zeroes.
130: */
131: msgs = msgs.clone();
132: Arrays.sort(msgs,
133: new Comparator<Message>() {
134: @Override
135: public int compare(Message msg1, Message msg2) {
136: return msg1.getMessageNumber() - msg2.getMessageNumber();
137: }
138: });
139: return toMessageSet(msgs, cond);
140: }
141:
142: /**
143: * Return UIDSets for the messages. Note that the UIDs
144: * must have already been fetched for the messages.
145: *
146: * @param msgs the messages
147: * @return the UIDSet array
148: */
149: public static UIDSet[] toUIDSet(Message[] msgs) {
150: List<UIDSet> v = new ArrayList<>(1);
151: long current, next;
152:
153: IMAPMessage msg;
154: for (int i = 0; i < msgs.length; i++) {
155: msg = (IMAPMessage) msgs[i];
156: if (msg.isExpunged()) // expunged message, skip it
157: continue;
158:
159: current = msg.getUID();
160:
161: UIDSet set = new UIDSet();
162: set.start = current;
163:
164: // Look for contiguous UIDs
165: for (++i; i < msgs.length; i++) {
166: // get next message
167: msg = (IMAPMessage) msgs[i];
168:
169: if (msg.isExpunged()) // expunged message, skip it
170: continue;
171: next = msg.getUID();
172:
173: if (next == current + 1)
174: current = next;
175: else { // break in sequence
176: // We need to reexamine this message at the top of
177: // the outer loop, so decrement 'i' to cancel the
178: // outer loop's autoincrement
179: i--;
180: break;
181: }
182: }
183: set.end = current;
184: v.add(set);
185: }
186:
187: if (v.isEmpty()) // No valid messages
188: return null;
189: else {
190: return v.toArray(new UIDSet[0]);
191: }
192: }
193:
194: /**
195: * Make the ResyncData UIDSet available to IMAPProtocol,
196: * which is in a different package. Note that this class
197: * is not included in the public javadocs, thus "hiding"
198: * this method.
199: *
200: * @param rd the ResyncData
201: * @return the UIDSet array
202: * @since JavaMail 1.5.1
203: */
204: public static UIDSet[] getResyncUIDSet(ResyncData rd) {
205: return rd.getUIDSet();
206: }
207:
208: /**
209: * This interface defines the test to be executed in
210: * <code>toMessageSet()</code>.
211: */
212: public static interface Condition {
213: public boolean test(IMAPMessage message);
214: }
215: }