1
20 package org.crosswire.jsword.book.sword;
21
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
25
26 import org.crosswire.jsword.book.BookException;
27 import org.crosswire.jsword.book.sword.state.OpenFileState;
28 import org.crosswire.jsword.passage.Key;
29 import org.crosswire.jsword.passage.RestrictionType;
30
31
38 public abstract class AbstractKeyBackend<T extends OpenFileState> extends AbstractBackend<T> implements Key {
39
45 public AbstractKeyBackend(SwordBookMetaData sbmd) {
46 super(sbmd);
47 }
48
49
52 public boolean canHaveChildren() {
53 return false;
54 }
55
56
59 public int getChildCount() {
60 return 0;
61 }
62
63
66 public boolean isEmpty() {
67 return getCardinality() == 0;
68 }
69
70
73 @Override
74 public boolean contains(Key key) {
75 return indexOf(key) >= 0;
76 }
77
78
81 public Iterator<Key> iterator() {
82 return new Iterator<Key>() {
83
84
87 public boolean hasNext() {
88 return here < count;
89 }
90
91
94 public Key next() throws NoSuchElementException {
95 if (here >= count) {
96 throw new NoSuchElementException();
97 }
98 return get(here++);
99 }
100
101
104 public void remove() {
105 throw new UnsupportedOperationException();
106 }
107
108 private int here;
109 private int count = getCardinality();
110 };
111 }
112
113
114
117 public void addAll(Key key) {
118 throw new UnsupportedOperationException();
119 }
120
121
124 public void removeAll(Key key) {
125 throw new UnsupportedOperationException();
126 }
127
128
131 public void clear() {
132 throw new UnsupportedOperationException();
133 }
134
135 public void setAliasKey(T state, Key alias, Key source) throws IOException {
136 throw new UnsupportedOperationException();
137 }
138
139 public void setRawText(T state, Key key, String text) throws BookException, IOException {
140 throw new UnsupportedOperationException();
141 }
142
143
146 public Key getParent() {
147 return null;
148 }
149
150 @Override
151 public AbstractKeyBackend<T> clone() {
152 AbstractKeyBackend<T> clone = null;
153 try {
154 clone = (AbstractKeyBackend<T>) super.clone();
155 } catch (CloneNotSupportedException e) {
156 assert false : e;
157 }
158 return clone;
159 }
160
161
164 public String getName() {
165 return getBookMetaData().getInitials();
166 }
167
168
171 public String getName(Key base) {
172 return getName();
173 }
174
175
178 public String getOsisID() {
179 return getName();
180 }
181
182
185 public String getOsisRef() {
186 return getName();
187 }
188
189
192 public String getRootName() {
193 return getName();
194 }
195
196
199 public void retainAll(Key key) {
200 throw new UnsupportedOperationException();
201 }
202
203 @Override
204 public boolean equals(Object obj) {
205 if (obj == null) {
207 return false;
208 }
209
210 if (!obj.getClass().equals(this.getClass())) {
213 return false;
214 }
215
216 return compareTo((Key) obj) == 0;
217 }
218
219 @Override
220 public int hashCode() {
221 return getName().hashCode();
222 }
223
224
227 public int compareTo(Key that) {
228
229 if (this == that) {
230 return 0;
231 }
232
233 if (that == null) {
234 return -1;
236 }
237
238 int ret = this.getName().compareTo(that.getName());
239
240 if (ret != 0) {
241 return ret;
242 }
243
244 Iterator<Key> thisIter = this.iterator();
246 Iterator<Key> thatIter = that.iterator();
247
248 Key thisfirst = null;
249 Key thatfirst = null;
250
251 if (thisIter.hasNext()) {
252 thisfirst = thisIter.next();
253 }
254
255 if (thatIter.hasNext()) {
256 thatfirst = thatIter.next();
257 }
258
259 if (thisfirst == null) {
260 if (thatfirst == null) {
261 return 0;
263 }
264 return 1;
266 }
267
268 if (thatfirst == null) {
269 return -1;
271 }
272
273 return thisfirst.getName().compareTo(thatfirst.getName());
274 }
275
276
279 public void blur(int by, RestrictionType restrict) {
280 }
281
282
285 private static final long serialVersionUID = -2782112117361556089L;
286 }
287