1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package javax.mail.event;
21
22 import javax.mail.Folder;
23 import javax.mail.Message;
24
25 /**
26 * Event indicating a change in the number of messages in a folder.
27 *
28 * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
29 */
30 public class MessageCountEvent extends MailEvent {
31 /**
32 * Messages were added to the folder.
33 */
34 public static final int ADDED = 1;
35
36 /**
37 * Messages were removed from the folder.
38 */
39 public static final int REMOVED = 2;
40
41 /**
42 * The affected messages.
43 */
44 protected transient Message msgs[];
45
46 /**
47 * The event type.
48 */
49 protected int type;
50
51 /**
52 * If true, then messages were expunged from the folder by this client
53 * and message numbers reflect the deletion; if false, then the change
54 * was the result of an expunge by a different client.
55 */
56 protected boolean removed;
57
58 /**
59 * Construct a new event.
60 *
61 * @param folder the folder containing the messages
62 * @param type the event type
63 * @param removed indicator of whether messages were expunged by this client
64 * @param messages the affected messages
65 */
66 public MessageCountEvent(Folder folder, int type, boolean removed, Message messages[]) {
67 super(folder);
68 this.msgs = messages;
69 this.type = type;
70 this.removed = removed;
71 }
72
73 /**
74 * Return the event type.
75 *
76 * @return the event type
77 */
78 public int getType() {
79 return type;
80 }
81
82 /**
83 * @return whether this event was the result of an expunge by this client
84 * @see MessageCountEvent#removed
85 */
86 public boolean isRemoved() {
87 return removed;
88 }
89
90 /**
91 * Return the affected messages.
92 *
93 * @return the affected messages
94 */
95 public Message[] getMessages() {
96 return msgs;
97 }
98
99 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 }