1
20 package org.crosswire.jsword.passage;
21
22 import java.util.Iterator;
23
24
30 public abstract class AbstractKeyList implements Key {
31
36 protected AbstractKeyList(String name) {
37 this.name = name;
38 }
39
40
43 public boolean isEmpty() {
44 return getCardinality() == 0;
45 }
46
47
50 public boolean contains(Key key) {
51 return indexOf(key) >= 0;
52 }
53
54
57 public void retainAll(Key key) {
58 Key shared = new DefaultKeyList();
59 shared.addAll(key);
60 retain(this, shared);
61 }
62
63
71 protected static void retain(Key alter, Key base) {
72 Iterator<Key> it = alter.iterator();
73 while (it.hasNext()) {
74 Key sublist = it.next();
75 if (sublist.canHaveChildren()) {
76 retain(sublist, base);
77 if (sublist.isEmpty()) {
78 it.remove();
79 }
80 } else {
81 if (!base.contains(sublist)) {
82 it.remove();
83 }
84 }
85 }
86 }
87
88 @Override
89 public String toString() {
90 return getName();
91 }
92
93
99 public void setName(String name) {
100 this.name = name;
101 }
102
103
106 public String getName() {
107 if (name != null) {
108 return name;
109 }
110
111 DefaultKeyVisitor visitor = new NameVisitor();
112 KeyUtil.visit(this, visitor);
113 return visitor.toString();
114 }
115
116
119 public String getName(Key base) {
120 return getName();
121 }
122
123
126 public String getRootName() {
127 return getName();
128 }
129
130
133 public String getOsisRef() {
134 DefaultKeyVisitor visitor = new OsisRefVisitor();
135 KeyUtil.visit(this, visitor);
136 return visitor.toString();
137 }
138
139
142 public String getOsisID() {
143 DefaultKeyVisitor visitor = new OsisIDVisitor();
144 KeyUtil.visit(this, visitor);
145 return visitor.toString();
146 }
147
148 @Override
149 public boolean equals(Object obj) {
150 if (obj == null) {
152 return false;
153 }
154
155 if (!obj.getClass().equals(this.getClass())) {
158 return false;
159 }
160
161 return compareTo((Key) obj) == 0;
162 }
163
164 @Override
165 public int hashCode() {
166 return getName().hashCode();
167 }
168
169
172 public int compareTo(Key that) {
173
174 if (this == that) {
175 return 0;
176 }
177
178 if (that == null) {
179 return -1;
181 }
182
183 int ret = this.getName().compareTo(that.getName());
184
185 if (ret != 0) {
186 return ret;
187 }
188
189 Iterator<Key> thisIter = this.iterator();
191 Iterator<Key> thatIter = that.iterator();
192
193 Key thisfirst = null;
194 Key thatfirst = null;
195
196 if (thisIter.hasNext()) {
197 thisfirst = thisIter.next();
198 }
199
200 if (thatIter.hasNext()) {
201 thatfirst = thatIter.next();
202 }
203
204 if (thisfirst == null) {
205 if (thatfirst == null) {
206 return 0;
208 }
209 return 1;
211 }
212
213 if (thatfirst == null) {
214 return -1;
216 }
217
218 return thisfirst.getName().compareTo(thatfirst.getName());
219 }
220
221 @Override
222 public AbstractKeyList clone() {
223 AbstractKeyList clone = null;
224 try {
225 clone = (AbstractKeyList) super.clone();
226 } catch (CloneNotSupportedException e) {
227 assert false : e;
228 }
229 return clone;
230 }
231
232
236 static class NameVisitor extends DefaultKeyVisitor {
237
240 NameVisitor() {
241 buffer = new StringBuilder();
242 }
243
244 @Override
245 public void visitLeaf(Key key) {
246 buffer.append(key.getName());
247 buffer.append(AbstractPassage.REF_PREF_DELIM);
248 }
249
250 @Override
251 public String toString() {
252 String reply = buffer.toString();
253 if (reply.length() > 0) {
254 reply = reply.substring(0, reply.length() - AbstractPassage.REF_PREF_DELIM.length());
256 }
257
258 return reply;
259 }
260
261 protected StringBuilder buffer;
262 }
263
264
268 static class OsisRefVisitor extends NameVisitor {
269 @Override
270 public void visitLeaf(Key key) {
271 buffer.append(key.getOsisRef());
272 buffer.append(AbstractPassage.REF_PREF_DELIM);
273 }
274 }
275
276
280 static class OsisIDVisitor extends NameVisitor {
281 @Override
282 public void visitLeaf(Key key) {
283 buffer.append(key.getOsisID());
284 buffer.append(AbstractPassage.REF_OSIS_DELIM);
285 }
286
287 @Override
288 public String toString() {
289 String reply = super.toString();
290 if (reply.length() > 0) {
291 reply = reply.substring(0, reply.length() - AbstractPassage.REF_OSIS_DELIM.length());
293 }
294
295 return reply;
296 }
297 }
298
299
302 private String name;
303
304
307 private static final long serialVersionUID = 3858640507828137034L;
308 }
309