KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > client > MessageSet


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.imapserver.client;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.mail.internet.MimeMessage JavaDoc;
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 JavaDoc[] msgs;
35     private ArrayList JavaDoc selectedMessageNumbers;
36
37     public MessageSet(MimeMessage JavaDoc[] 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 JavaDoc[] msgs,long[] uids,long number) {
43         this(msgs,number);
44         this.uids=uids;
45         this.useUid=true;
46     }
47     
48     public MessageSet(MimeMessage JavaDoc[] msgs,long from,long to) {
49         this(from,to);
50         this.msgs=msgs;
51     }
52     public MessageSet(MimeMessage JavaDoc[] 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 JavaDoc[] 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 JavaDoc(numbers.length);
76         for (int i = 0; i < numbers.length; i++) {
77             selectedMessageNumbers.add(new Integer JavaDoc(numbers[i]));
78         }
79
80     }
81     public String JavaDoc toString() {
82         String JavaDoc 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 JavaDoc getSelectedMessageNumbers() {
99         if (selectedMessageNumbers==null) {
100             selectedMessageNumbers=new ArrayList JavaDoc();
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 JavaDoc((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 JavaDoc((int)i));
141                 }
142             }
143         }
144         return selectedMessageNumbers;
145     }
146     public MimeMessage JavaDoc 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