1 package org.catacomb.interlish.content;
2
3 import java.util.ArrayList;
4
5 import org.catacomb.interlish.structure.StructureWatcher;
6 import org.catacomb.report.E;
7
8 public class QuantifiedListValue extends PrimitiveValue {
9
10
11 private NVPair[] items;
12
13
14 private ArrayList<StructureWatcher> p_structureWatchers;
15
16
17 public QuantifiedListValue() {
18 super();
19 items = new NVPair[0];
20 }
21
22
23 public QuantifiedListValue(NVPair[] aa) {
24 super();
25 items = aa;
26 }
27
28
29 public String toString() {
30 return "QuantifiedListValue: " + items.length + " items ";
31
32 }
33
34
35 public void silentSetQuantity(String s, double d) {
36 if (s == null) {
37 return;
38 }
39
40 boolean done = false;
41 for (NVPair nvp : items) {
42 if (nvp.isCalled(s)) {
43 nvp.setValue(d, sDouble(d));
44 done = true;
45 break;
46 }
47 }
48
49 if (done) {
50
51 } else {
52 NVPair[] newitems = new NVPair[items.length + 1];
53 for (int i = 0; i < items.length; i++) {
54 newitems[i] = items[i];
55 }
56 items = newitems;
57 items[items.length-1] = new NVPair(s, d, sDouble(d));
58 reportStructureChange("added");
59 }
60 logChange();
61 }
62
63
64 public int indexOf(String s) {
65 int ipos = -1;
66 for (int iit = 0; iit < items.length; iit++) {
67 if (items[iit].isCalled(s)) {
68 ipos = iit;
69 break;
70 }
71
72 }
73 return ipos;
74 }
75
76 public void removeItem(String s) {
77 int ipos = indexOf(s);
78 if (ipos >= 0) {
79 removeItem(ipos);
80 }
81 }
82
83
84 private void removeItem(int itg) {
85 NVPair[] newitems = new NVPair[items.length-1];
86 for (int i = 0; i < itg; i++) {
87 newitems[i] = items[i];
88 }
89 for (int i = itg; i < items.length-1; i++) {
90 newitems[i] = items[i+1];
91 }
92 items = newitems;
93 reportStructureChange("removed");
94 }
95
96
97 public void reportableSetQuantity(String s, double d, Object src) {
98 silentSetQuantity(s, d);
99
100 reportValueChange(src);
101 }
102
103
104 public int size() {
105 return items.length;
106 }
107
108
109 private String sDouble(double d) {
110 return String.format("%.3g", new Double(d));
111 }
112
113 public String[] getItemNames() {
114
115 String[] sa = new String[items.length];
116 for (int i = 0; i < items.length; i++) {
117 sa[i] = items[i].getName();
118 }
119 return sa;
120 }
121
122
123 public NVPair[] getItems() {
124 return items;
125 }
126
127 public NVPair[] getNameValueItems() {
128 return items;
129 }
130
131
132
133 public double getIthDouble(int idx) {
134 return items[idx].getValue();
135 }
136
137
138 public void setIthDouble(int idx, double dv) {
139 items[idx].setValue(dv, sDouble(dv));
140 E.info("qlv reporting set val " + dv);
141 reportValueChange(this);
142 }
143
144
145
146 public void addStructureWatcher(StructureWatcher sw) {
147 if (p_structureWatchers == null) {
148 p_structureWatchers = new ArrayList<StructureWatcher>();
149 }
150 p_structureWatchers.add(sw);
151 }
152
153 public void removeStructureWatcher(StructureWatcher sw) {
154 p_structureWatchers.remove(sw);
155 }
156
157 public void reportStructureChange(String typ) {
158 if (p_structureWatchers != null) {
159 for (StructureWatcher sw : p_structureWatchers) {
160 sw.structureChanged(this, typ);
161 }
162 }
163 }
164
165
166 }