1 package org.catacomb.druid.market;
2
3 import org.catacomb.interlish.structure.*;
4 import org.catacomb.report.E;
5
6
7
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Map;
11
12
13
14
15 public abstract class ProducerConsumerBoard {
16
17
18 HashMap<String, ArrayList<Producer>> producers;
19 HashMap<String, Consumer> consumers;
20 HashMap<String, Receiver> receivers;
21 HashMap<String, Provider> providers;
22
23 HashMap<String, Visible> visibles;
24 HashMap<String, ArrayList<Viewer>> viewers;
25
26 ArrayList<Supplier> suppliers;
27
28
29 String modality;
30
31
32
33 public ProducerConsumerBoard() {
34 producers = new HashMap<String, ArrayList<Producer>>();
35 consumers = new HashMap<String, Consumer>();
36 receivers = new HashMap<String, Receiver>();
37 providers = new HashMap<String, Provider>();
38 suppliers = new ArrayList<Supplier>();
39 visibles = new HashMap<String, Visible>();
40 viewers = new HashMap<String, ArrayList<Viewer>>();
41 }
42
43
44
45 public void setModality(String s) {
46 modality = s;
47 }
48
49
50
51 public void addProducer(Producer producer, String flavor) {
52
53
54
55
56
57
58
59 if (consumers.containsKey(flavor)) {
60 connect(producer, consumers.get(flavor));
61 consumers.remove(flavor);
62
63
64
65 } else if (receivers.containsKey(flavor)) {
66 connect(producer, receivers.get(flavor));
67
68 } else {
69
70 if (producers.containsKey(flavor)) {
71 producers.get(flavor).add(producer);
72
73
74
75 } else {
76 ArrayList<Producer> alp = new ArrayList<Producer>();
77 alp.add(producer);
78 producers.put(flavor, alp);
79
80 }
81
82
83 }
84 }
85
86
87
88 public void addConsumer(Consumer consumer, String flavor) {
89
90
91
92 if (producers.containsKey(flavor)) {
93
94 ArrayList<Producer> alp = producers.get(flavor);
95 if (alp.size() > 1) {
96 E.error("muptiple producdrs of " + flavor + " in " + modality + ": need a receiver "
97 + "but got a consumer: " + consumer);
98 }
99 connect(alp.get(0), consumer);
100 producers.remove(flavor);
101
102
103 } else if (providers.containsKey(flavor)) {
104 connect(providers.get(flavor), consumer);
105
106 } else {
107 if (checkSuppliersFor(consumer, flavor)) {
108
109
110
111 } else {
112 consumers.put(flavor, consumer);
113
114
115 }
116 }
117 }
118
119
120 public void addReceiver(Receiver rec, String flavor) {
121
122
123 if (receivers.containsKey(flavor)) {
124 E.error("multiple receivers of " + flavor + " only last one used " + rec);
125 }
126 receivers.put(flavor, rec);
127
128 checkReceiverProducers(rec, flavor);
129 }
130
131
132
133 public void addSupplier(Supplier sup) {
134 if (suppliers == null) {
135 suppliers = new ArrayList<Supplier>();
136 }
137 suppliers.add(sup);
138 checkConsumerSuppliers();
139
140 }
141
142
143 private void checkReceiverProducers(Receiver rec, String flav) {
144 if (producers.containsKey(flav)) {
145
146 for (Producer p : producers.get(flav)) {
147 connect(p, rec);
148 }
149
150 producers.remove(flav);
151 }
152 }
153
154
155 private void checkConsumerSuppliers() {
156 if (consumers != null) {
157 for (Map.Entry<String, Consumer> me : consumers.entrySet()) {
158 checkSuppliersFor(me.getValue(), me.getKey());
159 }
160 }
161 }
162
163
164
165 private boolean checkSuppliersFor(Consumer c, String item) {
166 boolean ret = false;
167
168 if (suppliers != null) {
169 for (Supplier sup : suppliers) {
170 if (sup.canSupply(modality, item)) {
171
172
173
174 sup.addDependent(new ConsumerAgent(c, modality, item));
175 ret = true;
176 break;
177 }
178 }
179 }
180
181 return ret;
182 }
183
184 public void connect(Producer producer, Consumer consumer) {
185 E.override();
186 }
187
188
189
190 public void logUnresolved(String boardID) {
191 logUnresolved(producers, "Producer", boardID);
192 logUnresolved(consumers, "Consumer", boardID);
193 logUnresolved(viewers, "Viewer", boardID);
194
195 }
196
197 private void logUnresolved(HashMap<?, ?> hm, String s, String boardID) {
198
199
200
201
202
203
204
205
206 }
207
208
209 public void addVisible(Visible vbl, String flavor) {
210
211
212
213 if (visibles.containsKey(flavor)) {
214 E.warning("visible item being overridden: modality=" + modality +
215 " flavor=" + flavor + " item=" + visibles.get(flavor));
216 }
217 visibles.put(flavor, vbl);
218 checkViewers(flavor, vbl);
219 }
220
221
222
223 public void addViewer(Viewer vwr, String flavor) {
224
225 if (visibles.containsKey(flavor)) {
226 connectVisibleViewer(visibles.get(flavor), vwr);
227
228 } else {
229 if (viewers.containsKey(flavor)) {
230 viewers.get(flavor).add(vwr);
231 } else {
232 ArrayList<Viewer> arv = new ArrayList<Viewer>();
233 arv.add(vwr);
234 viewers.put(flavor, arv);
235 }
236 }
237
238 }
239
240
241 private void checkViewers(String flv, Visible vbl) {
242 if (viewers.containsKey(flv)) {
243 for (Viewer vwr : viewers.get(flv)) {
244 connectVisibleViewer(vbl, vwr);
245 }
246 viewers.remove(flv);
247 }
248 }
249
250 public void connectVisibleViewer(Visible vbl, Viewer vwr) {
251 E.override();
252 }
253
254
255
256 public void addProvider(Provider provider, String flavor) {
257 if (consumers.containsKey(flavor)) {
258 connect(provider, consumers.get(flavor));
259 consumers.remove(flavor);
260 }
261
262 if (providers.containsKey(flavor)) {
263 E.warning("multiple providers of " + flavor + " " + modality);
264 } else {
265 providers.put(flavor, provider);
266 }
267
268 }
269
270 }