1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.api.base;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25
26 import net.sourceforge.mflow.api.Msg;
27 import net.sourceforge.mflow.api.MsgReceiver;
28 import net.sourceforge.mflow.api.MsgSource;
29
30 /***
31 * Abstract implementation of MsgSource
32 *
33 * @author <a href="mailto:david@carr.name">David Carr</a>
34 * @version $Revision: 1.3 $
35 */
36 public abstract class AbstractMsgSource
37 extends AbstractMsgFlowComponent
38 implements MsgSource {
39 /***
40 * Private storage of the MsgReceivers to get Msgs from this MsgSource
41 */
42 private List receivers = Collections.synchronizedList(new ArrayList());
43
44 /***
45 * Private storage of Msgs to send
46 */
47 private List messagesToSend = Collections.synchronizedList(new ArrayList());
48
49 /***
50 * @return the queued messages
51 */
52 protected final List getQueuedMessages() {
53 return this.messagesToSend;
54 }
55
56 /***
57 * Adds a MsgReceiver
58 *
59 * @param receiver the new receiver
60 */
61 public final void addReceiver(final MsgReceiver receiver) {
62 if (!this.receivers.contains(receiver)) {
63 this.receivers.add(receiver);
64 }
65 }
66
67 /***
68 * Removes a MsgReceiver
69 *
70 * @param receiver the receiver to remove
71 */
72 public final void removeReceiver(final MsgReceiver receiver) {
73 this.receivers.remove(receiver);
74 }
75
76 /***
77 * Accessor for all MsgReceivers associated with this MsgSource
78 *
79 * @return an array of MsgReceivers
80 */
81 public final MsgReceiver[] getReceivers() {
82 return (MsgReceiver[]) this.receivers.toArray(
83 new MsgReceiver[this.receivers.size()]);
84 }
85
86 /***
87 * Mutator for all MsgReceivers associated with this MsgSource
88 *
89 * @param aReceivers the new array of MsgReceivers to use
90 */
91 public final void setReceivers(final MsgReceiver[] aReceivers) {
92 this.receivers.clear();
93 this.receivers.addAll(Arrays.asList(aReceivers));
94 }
95
96 /***
97 * Called prior to sending messages
98 */
99 protected abstract void queueMessages();
100
101 /***
102 * Sends all queued Msgs
103 */
104 public final void send() {
105 queueMessages();
106 for (int i = 0; i < this.receivers.size(); i++) {
107 MsgReceiver mr = (MsgReceiver) this.receivers.get(i);
108 for (int j = 0; j < this.messagesToSend.size(); j++) {
109 Msg m = (Msg) this.messagesToSend.get(j);
110 mr.putMsg(m);
111 }
112 }
113 this.messagesToSend.clear();
114 }
115 }
This page was automatically generated by Maven