1 package org.catacomb.druid.market;
2
3 import org.catacomb.interlish.structure.Dependent;
4 import org.catacomb.interlish.structure.Supplier;
5
6 import java.util.ArrayList;
7 import java.util.HashMap;
8
9
10
11
12 public class SupplyClerk {
13
14
15 String modality;
16
17 HashMap<String, ArrayList<Dependent>> itemDependents;
18
19 HashMap<String, Object> lastSupplied;
20
21
22 public SupplyClerk(String s) {
23 modality = s;
24
25 itemDependents = new HashMap<String, ArrayList<Dependent>>();
26 lastSupplied = new HashMap<String, Object>();
27
28 }
29
30
31
32 public void notifyAllIfChanged(Supplier supplier) {
33
34 for (String item : itemDependents.keySet()) {
35
36 Object value = supplier.get(modality, item);
37
38 if (sameAsLast(item, value)) {
39
40 } else {
41 lastSupplied.put(item, value);
42 sendTo(itemDependents.get(item), value);
43 }
44
45 }
46
47 }
48
49
50 private boolean sameAsLast(String s, Object val) {
51 boolean ret = false;
52 if (lastSupplied.containsKey(s)) {
53 Object oval = lastSupplied.get(s);
54 if (oval.equals(val)) {
55 ret = true;
56 }
57 }
58
59 return ret;
60 }
61
62
63
64 private void sendTo(ArrayList<Dependent> arl, Object value) {
65 for (Dependent dep : arl) {
66 dep.newValue(value);
67 }
68 }
69
70
71
72
73 public void addDependent(Dependent dep) {
74 String s = dep.getInterestedIn();
75
76 if (itemDependents.containsKey(s)) {
77 itemDependents.get(s).add(dep);
78
79 } else {
80 ArrayList<Dependent> arl = new ArrayList<Dependent>();
81 arl.add(dep);
82 itemDependents.put(s, arl);
83 }
84 }
85
86
87
88
89 }