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