1 21 22 27 package com.sun.mail.imap.protocol; 28 29 import java.util.Vector ; 30 31 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 49 public int size() { 50 return end - start + 1; 51 } 52 53 56 public static MessageSet[] createMessageSets(int[] msgs) { 57 Vector v = new Vector (); 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 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; } 73 MessageSet[] msgsets = new MessageSet[v.size()]; 74 v.copyInto(msgsets); 75 return msgsets; 76 } 77 78 81 public static String toString(MessageSet[] msgsets) { 82 if (msgsets == null || msgsets.length == 0) return null; 84 85 int i = 0; StringBuffer s = new StringBuffer (); 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 s.append(start); 98 99 i++; if (i >= size) break; 102 else 103 s.append(','); 104 } 105 return s.toString(); 106 } 107 108 109 112 public static int size(MessageSet[] msgsets) { 113 int count = 0; 114 115 if (msgsets == null) 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 |