1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.kernel.bo.module;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.qedeq.base.trace.Trace;
24 import org.qedeq.base.utility.EqualsUtility;
25 import org.qedeq.kernel.bo.common.ModuleReferenceList;
26 import org.qedeq.kernel.bo.common.QedeqBo;
27 import org.qedeq.kernel.se.common.IllegalModuleDataException;
28 import org.qedeq.kernel.se.common.ModuleContext;
29
30
31
32
33
34
35
36
37
38 public class KernelModuleReferenceList implements ModuleReferenceList {
39
40
41 private static final Class CLASS = KernelModuleReferenceList.class;
42
43
44 private final List labels;
45
46
47 private final List props;
48
49
50 private final List contexts;
51
52
53 private final Map label2Context;
54
55
56
57
58 public KernelModuleReferenceList() {
59 labels = new ArrayList();
60 props = new ArrayList();
61 contexts = new ArrayList();
62 label2Context = new HashMap();
63 }
64
65
66
67
68
69
70
71
72
73 public void add(final ModuleContext context, final String label, final QedeqBo prop)
74 throws IllegalModuleDataException {
75 if (label == null || label.length() <= 0) {
76 throw new IllegalModuleDataException(10003, "An label was not defined.",
77 new ModuleContext(context), null,
78 null);
79 }
80 final ModuleContext con = new ModuleContext(context);
81 labels.add(label);
82 label2Context.put(label, con);
83 contexts.add(con);
84 Trace.param(CLASS, "add(ModuleContext, String, QedeqBo)", "context", con);
85 props.add(prop);
86 }
87
88
89
90
91
92
93
94
95
96
97 public void addLabelUnique(final ModuleContext context, final String label,
98 final QedeqBo prop) throws IllegalModuleDataException {
99 if (labels.contains(label)) {
100
101 throw new IllegalModuleDataException(10004, "Label \"" + label
102 + "\" defined more than once.", new ModuleContext(context),
103 (ModuleContext) label2Context.get(label), null);
104 }
105 add(new ModuleContext(context), label, prop);
106 }
107
108 public int size() {
109 return labels.size();
110 }
111
112 public String getLabel(final int index) {
113 return (String) labels.get(index);
114 }
115
116 public QedeqBo getQedeqBo(final int index) {
117 return (QedeqBo) props.get(index);
118 }
119
120
121
122
123
124
125
126 public KernelQedeqBo getKernelQedeqBo(final int index) {
127 return (KernelQedeqBo) props.get(index);
128 }
129
130 public ModuleContext getModuleContext(final int index) {
131 return (ModuleContext) contexts.get(index);
132 }
133
134 public QedeqBo getQedeqBo(final String label) {
135 final int index = labels.indexOf(label);
136 if (index < 0) {
137 return null;
138 }
139 return (QedeqBo) props.get(index);
140 }
141
142
143
144
145
146
147
148 public KernelQedeqBo getKernelQedeqBo(final String label) {
149 final int index = labels.indexOf(label);
150 if (index < 0) {
151 return null;
152 }
153 return (KernelQedeqBo) props.get(index);
154 }
155
156
157
158
159
160
161
162 public boolean contains(final KernelQedeqBo bo) {
163 return props.contains(bo);
164 }
165
166
167
168
169
170
171 public void remove(final KernelQedeqBo bo) {
172 int index;
173 while (0 <= (index = props.indexOf(bo))) {
174 final String label = (String) labels.get(index);
175 label2Context.remove(label);
176 props.remove(index);
177 labels.remove(index);
178 contexts.remove(index);
179 }
180 }
181
182 public boolean equals(final Object obj) {
183 if (!(obj instanceof KernelModuleReferenceList)) {
184 return false;
185 }
186 final ModuleReferenceList otherList = (ModuleReferenceList) obj;
187 if (size() != otherList.size()) {
188 return false;
189 }
190 for (int i = 0; i < size(); i++) {
191 if (!EqualsUtility.equals(getLabel(i), otherList.getLabel(i))
192 || !EqualsUtility.equals(getQedeqBo(i),
193 otherList.getQedeqBo(i))) {
194 return false;
195 }
196 }
197 return true;
198 }
199
200
201
202
203 public void clear() {
204 labels.clear();
205 props.clear();
206 contexts.clear();
207 label2Context.clear();
208 }
209
210
211
212
213
214
215 public void set(final KernelModuleReferenceList list) {
216 clear();
217 this.labels.addAll(list.labels);
218 this.props.addAll(list.props);
219 this.contexts.addAll(list.contexts);
220 this.label2Context.putAll(list.label2Context);
221 }
222
223 public int hashCode() {
224 int hash = 0;
225 for (int i = 0; i < size(); i++) {
226 hash = hash ^ (i + 1);
227 if (getLabel(i) != null) {
228 hash = hash ^ getLabel(i).hashCode();
229 hash = hash ^ getQedeqBo(i).hashCode();
230 }
231 }
232 return hash;
233 }
234
235 public String toString() {
236 final StringBuffer buffer = new StringBuffer("module reference list:\n");
237 for (int i = 0; i < size(); i++) {
238 if (i != 0) {
239 buffer.append("\n");
240 }
241 buffer.append((i + 1) + ":\t");
242 buffer.append(getLabel(i)).append(": ").append(getQedeqBo(i)).append("\n");
243 }
244 return buffer.toString();
245 }
246
247 }