1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)FlagTerm.java 1.13 07/05/04
39 */
40
41 package javax.mail.search;
42
43 import javax.mail;
44
45 /**
46 * This class implements comparisons for Message Flags.
47 *
48 * @author Bill Shannon
49 * @author John Mani
50 */
51 public final class FlagTerm extends SearchTerm {
52
53 /**
54 * Indicates whether to test for the presence or
55 * absence of the specified Flag. If <code>true</code>,
56 * then test whether all the specified flags are present, else
57 * test whether all the specified flags are absent.
58 *
59 * @serial
60 */
61 protected boolean set;
62
63 /**
64 * Flags object containing the flags to test.
65 *
66 * @serial
67 */
68 protected Flags flags;
69
70 private static final long serialVersionUID = -142991500302030647L;
71
72 /**
73 * Constructor.
74 *
75 * @param flags Flags object containing the flags to check for
76 * @param set the flag setting to check for
77 */
78 public FlagTerm(Flags flags, boolean set) {
79 this.flags = flags;
80 this.set = set;
81 }
82
83 /**
84 * Return the Flags to test.
85 */
86 public Flags getFlags() {
87 return (Flags)flags.clone();
88 }
89
90 /**
91 * Return true if testing whether the flags are set.
92 */
93 public boolean getTestSet() {
94 return set;
95 }
96
97 /**
98 * The comparison method.
99 *
100 * @param msg The flag comparison is applied to this Message
101 * @return true if the comparson succeeds, otherwise false.
102 */
103 public boolean match(Message msg) {
104
105 try {
106 Flags f = msg.getFlags();
107 if (set) { // This is easy
108 if (f.contains(flags))
109 return true;
110 else
111 return false;
112 }
113
114 // Return true if ALL flags in the passed in Flags
115 // object are NOT set in this Message.
116
117 // Got to do this the hard way ...
118 Flags.Flag[] sf = flags.getSystemFlags();
119
120 // Check each flag in the passed in Flags object
121 for (int i = 0; i < sf.length; i++) {
122 if (f.contains(sf[i]))
123 // this flag IS set in this Message, get out.
124 return false;
125 }
126
127 String[] s = flags.getUserFlags();
128
129 // Check each flag in the passed in Flags object
130 for (int i = 0; i < s.length; i++) {
131 if (f.contains(s[i]))
132 // this flag IS set in this Message, get out.
133 return false;
134 }
135
136 return true;
137
138 } catch (Exception e) {
139 return false;
140 }
141 }
142
143 /**
144 * Equality comparison.
145 */
146 public boolean equals(Object obj) {
147 if (!(obj instanceof FlagTerm))
148 return false;
149 FlagTerm ft = (FlagTerm)obj;
150 return ft.set == this.set && ft.flags.equals(this.flags);
151 }
152
153 /**
154 * Compute a hashCode for this object.
155 */
156 public int hashCode() {
157 return set ? flags.hashCode() : ~flags.hashCode();
158 }
159 }