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