001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package javax.mail.search; 019 020 import javax.mail.Flags; 021 import javax.mail.Message; 022 import javax.mail.MessagingException; 023 024 /** 025 * Term for matching message {@link Flags}. 026 * 027 * @version $Rev: 126550 $ $Date: 2005-01-26 14:27:45 -0800 (Wed, 26 Jan 2005) $ 028 */ 029 public final class FlagTerm extends SearchTerm { 030 /** 031 * If true, test that all flags are set; if false, test that all flags are clear. 032 */ 033 protected boolean set; 034 /** 035 * The flags to test. 036 */ 037 protected Flags flags; 038 039 /** 040 * @param flags the flags to test 041 * @param set test for set or clear; {@link #set} 042 */ 043 public FlagTerm(Flags flags, boolean set) { 044 this.set = set; 045 this.flags = flags; 046 } 047 048 public Flags getFlags() { 049 return flags; 050 } 051 052 public boolean getTestSet() { 053 return set; 054 } 055 056 public boolean match(Message message) { 057 try { 058 Flags msgFlags = message.getFlags(); 059 if (set) { 060 return msgFlags.contains(flags); 061 } else { 062 // yuk - I wish we could get at the internal state of the Flags 063 Flags.Flag[] system = flags.getSystemFlags(); 064 for (int i = 0; i < system.length; i++) { 065 Flags.Flag flag = system[i]; 066 if (msgFlags.contains(flag)) { 067 return false; 068 } 069 } 070 String[] user = flags.getUserFlags(); 071 for (int i = 0; i < user.length; i++) { 072 String flag = user[i]; 073 if (msgFlags.contains(flag)) { 074 return false; 075 } 076 } 077 return true; 078 } 079 } catch (MessagingException e) { 080 return false; 081 } 082 } 083 084 public boolean equals(Object other) { 085 if (other == this) return true; 086 if (other instanceof FlagTerm == false) return false; 087 final FlagTerm otherFlags = (FlagTerm) other; 088 return otherFlags.set == this.set && otherFlags.flags.equals(flags); 089 } 090 091 public int hashCode() { 092 return flags.hashCode(); 093 } 094 }