KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > search > FlagTerm


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)FlagTerm.java 1.12 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.search;
29
30 import javax.mail.*;
31
32 /**
33  * This class implements comparisons for Message Flags.
34  *
35  * @author Bill Shannon
36  * @author John Mani
37  */

38 public final class FlagTerm extends SearchTerm JavaDoc {
39
40     /**
41      * Indicates whether to test for the presence or
42      * absence of the specified Flag. If <code>true</code>,
43      * then test whether all the specified flags are present, else
44      * test whether all the specified flags are absent.
45      *
46      * @serial
47      */

48     protected boolean set;
49
50     /**
51      * Flags object containing the flags to test.
52      *
53      * @serial
54      */

55     protected Flags flags;
56
57     private static final long serialVersionUID = -142991500302030647L;
58
59     /**
60      * Constructor.
61      *
62      * @param flags Flags object containing the flags to check for
63      * @param set the flag setting to check for
64      */

65     public FlagTerm(Flags flags, boolean set) {
66     this.flags = flags;
67     this.set = set;
68     }
69
70     /**
71      * Return the Flags to test.
72      */

73     public Flags getFlags() {
74     return (Flags)flags.clone();
75     }
76
77     /**
78      * Return true if testing whether the flags are set.
79      */

80     public boolean getTestSet() {
81     return set;
82     }
83
84     /**
85      * The comparison method.
86      *
87      * @param msg The flag comparison is applied to this Message
88      * @return true if the comparson succeeds, otherwise false.
89      */

90     public boolean match(Message msg) {
91
92     try {
93         Flags f = msg.getFlags();
94         if (set) { // This is easy
95
if (f.contains(flags))
96             return true;
97         else
98             return false;
99         }
100
101         // Return true if ALL flags in the passed in Flags
102
// object are NOT set in this Message.
103

104         // Got to do this the hard way ...
105
Flags.Flag[] sf = flags.getSystemFlags();
106
107         // Check each flag in the passed in Flags object
108
for (int i = 0; i < sf.length; i++) {
109         if (f.contains(sf[i]))
110             // this flag IS set in this Message, get out.
111
return false;
112         }
113
114         String JavaDoc[] s = flags.getUserFlags();
115
116         // Check each flag in the passed in Flags object
117
for (int i = 0; i < s.length; i++) {
118         if (f.contains(s[i]))
119             // this flag IS set in this Message, get out.
120
return false;
121         }
122
123         return true;
124
125     } catch (Exception JavaDoc e) {
126         return false;
127     }
128     }
129
130     /**
131      * Equality comparison.
132      */

133     public boolean equals(Object JavaDoc obj) {
134     if (!(obj instanceof FlagTerm JavaDoc))
135         return false;
136     FlagTerm JavaDoc ft = (FlagTerm JavaDoc)obj;
137     return ft.set == this.set && ft.flags.equals(this.flags);
138     }
139
140     /**
141      * Compute a hashCode for this object.
142      */

143     public int hashCode() {
144     return set ? flags.hashCode() : ~flags.hashCode();
145     }
146 }
147
Popular Tags