001    /**
002     *
003     * Copyright 2003-2006 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.event;
019    
020    import javax.mail.Folder;
021    import javax.mail.Message;
022    
023    /**
024     * Event indicating a change in the number of messages in a folder.
025     *
026     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
027     */
028    public class MessageCountEvent extends MailEvent {
029        /**
030         * Messages were added to the folder.
031         */
032        public static final int ADDED = 1;
033    
034        /**
035         * Messages were removed from the folder.
036         */
037        public static final int REMOVED = 2;
038    
039        /**
040         * The affected messages.
041         */
042        protected transient Message msgs[];
043    
044        /**
045         * The event type.
046         */
047        protected int type;
048    
049        /**
050         * If true, then messages were expunged from the folder by this client
051         * and message numbers reflect the deletion; if false, then the change
052         * was the result of an expunge by a different client.
053         */
054        protected boolean removed;
055    
056        /**
057         * Construct a new event.
058         *
059         * @param folder   the folder containing the messages
060         * @param type     the event type
061         * @param removed  indicator of whether messages were expunged by this client
062         * @param messages the affected messages
063         */
064        public MessageCountEvent(Folder folder, int type, boolean removed, Message messages[]) {
065            super(folder);
066            this.msgs = messages;
067            this.type = type;
068            this.removed = removed;
069        }
070    
071        /**
072         * Return the event type.
073         *
074         * @return the event type
075         */
076        public int getType() {
077            return type;
078        }
079    
080        /**
081         * @return whether this event was the result of an expunge by this client
082         * @see MessageCountEvent#removed
083         */
084        public boolean isRemoved() {
085            return removed;
086        }
087    
088        /**
089         * Return the affected messages.
090         *
091         * @return the affected messages
092         */
093        public Message[] getMessages() {
094            return msgs;
095        }
096    
097        public void dispatch(Object listener) {
098            MessageCountListener l = (MessageCountListener) listener;
099            switch (type) {
100            case ADDED:
101                l.messagesAdded(this);
102                break;
103            case REMOVED:
104                l.messagesRemoved(this);
105                break;
106            default:
107                throw new IllegalArgumentException("Invalid type " + type);
108            }
109        }
110    }