Skip to content

Method: dispatch(Object)

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.event;
18:
19: import java.util.*;
20: import jakarta.mail.*;
21:
22: /**
23: * This class notifies changes in the number of messages in a folder. <p>
24: *
25: * Note that some folder types may only deliver MessageCountEvents at
26: * certain times or after certain operations. IMAP in particular will
27: * only notify the client of MessageCountEvents when a client issues a
28: * new command. Refer to
29: * <A HREF="http://www.ietf.org/rfc/rfc3501.txt" TARGET="_top">RFC 3501</A>
30: * for details.
31: * A client may want to "poll" the folder by occasionally calling the
32: * {@link jakarta.mail.Folder#getMessageCount getMessageCount} or
33: * {@link jakarta.mail.Folder#isOpen isOpen} methods
34: * to solicit any such notifications.
35: *
36: * @author John Mani
37: */
38:
39: public class MessageCountEvent extends MailEvent {
40:
41: /** The messages were added to their folder */
42: public static final int ADDED                 = 1;
43: /** The messages were removed from their folder */
44: public static final int REMOVED                 = 2;
45:
46: /**
47: * The event type.
48: *
49: * @serial
50: */
51: protected int type;
52:
53: /**
54: * If true, this event is the result of an explicit
55: * expunge by this client, and the messages in this
56: * folder have been renumbered to account for this.
57: * If false, this event is the result of an expunge
58: * by external sources.
59: *
60: * @serial
61: */
62: protected boolean removed;
63:
64: /**
65: * The messages.
66: */
67: transient protected Message[] msgs;
68:
69: private static final long serialVersionUID = -7447022340837897369L;
70:
71: /**
72: * Constructor.
73: * @param folder         The containing folder
74: * @param type        The event type
75: * @param removed        If true, this event is the result of an explicit
76: *                        expunge by this client, and the messages in this
77: *                        folder have been renumbered to account for this.
78: *                        If false, this event is the result of an expunge
79: *                        by external sources.
80: *
81: * @param msgs        The messages added/removed
82: */
83: public MessageCountEvent(Folder folder, int type,
84:                          boolean removed, Message[] msgs) {
85:         super(folder);
86:         this.type = type;
87:         this.removed = removed;
88:         this.msgs = msgs;
89: }
90:
91: /**
92: * Return the type of this event.
93: * @return type
94: */
95: public int getType() {
96:         return type;
97: }
98:
99: /**
100: * Indicates whether this event is the result of an explicit
101: * expunge by this client, or due to an expunge from external
102: * sources. If <code>true</code>, this event is due to an
103: * explicit expunge and hence all remaining messages in this
104: * folder have been renumbered. If <code>false</code>, this event
105: * is due to an external expunge. <p>
106: *
107: * Note that this method is valid only if the type of this event
108: * is <code>REMOVED</code>
109: *
110: * @return        true if the message has been removed
111: */
112: public boolean isRemoved() {
113:         return removed;
114: }
115:
116: /**
117: * Return the array of messages added or removed.
118: * @return array of messages
119: */
120: public Message[] getMessages() {
121:         return msgs;
122: }
123:
124: /**
125: * Invokes the appropriate MessageCountListener method.
126: */
127: @Override
128: public void dispatch(Object listener) {
129:•        if (type == ADDED)
130:          ((MessageCountListener)listener).messagesAdded(this);
131:         else // REMOVED
132:          ((MessageCountListener)listener).messagesRemoved(this);
133: }
134: }