1 19 20 package org.apache.james.imapserver.client; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 import javax.mail.internet.MimeMessage ; 27 28 public class MessageSet { 29 30 private final long from; 31 private final long to; 32 private boolean useUid=false; 33 private long[] uids; 34 private MimeMessage [] msgs; 35 private ArrayList selectedMessageNumbers; 36 37 public MessageSet(MimeMessage [] msgs,long[] uids,long from,long to) { 38 this(msgs,from,to); 39 this.uids=uids; 40 this.useUid=true; 41 } 42 public MessageSet(MimeMessage [] msgs,long[] uids,long number) { 43 this(msgs,number); 44 this.uids=uids; 45 this.useUid=true; 46 } 47 48 public MessageSet(MimeMessage [] msgs,long from,long to) { 49 this(from,to); 50 this.msgs=msgs; 51 } 52 public MessageSet(MimeMessage [] msgs,long number) { 53 this(number); 54 this.msgs=msgs; 55 } 56 public MessageSet(long from,long to) { 57 this.from=from; 58 this.to=to; 59 } 60 public MessageSet(long number) { 61 this.from=-1; 62 this.to=number; 63 } 64 65 public MessageSet(MimeMessage [] msgs, long from, long to, int[] numbers, long[] uids, boolean useUid) { 66 if (useUid) { 67 from=uids[(int)from-1]; 68 to=uids[(int)to-1]; 69 } 70 this.msgs=msgs; 71 this.from=from; 72 this.to=to; 73 this.uids=uids; 74 this.useUid=useUid; 75 selectedMessageNumbers=new ArrayList (numbers.length); 76 for (int i = 0; i < numbers.length; i++) { 77 selectedMessageNumbers.add(new Integer (numbers[i])); 78 } 79 80 } 81 public String toString() { 82 String result=""; 83 if (from>0) { 84 result += from +":"; 85 } 86 if (to>0) { 87 result += to; 88 } else { 89 result += "*"; 90 } 91 return result; 92 } 93 94 public boolean isUid() { 95 return useUid; 96 } 97 98 public List getSelectedMessageNumbers() { 99 if (selectedMessageNumbers==null) { 100 selectedMessageNumbers=new ArrayList (); 101 if (isUid()) { 102 final long from; 103 if (this.from>0) { 104 from=this.from; 105 } else { 106 from=this.to; 107 } 108 final long to; 109 if (this.to>0) { 110 to=this.to; 111 } else { 112 to=Long.MAX_VALUE; 113 } 114 for (int i=0; i< msgs.length; i++) { 115 if (uids[i]>=from && uids[i]<=to) { 116 selectedMessageNumbers.add(new Integer ((int)i+1)); 117 } 118 } 119 120 } else { 121 final long from; 122 if (this.from > 0) { 123 from = this.from; 124 } else { 125 from = this.to; 126 } 127 128 final long to; 129 if (this.to > 0) { 130 if (this.to > msgs.length) { 131 to = msgs.length; 132 } else { 133 to = this.to; 134 } 135 } else { 136 to = msgs.length; 137 } 138 139 for (long i = from; i <= to; i++) { 140 selectedMessageNumbers.add(new Integer ((int)i)); 141 } 142 } 143 } 144 return selectedMessageNumbers; 145 } 146 public MimeMessage getMessage(int no) { 147 return msgs[no-1]; 148 } 149 public long getUid(int no) { 150 return uids[no-1]; 151 } 152 153 } 154 | Popular Tags |