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 javax.mail.MessagingException;
024    
025    import org.apache.mailet.Mail;
026    import org.apache.mailet.MailAddress;
027    
028    import java.util.Collection;
029    import java.util.Iterator;
030    import java.util.Vector;
031    
032    /**
033     * GenericMatcher makes writing recipient based matchers easier. It provides
034     * simple versions of the lifecycle methods init and destroy and of the methods
035     * in the MatcherConfig interface. GenericMatcher also implements the log method,
036     * declared in the MatcherContext interface.
037     *
038     * @version 1.0.0, 24/04/1999
039     */
040    public abstract class GenericRecipientMatcher extends GenericMatcher {
041    
042        /**
043         * Matches each recipient one by one through matchRecipient(MailAddress
044         * recipient) method.  Handles splitting the recipients Collection
045         * as appropriate.
046         *
047         * @param mail - the message and routing information to determine whether to match
048         * @return Collection the Collection of MailAddress objects that have been matched
049         */
050        public final Collection<MailAddress> match(Mail mail) throws MessagingException {
051            Collection<MailAddress> matching = new Vector<MailAddress>();
052            for (MailAddress rec : mail.getRecipients()) {
053                if (matchRecipient(rec)) {
054                    matching.add(rec);
055                }
056            }
057            return matching;
058        }
059    
060        /**
061         * Simple check to match exclusively on the email address (not
062         * message information).
063         *
064         * @param recipient - the address to determine whether to match
065         * @return boolean whether the recipient is a match
066         */
067        public abstract boolean matchRecipient(MailAddress recipient) throws MessagingException;
068    }