Skip to contentPackage: MessageSet
MessageSet
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.protocol;
18:
19: import java.util.List;
20: import java.util.ArrayList;
21:
22: /**
23: * This class holds the 'start' and 'end' for a range of messages.
24: */
25: public class MessageSet {
26:
27: public int start;
28: public int end;
29:
30: public MessageSet() { }
31:
32: public MessageSet(int start, int end) {
33:         this.start = start;
34:         this.end = end;
35: }
36:
37: /**
38: * Count the total number of elements in a MessageSet
39: *
40: * @return        how many messages in this MessageSet
41: */
42: public int size() {
43:         return end - start + 1;
44: }
45:
46: /**
47: * Convert an array of integers into an array of MessageSets
48: *
49: * @param        msgs        the messages
50: * @return                array of MessageSet objects
51: */
52: public static MessageSet[] createMessageSets(int[] msgs) {
53:         List<MessageSet> v = new ArrayList<>();
54:         int i,j;
55:
56:•        for (i=0; i < msgs.length; i++) {
57:          MessageSet ms = new MessageSet();
58:          ms.start = msgs[i];
59:
60:          // Look for contiguous elements
61:•         for (j=i+1; j < msgs.length; j++) {
62:•                if (msgs[j] != msgs[j-1] +1)
63:                  break;
64:          }
65:          ms.end = msgs[j-1];
66:          v.add(ms);
67:          i = j-1; // i gets incremented @ top of the loop
68:         }
69:         return v.toArray(new MessageSet[v.size()]);        
70: }
71:
72: /**
73: * Convert an array of MessageSets into an IMAP sequence range
74: *
75: * @param        msgsets        the MessageSets
76: * @return                IMAP sequence string
77: */
78: public static String toString(MessageSet[] msgsets) {
79:•        if (msgsets == null || msgsets.length == 0) // Empty msgset
80:          return null;
81:
82:         int i = 0; // msgset index
83:         StringBuilder s = new StringBuilder();
84:         int size = msgsets.length;
85:         int start, end;
86:
87:         for (;;) {
88:          start = msgsets[i].start;
89:          end = msgsets[i].end;
90:
91:•         if (end > start)
92:                 s.append(start).append(':').append(end);
93:          else // end == start means only one element
94:                 s.append(start);
95:         
96:          i++; // Next MessageSet
97:•         if (i >= size) // No more MessageSets
98:                 break;
99:          else
100:                 s.append(',');
101:         }
102:         return s.toString();
103: }
104:
105:         
106: /*
107: * Count the total number of elements in an array of MessageSets
108: */
109: public static int size(MessageSet[] msgsets) {
110:         int count = 0;
111:
112:•        if (msgsets == null) // Null msgset
113:          return 0;
114:
115:•        for (int i=0; i < msgsets.length; i++)
116:          count += msgsets[i].size();
117:         
118:         return count;
119: }
120: }