1 package org.catacomb.numeric.data;
2
3 import org.catacomb.interlish.structure.*;
4 import org.catacomb.report.E;
5
6 import java.util.Collection;
7 import java.util.HashMap;
8
9
10
11
12 public class NumDataSet extends DataItem implements ElementWriter {
13
14
15 HashMap<String, DataItem> itemHM;
16
17
18 public NumDataSet() {
19 this("error");
20
21 }
22
23
24 public NumDataSet(String s) {
25 super(s);
26 itemHM = new HashMap<String, DataItem>();
27 }
28
29
30
31 public Collection<String> getNames() {
32 return itemHM.keySet();
33 }
34
35 public Collection<DataItem> getValues() {
36 return itemHM.values();
37 }
38
39
40 public int size() {
41 return itemHM.keySet().size();
42 }
43
44 public String toString() {
45 return "NumDataSet:" + name;
46 }
47
48
49
50 public boolean matches(NumDataSet ds) {
51 return name.equals(ds.getName());
52 }
53
54
55
56
57
58
59 public DataItem get(String sit) {
60 DataItem ret = null;
61 if (itemHM.containsKey(sit)) {
62 ret = itemHM.get(sit);
63 } else {
64 E.error("Data Set no such item " + sit);
65 }
66 return ret;
67 }
68
69
70
71
72
73 public void add(Object obj) {
74 if (obj instanceof Named && obj instanceof DataItem) {
75 itemHM.put(((Named)obj).getName(), (DataItem)obj);
76 } else {
77 E.error("NumDataSet cant add " + obj);
78 }
79 }
80
81
82 public void addScalar(String lname, String value) {
83 addScalar(new FloatScalar(lname, value));
84 }
85
86 public void addScalar(FloatScalar fs) {
87 itemHM.put(fs.getName(), fs);
88 }
89
90
91 public void addVector(FloatVector fv) {
92 itemHM.put(fv.getName(), fv);
93 }
94
95
96 public void addVectorSet(VectorSet vs) {
97 FloatVector[] fva = vs.getVectors();
98 for (int i = 0; i < fva.length; i++) {
99 addVector(fva[i]);
100 }
101 }
102
103
104 public void addVectorOrScalar(String nm, String stxt) {
105 FloatVector fv = new FloatVector(nm, stxt);
106 if (fv.length() > 1) {
107 addVector(fv);
108 } else {
109 addScalar(new FloatScalar(fv));
110 }
111 }
112
113
114
115
116 public void addDataSet(NumDataSet ds) {
117 String nm = ds.getName();
118 if (itemHM.containsKey(nm)) {
119 Object obj = itemHM.get(nm);
120
121 if (obj instanceof DataSetArray) {
122 ((DataSetArray)obj).add(ds);
123
124 } else if (obj instanceof NumDataSet) {
125 DataSetArray arl = new DataSetArray();
126 arl.setName(nm);
127 arl.add((NumDataSet)obj);
128 arl.add(ds);
129 itemHM.remove(nm);
130 itemHM.put(nm, arl);
131 } else {
132 E.error("cant add data set " + ds + " to " + this + " : mismatched types");
133 }
134
135
136 } else {
137 itemHM.put(nm, ds);
138 }
139 }
140
141
142
143
144 public Element makeElement(ElementFactory efac, Elementizer eltz) {
145 Element elt = efac.makeElement(name != null ? name : "NumDataSet");
146
147 for (String sk : itemHM.keySet()) {
148 Object obj = itemHM.get(sk);
149
150 if (obj instanceof NumDataSet) {
151 efac.addElement(elt, ((NumDataSet)obj).makeElement(efac, eltz));
152
153 } else if (obj instanceof FloatScalar) {
154 FloatScalar fs = (FloatScalar)obj;
155 efac.addAttribute(elt, fs.getName(), fs.getValue());
156
157 } else if (obj instanceof FloatVector) {
158 FloatVector fv = (FloatVector)obj;
159 efac.addElement(elt, efac.makeElement(fv.getName(), fv.getStringValue()));
160
161 } else if (obj instanceof DataSetArray) {
162 DataSetArray dsa = (DataSetArray)obj;
163 NumDataSet[] dsv = dsa.getDataSets();
164 for (int i = 0; i < dsv.length; i++) {
165 efac.addElement(elt, dsv[i].makeElement(efac, eltz));
166 }
167
168 } else {
169 E.error("Data set element making cant handle " + obj);
170 }
171 }
172
173 return elt;
174 }
175
176
177
178 public DataItem getMarked() {
179 return copyMarked();
180 }
181
182
183 public NumDataSet copyMarked() {
184 NumDataSet ret = new NumDataSet(getName());
185
186 for (DataItem dit : itemHM.values()) {
187 if (dit.isMarked()) {
188 ret.add(dit.getMarked());
189
190
191 } else {
192
193 }
194 }
195 return ret;
196 }
197
198
199 }
200