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