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    
021    package org.apache.mailet.base;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    
026    import javax.mail.MessagingException;
027    
028    import org.apache.mailet.Mail;
029    import org.apache.mailet.MailAddress;
030    import org.apache.mailet.Matcher;
031    import org.apache.mailet.MatcherConfig;
032    
033    /**
034     * This class can be used as a wrapper for getting the "not matched" recipients
035     * 
036     */
037    public class MatcherInverter implements Matcher {
038    
039        private Matcher wrappedMatcher;
040    
041        public MatcherInverter(Matcher wrappedMatcher) {
042            this.wrappedMatcher = wrappedMatcher;
043        }
044    
045        /**
046         * @see org.apache.mailet.Matcher#destroy()
047         */
048        public void destroy() {
049            wrappedMatcher.destroy();
050        }
051    
052        /**
053         * @see org.apache.mailet.Matcher#getMatcherConfig()
054         */
055        public MatcherConfig getMatcherConfig() {
056            return wrappedMatcher.getMatcherConfig();
057        }
058    
059        /**
060         * @see org.apache.mailet.Matcher#getMatcherInfo()
061         */
062        public String getMatcherInfo() {
063            return wrappedMatcher.getMatcherInfo();
064        }
065    
066        /**
067         * @see org.apache.mailet.Matcher#destroy()
068         */
069        public void init(MatcherConfig config) throws MessagingException {
070            wrappedMatcher.init(config);
071        }
072    
073        /**
074         * Return a Collection of "not matched" recipients
075         *
076         */
077        public Collection<MailAddress> match(Mail mail) throws MessagingException {
078            // Create a new recipient Collection cause mail.getRecipients() give a reference to the internal 
079            // list of recipients. If we make changes there the original collection whould be corrupted
080            Collection<MailAddress> recipients = new ArrayList<MailAddress>(mail.getRecipients());
081            Collection<MailAddress> matchedRcpts = wrappedMatcher.match(mail);
082            
083            // check if a only a part of the recipients matched
084            if (matchedRcpts != null) {
085                recipients.removeAll(matchedRcpts);
086                if (recipients.isEmpty()) {
087                    return null;
088                }
089            }
090           
091            return recipients;
092        }
093    
094    }