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