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 javax.mail.search;
21
22 import javax.mail.Flags;
23 import javax.mail.Message;
24 import javax.mail.MessagingException;
25
26 /**
27 * Term for matching message {@link Flags}.
28 *
29 * @version $Rev: 593593 $ $Date: 2007-11-09 12:04:20 -0500 (Fri, 09 Nov 2007) $
30 */
31 public final class FlagTerm extends SearchTerm {
32 /**
33 * If true, test that all flags are set; if false, test that all flags are clear.
34 */
35 protected boolean set;
36 /**
37 * The flags to test.
38 */
39 protected Flags flags;
40
41 /**
42 * @param flags the flags to test
43 * @param set test for set or clear; {@link #set}
44 */
45 public FlagTerm(Flags flags, boolean set) {
46 this.set = set;
47 this.flags = flags;
48 }
49
50 public Flags getFlags() {
51 return flags;
52 }
53
54 public boolean getTestSet() {
55 return set;
56 }
57
58 public boolean match(Message message) {
59 try {
60 Flags msgFlags = message.getFlags();
61 if (set) {
62 return msgFlags.contains(flags);
63 } else {
64 // yuk - I wish we could get at the internal state of the Flags
65 Flags.Flag[] system = flags.getSystemFlags();
66 for (int i = 0; i < system.length; i++) {
67 Flags.Flag flag = system[i];
68 if (msgFlags.contains(flag)) {
69 return false;
70 }
71 }
72 String[] user = flags.getUserFlags();
73 for (int i = 0; i < user.length; i++) {
74 String flag = user[i];
75 if (msgFlags.contains(flag)) {
76 return false;
77 }
78 }
79 return true;
80 }
81 } catch (MessagingException e) {
82 return false;
83 }
84 }
85
86 public boolean equals(Object other) {
87 if (other == this) return true;
88 if (other instanceof FlagTerm == false) return false;
89 final FlagTerm otherFlags = (FlagTerm) other;
90 return otherFlags.set == this.set && otherFlags.flags.equals(flags);
91 }
92
93 public int hashCode() {
94 return set ? flags.hashCode() : ~flags.hashCode();
95 }
96 }