KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > SimpleFolder


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

17
18 package javax.mail;
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.List;
22 /**
23  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
24  */

25 public class SimpleFolder extends Folder {
26     private static final Message[] MESSAGE_ARRAY = new Message[0];
27     private List _messages = new LinkedList();
28     private String _name;
29     public SimpleFolder(Store store) {
30         this(store, "SimpleFolder");
31     }
32     SimpleFolder(Store store, String name) {
33         super(store);
34         _name = name;
35     }
36     /* (non-Javadoc)
37      * @see javax.mail.Folder#appendMessages(javax.mail.Message[])
38      */

39     public void appendMessages(Message[] messages) throws MessagingException {
40         for (int i = 0; i < messages.length; i++) {
41             Message message = messages[i];
42             _messages.add(message);
43         }
44     }
45     /* (non-Javadoc)
46      * @see javax.mail.Folder#close(boolean)
47      */

48     public void close(boolean expunge) throws MessagingException {
49         if (expunge) {
50             expunge();
51         }
52     }
53     /* (non-Javadoc)
54      * @see javax.mail.Folder#create(int)
55      */

56     public boolean create(int type) throws MessagingException {
57         if (type == HOLDS_MESSAGES) {
58             return true;
59         } else {
60             throw new MessagingException("Cannot create folders that hold folders");
61         }
62     }
63     /* (non-Javadoc)
64      * @see javax.mail.Folder#delete(boolean)
65      */

66     public boolean delete(boolean recurse) throws MessagingException {
67         _messages = new LinkedList();
68         return true;
69     }
70     /* (non-Javadoc)
71      * @see javax.mail.Folder#exists()
72      */

73     public boolean exists() throws MessagingException {
74         return true;
75     }
76     /* (non-Javadoc)
77      * @see javax.mail.Folder#expunge()
78      */

79     public Message[] expunge() throws MessagingException {
80         Iterator it = _messages.iterator();
81         List result = new LinkedList();
82         while (it.hasNext()) {
83             Message message = (Message) it.next();
84             if (message.isSet(Flags.Flag.DELETED)) {
85                 it.remove();
86                 result.add(message);
87             }
88         }
89         // run through and renumber the messages
90
for (int i = 0; i < _messages.size(); i++) {
91             Message message = (Message) _messages.get(i);
92             message.setMessageNumber(i);
93         }
94         return (Message[]) result.toArray(MESSAGE_ARRAY);
95     }
96     /* (non-Javadoc)
97      * @see javax.mail.Folder#getFolder(java.lang.String)
98      */

99     public Folder getFolder(String name) throws MessagingException {
100         return null;
101     }
102     /* (non-Javadoc)
103      * @see javax.mail.Folder#getFullName()
104      */

105     public String getFullName() {
106         return getName();
107     }
108     /* (non-Javadoc)
109      * @see javax.mail.Folder#getMessage(int)
110      */

111     public Message getMessage(int id) throws MessagingException {
112         return (Message) _messages.get(id);
113     }
114     /* (non-Javadoc)
115      * @see javax.mail.Folder#getMessageCount()
116      */

117     public int getMessageCount() throws MessagingException {
118         return _messages.size();
119     }
120     /* (non-Javadoc)
121      * @see javax.mail.Folder#getName()
122      */

123     public String getName() {
124         return _name;
125     }
126     /* (non-Javadoc)
127      * @see javax.mail.Folder#getParent()
128      */

129     public Folder getParent() throws MessagingException {
130         return null;
131     }
132     /* (non-Javadoc)
133      * @see javax.mail.Folder#getPermanentFlags()
134      */

135     public Flags getPermanentFlags() {
136         return null;
137     }
138     /* (non-Javadoc)
139      * @see javax.mail.Folder#getSeparator()
140      */

141     public char getSeparator() throws MessagingException {
142         return '/';
143     }
144     /* (non-Javadoc)
145      * @see javax.mail.Folder#getType()
146      */

147     public int getType() throws MessagingException {
148         return HOLDS_MESSAGES;
149     }
150     /* (non-Javadoc)
151      * @see javax.mail.Folder#hasNewMessages()
152      */

153     public boolean hasNewMessages() throws MessagingException {
154         return false;
155     }
156     /* (non-Javadoc)
157      * @see javax.mail.Folder#isOpen()
158      */

159     public boolean isOpen() {
160         return true;
161     }
162     /* (non-Javadoc)
163      * @see javax.mail.Folder#list(java.lang.String)
164      */

165     public Folder[] list(String pattern) throws MessagingException {
166         return null;
167     }
168     /* (non-Javadoc)
169      * @see javax.mail.Folder#open(int)
170      */

171     public void open(int mode) throws MessagingException {
172         if (mode != HOLDS_MESSAGES) {
173             throw new MessagingException("SimpleFolder can only be opened with HOLDS_MESSAGES");
174         }
175     }
176     /* (non-Javadoc)
177      * @see javax.mail.Folder#renameTo(javax.mail.Folder)
178      */

179     public boolean renameTo(Folder newName) throws MessagingException {
180         _name = newName.getName();
181         return true;
182     }
183 }
184
Popular Tags