KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > Flags


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.io.IOException JavaDoc;
39 import java.io.ObjectInputStream JavaDoc;
40 import java.io.ObjectOutputStream JavaDoc;
41
42 /**
43  * Flags of a message. The Flags are stored
44  * as an int value.
45  *
46  * @author Timo Stich <tstich@users.sourceforge.net>
47  */

48 public class Flags implements Saveable, Cloneable JavaDoc {
49
50     
51     /**
52      * Flag
53      */

54     public static final short ANSWERED = 0x0001;
55     /**
56      * Flag
57      */

58     public static final short DRAFT = 0x0004;
59     /**
60      * Flag
61      */

62     public static final short FLAGGED = 0x0008;
63     /**
64      * Flag
65      */

66     public static final short RECENT = 0x0010;
67     /**
68      * Flag
69      */

70     public static final short SEEN = 0x0020;
71     /**
72      * Flag
73      */

74     public static final short DELETED = 0x0040;
75
76     private short flags;
77
78     /**
79      * Constructs the Flags.
80      */

81     public Flags() {
82         flags = 0;
83     }
84
85     /**
86      * Constructs the Flags.
87      *
88      * @param flags
89      */

90     public Flags(short flags) {
91         this.flags = flags;
92     }
93
94     /**
95      * Constructs the Flags.
96      *
97      * @param in
98      * @throws IOException
99      */

100     public Flags(ObjectInputStream JavaDoc in) throws IOException JavaDoc {
101         load(in);
102     }
103
104     /**
105      * Get the value of the Flag.
106      *
107      * @param mask the Flag to check (eg #ANSWERED)
108      * @return the value of the Flag
109      */

110     public boolean get(short mask) {
111         return (flags & mask) > 0;
112     }
113
114     /**
115      * Set the flag to the value.
116      *
117      * @param mask the Flag to set (eg #ANSWERED)
118      * @param value
119      */

120     public void set(short mask, boolean value) {
121         if (value) {
122             set(mask);
123         } else {
124             clear(mask);
125         }
126     }
127
128     /**
129      * Set the Flag to <code>true</code>
130      *
131      * @param mask the Flag to set (eg #ANSWERED)
132      */

133     public void set(short mask) {
134         flags |= mask;
135     }
136
137     /**
138      * Set the Flag to <code>false</code>
139      *
140      * @param mask mask the Flag to clear (eg #ANSWERED)
141      */

142     public void clear(short mask) {
143         flags &= 0x0ffffffff ^ mask;
144     }
145
146     /**
147      * Toggle the Flag.
148      *
149      * @param mask the Flag to toggle (eg #ANSWERED)
150      */

151     public void toggle(short mask) {
152         flags ^= mask;
153     }
154
155     /**
156      * @return the SEEN value
157      */

158     public boolean getSeen() {
159         return get(SEEN);
160     }
161
162     /**
163      * @return the RECENT value
164      */

165     public boolean getRecent() {
166         return get(RECENT);
167     }
168
169     /**
170      * @return the ANSWERED value
171      */

172     public boolean getAnswered() {
173         return get(ANSWERED);
174     }
175
176     /**
177      * @return the FLAGGED value
178      */

179     public boolean getFlagged() {
180         return get(FLAGGED);
181     }
182
183     /**
184      * @return the DELETED value
185      */

186     public boolean getDeleted() {
187         return get(DELETED);
188     }
189
190     /**
191      * @return the DRAFT value
192      */

193     public boolean getDraft() {
194         return get(DRAFT);
195     }
196
197     /**
198      * Set the SEEN Flag.
199      *
200      * @param b
201      */

202     public void setSeen(boolean b) {
203         if (b) {
204             set(SEEN);
205         } else {
206             clear(SEEN);
207         }
208     }
209
210     /**
211      * Set the RECENT value.
212      *
213      * @param b
214      */

215     public void setRecent(boolean b) {
216         if (b) {
217             set(RECENT);
218         } else {
219             clear(RECENT);
220         }
221     }
222
223     /**
224      * Set the ANSWERED value.
225      *
226      * @param b
227      */

228     public void setAnswered(boolean b) {
229         if (b) {
230             set(ANSWERED);
231         } else {
232             clear(ANSWERED);
233         }
234     }
235
236     /**
237      * Set the FLAGGED value.
238      *
239      * @param b
240      */

241     public void setFlagged(boolean b) {
242         if (b) {
243             set(FLAGGED);
244         } else {
245             clear(FLAGGED);
246         }
247     }
248
249     /**
250      * Set the DELTED value.
251      *
252      * @param b
253      */

254     public void setDeleted(boolean b) {
255         if (b) {
256             set(DELETED);
257         } else {
258             clear(DELETED);
259         }
260     }
261
262     /**
263      * Set the DRAFT value.
264      *
265      * @param b
266      */

267     public void setDraft(boolean b) {
268         if (b) {
269             set(DRAFT);
270         } else {
271             clear(DRAFT);
272         }
273     }
274
275     /**
276      * @see org.columba.ristretto.message.Saveable#load(java.io.ObjectInputStream)
277      */

278     public void load(ObjectInputStream JavaDoc in) throws IOException JavaDoc {
279         flags = in.readShort();
280     }
281
282     /**
283      * @see org.columba.ristretto.message.Saveable#save(java.io.ObjectOutputStream)
284      */

285     public void save(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
286         out.writeShort(flags);
287     }
288
289     /** {@inheritDoc} */
290     public Object JavaDoc clone() {
291         Flags clone;
292         try {
293             clone = (Flags) super.clone();
294             clone.flags = flags;
295             return clone;
296         } catch (CloneNotSupportedException JavaDoc e) {
297             throw new RuntimeException JavaDoc(e);
298         }
299     }
300
301     /**
302      * @return the flags as a short.
303      */

304     public short getFlags() {
305         return flags;
306     }
307
308     /**
309      * @param s the flags as a short.
310      */

311     public void setFlags(short s) {
312         flags = s;
313     }
314
315     /** {@inheritDoc} */
316     public boolean equals(Object JavaDoc obj) {
317         boolean isEqual = false;
318         if ((obj != null) && (obj instanceof Flags)) {
319             Flags other = (Flags) obj;
320             isEqual = (flags == other.flags);
321         }
322         return isEqual;
323     }
324
325     /** {@inheritDoc} */
326     public int hashCode() {
327         return flags;
328     }
329
330     /** {@inheritDoc} */
331     public String JavaDoc toString() {
332         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
333         buffer.append("Flags[answered=");
334         buffer.append(getAnswered());
335         buffer.append(", draft=");
336         buffer.append(getDraft());
337         buffer.append(", expunged=");
338         buffer.append(getDeleted());
339         buffer.append(", flagged=");
340         buffer.append(getFlagged());
341         buffer.append(", recent=");
342         buffer.append(getRecent());
343         buffer.append(", seen=");
344         buffer.append(getSeen());
345         buffer.append("]");
346         return buffer.toString();
347     }
348 }
349
Popular Tags