| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.framework; |
| 12 | |
| 13 | import java.util.Iterator; |
| 14 | import java.util.NoSuchElementException; |
| 15 | import java.util.List; |
| 16 | import java.util.ListIterator; |
| 17 | |
| 18 | /** |
| 19 | * Returns instances of {@link Sequence} such that state cannot be modified. |
| 20 | * |
| 21 | * @see Sequence |
| 22 | * @author %javadoc.author.tag% |
| 23 | * @version %version%<br/> |
| 24 | * <i>Build Number %build.number%</i><br/> |
| 25 | * <i>Build Time %build.time%</i> |
| 26 | */ |
| 27 | public final class SequenceFactory |
| 28 | { |
| 29 | private SequenceFactory() throws UnsupportedOperationException |
| 30 | { |
| 31 | throw new UnsupportedOperationException(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Returns a new instance of {@link Sequence} that is backed by the given arguments. |
| 36 | * |
| 37 | * @param a The array that backs the returned {@link Sequence}. |
| 38 | * @return A new instance of {@link Sequence} that is backed by the given arguments. |
| 39 | * @throws NullPointerException If <code>a</code> is <code>null</code>. |
| 40 | */ |
| 41 | public static <E> Sequence<E> newSequence(final E... a) throws NullPointerException |
| 42 | { |
| 43 | if(a == null) |
| 44 | { |
| 45 | throw new NullPointerException(); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | return new ArraySequence<E>(a); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns a new instance of {@link Sequence} that is backed by the given {@link java.util.List}. |
| 55 | * |
| 56 | * @param l The {@link java.util.List} that backs the returned {@link Sequence}. |
| 57 | * @return A new instance of {@link Sequence} that is backed by the given {@link java.util.List}. |
| 58 | * @throws NullPointerException If <code>l</code> is <code>null</code>. |
| 59 | */ |
| 60 | public static <E> Sequence<E> newSequence(final List<E> l) throws NullPointerException |
| 61 | { |
| 62 | if(l == null) |
| 63 | { |
| 64 | throw new NullPointerException(); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | return new ListSequence<E>(l); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns a new instance of {@link Sequence} that is backed by the given constant for the given length. |
| 74 | * |
| 75 | * @param constant The constant that backs the returned {@link Sequence}. |
| 76 | * @param length The length of the constant that backs the returned {@link Sequence}. |
| 77 | * @return A new instance of {@link Sequence} that is backed by the given constant. |
| 78 | * @throws IllegalArgumentException If <code>length</code> is less than <code>1</code>. |
| 79 | */ |
| 80 | public static <E> Sequence<E> newConstantSequence(final E constant, final int length) throws IllegalArgumentException |
| 81 | { |
| 82 | if(length < 1) |
| 83 | { |
| 84 | throw new IllegalArgumentException(); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | return new ConstantSequence<E>(constant, length); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private static final class ArraySequence<E> implements Sequence<E> |
| 93 | { |
| 94 | private static final long serialVersionUID = 2L; |
| 95 | |
| 96 | private final E[] a; |
| 97 | private int hc; |
| 98 | private boolean hcCalculated; |
| 99 | private String ts; |
| 100 | |
| 101 | ArraySequence(final E... a) |
| 102 | { |
| 103 | this.a = a; |
| 104 | } |
| 105 | |
| 106 | public E get(final int index) throws IndexOutOfBoundsException |
| 107 | { |
| 108 | if(index < 0 || index >= length()) |
| 109 | { |
| 110 | throw new IndexOutOfBoundsException(String.valueOf(index)); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | return a[index]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public int length() |
| 119 | { |
| 120 | return a.length; |
| 121 | } |
| 122 | |
| 123 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
| 124 | { |
| 125 | if(sequence == null) |
| 126 | { |
| 127 | throw new NullPointerException(); |
| 128 | } |
| 129 | else if(length() == 0) |
| 130 | { |
| 131 | return sequence; |
| 132 | } |
| 133 | else if(sequence.length() == 0) |
| 134 | { |
| 135 | return this; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | return new CompositeSequence<E>(this, sequence); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 144 | { |
| 145 | if(fromPosition < 0) |
| 146 | { |
| 147 | throw new PositionOutOfBoundsException(fromPosition); |
| 148 | } |
| 149 | else if(toPosition > length()) |
| 150 | { |
| 151 | throw new PositionOutOfBoundsException(toPosition); |
| 152 | } |
| 153 | else if(fromPosition > toPosition) |
| 154 | { |
| 155 | throw new PositionOutOfBoundsException(fromPosition); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | return new SubSequence<E>(this, fromPosition, toPosition); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
| 164 | { |
| 165 | if(sequence == null) |
| 166 | { |
| 167 | throw new NullPointerException(); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | if(position < 0 || position > length()) |
| 172 | { |
| 173 | throw new PositionOutOfBoundsException(position); |
| 174 | } |
| 175 | else if(position == 0) |
| 176 | { |
| 177 | return sequence.append(this); |
| 178 | } |
| 179 | else if(position == length()) |
| 180 | { |
| 181 | return this.append(sequence); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | return new InsertSequence<E>(position, this, sequence); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 191 | { |
| 192 | if(fromPosition < 0) |
| 193 | { |
| 194 | throw new PositionOutOfBoundsException(fromPosition); |
| 195 | } |
| 196 | else if(toPosition > length()) |
| 197 | { |
| 198 | throw new PositionOutOfBoundsException(toPosition); |
| 199 | } |
| 200 | else if(fromPosition > toPosition) |
| 201 | { |
| 202 | throw new PositionOutOfBoundsException(fromPosition); |
| 203 | } |
| 204 | else if(fromPosition == toPosition) |
| 205 | { |
| 206 | return this; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | public int indexOf(final E e) |
| 215 | { |
| 216 | return indexOf(0, e); |
| 217 | } |
| 218 | |
| 219 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 220 | { |
| 221 | if(position < 0 || position > length()) |
| 222 | { |
| 223 | throw new PositionOutOfBoundsException(position); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | final SequenceIterator<E> it = getIterator(position); |
| 228 | |
| 229 | while(it.hasNext()) |
| 230 | { |
| 231 | final int itPosition = it.getPosition(); |
| 232 | final E x = it.next(); |
| 233 | |
| 234 | if(x == null ? e == null : x.equals(e)) |
| 235 | { |
| 236 | return itPosition; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return -1; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | public int lastIndexOf(final E e) |
| 245 | { |
| 246 | return lastIndexOf(length(), e); |
| 247 | } |
| 248 | |
| 249 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 250 | { |
| 251 | if(position < 0 || position > length()) |
| 252 | { |
| 253 | throw new PositionOutOfBoundsException(position); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | final SequenceIterator<E> it = getIterator(position); |
| 258 | |
| 259 | while(it.hasPrevious()) |
| 260 | { |
| 261 | final E x = it.previous(); |
| 262 | final int itPosition = it.getPosition(); |
| 263 | |
| 264 | if(x == null ? e == null : x.equals(e)) |
| 265 | { |
| 266 | return itPosition; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return -1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | public boolean isEmpty() |
| 275 | { |
| 276 | return length() == 0; |
| 277 | } |
| 278 | |
| 279 | public Iterator<E> iterator() |
| 280 | { |
| 281 | return getIterator(0); |
| 282 | } |
| 283 | |
| 284 | public SequenceIterator<E> getIterator() |
| 285 | { |
| 286 | return getIterator(0); |
| 287 | } |
| 288 | |
| 289 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
| 290 | { |
| 291 | if(position < 0 || position > length()) |
| 292 | { |
| 293 | throw new PositionOutOfBoundsException(position); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | return new SequenceIterator<E>() |
| 298 | { |
| 299 | private int pos; |
| 300 | |
| 301 | { |
| 302 | pos = position; |
| 303 | } |
| 304 | |
| 305 | public final boolean hasNext() |
| 306 | { |
| 307 | return pos != length(); |
| 308 | } |
| 309 | |
| 310 | public final E next() |
| 311 | { |
| 312 | if(pos == length()) |
| 313 | { |
| 314 | throw new NoSuchElementException(); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | return a[pos++]; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public final void remove() |
| 323 | { |
| 324 | throw new UnsupportedOperationException(); |
| 325 | } |
| 326 | |
| 327 | public boolean hasPrevious() |
| 328 | { |
| 329 | return pos != 0; |
| 330 | } |
| 331 | |
| 332 | public E previous() throws NoSuchElementException |
| 333 | { |
| 334 | if(pos == 0) |
| 335 | { |
| 336 | throw new NoSuchElementException(); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | return a[--pos]; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | public int getPosition() |
| 345 | { |
| 346 | return pos; |
| 347 | } |
| 348 | }; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public String toString() |
| 354 | { |
| 355 | if(ts == null) |
| 356 | { |
| 357 | final StringBuilder sb = new StringBuilder(); |
| 358 | |
| 359 | sb.append("["); |
| 360 | |
| 361 | final SequenceIterator<E> it = getIterator(); |
| 362 | |
| 363 | while(it.hasNext()) |
| 364 | { |
| 365 | sb.append(it.next()); |
| 366 | |
| 367 | if(it.hasNext()) |
| 368 | { |
| 369 | sb.append("]["); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | sb.append("]"); |
| 374 | |
| 375 | ts = sb.toString(); |
| 376 | } |
| 377 | |
| 378 | return ts; |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public boolean equals(final Object o) |
| 383 | { |
| 384 | if(this == o) |
| 385 | { |
| 386 | return true; |
| 387 | } |
| 388 | else if(o == null || (!(o instanceof Sequence))) |
| 389 | { |
| 390 | return false; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | final Sequence<?> s = (Sequence<?>)o; |
| 395 | |
| 396 | if(s.length() != length()) |
| 397 | { |
| 398 | return false; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | final Iterator<?> it1 = iterator(); |
| 403 | final Iterator<?> it2 = s.iterator(); |
| 404 | |
| 405 | while(it1.hasNext() && it2.hasNext()) |
| 406 | { |
| 407 | final Object se1 = it1.next(); |
| 408 | final Object se2 = it2.next(); |
| 409 | |
| 410 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
| 411 | { |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public int hashCode() |
| 423 | { |
| 424 | if(!hcCalculated) |
| 425 | { |
| 426 | final int oddPrime = 461; |
| 427 | int result = 73; |
| 428 | |
| 429 | for(E e : this) |
| 430 | { |
| 431 | if(e != null) |
| 432 | { |
| 433 | result = result * oddPrime + e.hashCode(); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | hc = result; |
| 438 | hcCalculated = true; |
| 439 | } |
| 440 | |
| 441 | return hc; |
| 442 | } |
| 443 | |
| 444 | public Sequence<E> clone() |
| 445 | { |
| 446 | return new ArraySequence<E>(a); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | private static final class ListSequence<E> implements Sequence<E> |
| 451 | { |
| 452 | private static final long serialVersionUID = 2L; |
| 453 | |
| 454 | private final List<E> l; |
| 455 | private int hc; |
| 456 | private boolean hcCalculated; |
| 457 | private String ts; |
| 458 | |
| 459 | ListSequence(final List<E> l) |
| 460 | { |
| 461 | this.l = l; |
| 462 | } |
| 463 | |
| 464 | public E get(final int index) throws IndexOutOfBoundsException |
| 465 | { |
| 466 | if(index < 0 || index >= length()) |
| 467 | { |
| 468 | throw new IndexOutOfBoundsException(String.valueOf(index)); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | return l.get(index); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | public int length() |
| 477 | { |
| 478 | return l.size(); |
| 479 | } |
| 480 | |
| 481 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
| 482 | { |
| 483 | if(sequence == null) |
| 484 | { |
| 485 | throw new NullPointerException(); |
| 486 | } |
| 487 | else if(length() == 0) |
| 488 | { |
| 489 | return sequence; |
| 490 | } |
| 491 | else if(sequence.length() == 0) |
| 492 | { |
| 493 | return this; |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | return new CompositeSequence<E>(this, sequence); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 502 | { |
| 503 | if(fromPosition < 0) |
| 504 | { |
| 505 | throw new PositionOutOfBoundsException(fromPosition); |
| 506 | } |
| 507 | else if(toPosition > length()) |
| 508 | { |
| 509 | throw new PositionOutOfBoundsException(toPosition); |
| 510 | } |
| 511 | else if(fromPosition > toPosition) |
| 512 | { |
| 513 | throw new PositionOutOfBoundsException(fromPosition); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | return new SubSequence<E>(this, fromPosition, toPosition); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
| 522 | { |
| 523 | if(sequence == null) |
| 524 | { |
| 525 | throw new NullPointerException(); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | if(position < 0 || position > length()) |
| 530 | { |
| 531 | throw new PositionOutOfBoundsException(position); |
| 532 | } |
| 533 | else if(position == 0) |
| 534 | { |
| 535 | return sequence.append(this); |
| 536 | } |
| 537 | else if(position == length()) |
| 538 | { |
| 539 | return this.append(sequence); |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | return new InsertSequence<E>(position, this, sequence); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 549 | { |
| 550 | if(fromPosition < 0) |
| 551 | { |
| 552 | throw new PositionOutOfBoundsException(fromPosition); |
| 553 | } |
| 554 | else if(toPosition > length()) |
| 555 | { |
| 556 | throw new PositionOutOfBoundsException(toPosition); |
| 557 | } |
| 558 | else if(fromPosition > toPosition) |
| 559 | { |
| 560 | throw new PositionOutOfBoundsException(fromPosition); |
| 561 | } |
| 562 | else if(fromPosition == toPosition) |
| 563 | { |
| 564 | return this; |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | public int indexOf(final E e) |
| 573 | { |
| 574 | return indexOf(0, e); |
| 575 | } |
| 576 | |
| 577 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 578 | { |
| 579 | if(position < 0 || position > length()) |
| 580 | { |
| 581 | throw new PositionOutOfBoundsException(position); |
| 582 | } |
| 583 | else |
| 584 | { |
| 585 | final SequenceIterator<E> it = getIterator(position); |
| 586 | |
| 587 | while(it.hasNext()) |
| 588 | { |
| 589 | final int itPosition = it.getPosition(); |
| 590 | final E x = it.next(); |
| 591 | |
| 592 | if(x == null ? e == null : x.equals(e)) |
| 593 | { |
| 594 | return itPosition; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return -1; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | public int lastIndexOf(final E e) |
| 603 | { |
| 604 | return lastIndexOf(length(), e); |
| 605 | } |
| 606 | |
| 607 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 608 | { |
| 609 | if(position < 0 || position > length()) |
| 610 | { |
| 611 | throw new PositionOutOfBoundsException(position); |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | final SequenceIterator<E> it = getIterator(position); |
| 616 | |
| 617 | while(it.hasPrevious()) |
| 618 | { |
| 619 | final E x = it.previous(); |
| 620 | final int itPosition = it.getPosition(); |
| 621 | |
| 622 | if(x == null ? e == null : x.equals(e)) |
| 623 | { |
| 624 | return itPosition; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return -1; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | public boolean isEmpty() |
| 633 | { |
| 634 | return length() == 0; |
| 635 | } |
| 636 | |
| 637 | public Iterator<E> iterator() |
| 638 | { |
| 639 | return getIterator(0); |
| 640 | } |
| 641 | |
| 642 | public SequenceIterator<E> getIterator() |
| 643 | { |
| 644 | return getIterator(0); |
| 645 | } |
| 646 | |
| 647 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
| 648 | { |
| 649 | if(position < 0 || position > length()) |
| 650 | { |
| 651 | throw new PositionOutOfBoundsException(position); |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | return new SequenceIterator<E>() |
| 656 | { |
| 657 | private int pos; |
| 658 | private final ListIterator<E> it; |
| 659 | |
| 660 | { |
| 661 | pos = position; |
| 662 | it = l.listIterator(position); |
| 663 | } |
| 664 | |
| 665 | public final boolean hasNext() |
| 666 | { |
| 667 | return pos != length(); |
| 668 | } |
| 669 | |
| 670 | public final E next() |
| 671 | { |
| 672 | if(pos == length()) |
| 673 | { |
| 674 | throw new NoSuchElementException(); |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | pos++; |
| 679 | return it.next(); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | public final void remove() |
| 684 | { |
| 685 | throw new UnsupportedOperationException(); |
| 686 | } |
| 687 | |
| 688 | public boolean hasPrevious() |
| 689 | { |
| 690 | return pos != 0; |
| 691 | } |
| 692 | |
| 693 | public E previous() throws NoSuchElementException |
| 694 | { |
| 695 | if(pos == 0) |
| 696 | { |
| 697 | throw new NoSuchElementException(); |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | pos--; |
| 702 | return it.previous(); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | public int getPosition() |
| 707 | { |
| 708 | return pos; |
| 709 | } |
| 710 | }; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | public String toString() |
| 716 | { |
| 717 | if(ts == null) |
| 718 | { |
| 719 | final StringBuilder sb = new StringBuilder(); |
| 720 | |
| 721 | sb.append("["); |
| 722 | |
| 723 | final SequenceIterator<E> it = getIterator(); |
| 724 | |
| 725 | while(it.hasNext()) |
| 726 | { |
| 727 | sb.append(it.next()); |
| 728 | |
| 729 | if(it.hasNext()) |
| 730 | { |
| 731 | sb.append("]["); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | sb.append("]"); |
| 736 | |
| 737 | ts = sb.toString(); |
| 738 | } |
| 739 | |
| 740 | return ts; |
| 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public boolean equals(final Object o) |
| 745 | { |
| 746 | if(this == o) |
| 747 | { |
| 748 | return true; |
| 749 | } |
| 750 | else if(o == null || (!(o instanceof Sequence))) |
| 751 | { |
| 752 | return false; |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | final Sequence<?> s = (Sequence<?>)o; |
| 757 | |
| 758 | if(s.length() != length()) |
| 759 | { |
| 760 | return false; |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | final Iterator<?> it1 = iterator(); |
| 765 | final Iterator<?> it2 = s.iterator(); |
| 766 | |
| 767 | while(it1.hasNext() && it2.hasNext()) |
| 768 | { |
| 769 | final Object se1 = it1.next(); |
| 770 | final Object se2 = it2.next(); |
| 771 | |
| 772 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
| 773 | { |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | return true; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | public int hashCode() |
| 785 | { |
| 786 | if(!hcCalculated) |
| 787 | { |
| 788 | final int oddPrime = 461; |
| 789 | int result = 73; |
| 790 | |
| 791 | for(E e : this) |
| 792 | { |
| 793 | if(e != null) |
| 794 | { |
| 795 | result = result * oddPrime + e.hashCode(); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | hc = result; |
| 800 | hcCalculated = true; |
| 801 | } |
| 802 | |
| 803 | return hc; |
| 804 | } |
| 805 | |
| 806 | public Sequence<E> clone() |
| 807 | { |
| 808 | return new ListSequence<E>(l); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | private static final class CompositeSequence<E> implements Sequence<E> |
| 813 | { |
| 814 | private static final long serialVersionUID = 2L; |
| 815 | |
| 816 | private final Sequence<E> seq1; |
| 817 | private final Sequence<E> seq2; |
| 818 | private int hc; |
| 819 | private boolean hcCalculated; |
| 820 | private String ts; |
| 821 | |
| 822 | CompositeSequence(final Sequence<E> seq1, final Sequence<E> seq2) |
| 823 | { |
| 824 | this.seq1 = seq1; |
| 825 | this.seq2 = seq2; |
| 826 | } |
| 827 | |
| 828 | public E get(final int index) throws IndexOutOfBoundsException |
| 829 | { |
| 830 | if(index < 0 || index >= length()) |
| 831 | { |
| 832 | throw new IndexOutOfBoundsException(); |
| 833 | } |
| 834 | else if(index < seq1.length()) |
| 835 | { |
| 836 | return seq1.get(index); |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | return seq2.get(index - seq1.length()); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | public int length() |
| 845 | { |
| 846 | return seq1.length() + seq2.length(); |
| 847 | } |
| 848 | |
| 849 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
| 850 | { |
| 851 | if(sequence == null) |
| 852 | { |
| 853 | throw new NullPointerException(); |
| 854 | } |
| 855 | else if(sequence.length() == 0) |
| 856 | { |
| 857 | return this; |
| 858 | } |
| 859 | else |
| 860 | { |
| 861 | return new CompositeSequence<E>(this, sequence); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 866 | { |
| 867 | if(fromPosition < 0) |
| 868 | { |
| 869 | throw new PositionOutOfBoundsException(fromPosition); |
| 870 | } |
| 871 | else if(toPosition > length()) |
| 872 | { |
| 873 | throw new PositionOutOfBoundsException(toPosition); |
| 874 | } |
| 875 | else if(fromPosition > toPosition) |
| 876 | { |
| 877 | throw new PositionOutOfBoundsException(fromPosition); |
| 878 | } |
| 879 | else |
| 880 | { |
| 881 | return new SubSequence<E>(this, fromPosition, toPosition); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
| 886 | { |
| 887 | if(sequence == null) |
| 888 | { |
| 889 | throw new NullPointerException(); |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | if(position < 0 || position > length()) |
| 894 | { |
| 895 | throw new PositionOutOfBoundsException(position); |
| 896 | } |
| 897 | else if(position == 0) |
| 898 | { |
| 899 | return sequence.append(this); |
| 900 | } |
| 901 | else if(position == length()) |
| 902 | { |
| 903 | return this.append(sequence); |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | return new InsertSequence<E>(position, this, sequence); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 913 | { |
| 914 | if(fromPosition < 0) |
| 915 | { |
| 916 | throw new PositionOutOfBoundsException(fromPosition); |
| 917 | } |
| 918 | else if(toPosition > length()) |
| 919 | { |
| 920 | throw new PositionOutOfBoundsException(toPosition); |
| 921 | } |
| 922 | else if(fromPosition > toPosition) |
| 923 | { |
| 924 | throw new PositionOutOfBoundsException(fromPosition); |
| 925 | } |
| 926 | else if(fromPosition == toPosition) |
| 927 | { |
| 928 | return this; |
| 929 | } |
| 930 | else |
| 931 | { |
| 932 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | public int indexOf(final E e) |
| 937 | { |
| 938 | return indexOf(0, e); |
| 939 | } |
| 940 | |
| 941 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 942 | { |
| 943 | if(position < 0 || position > length()) |
| 944 | { |
| 945 | throw new PositionOutOfBoundsException(position); |
| 946 | } |
| 947 | else |
| 948 | { |
| 949 | final SequenceIterator<E> it = getIterator(position); |
| 950 | |
| 951 | while(it.hasNext()) |
| 952 | { |
| 953 | final int itPosition = it.getPosition(); |
| 954 | final E x = it.next(); |
| 955 | |
| 956 | if(x == null ? e == null : x.equals(e)) |
| 957 | { |
| 958 | return itPosition; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return -1; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | public int lastIndexOf(final E e) |
| 967 | { |
| 968 | return lastIndexOf(length(), e); |
| 969 | } |
| 970 | |
| 971 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
| 972 | { |
| 973 | if(position < 0 || position > length()) |
| 974 | { |
| 975 | throw new PositionOutOfBoundsException(position); |
| 976 | } |
| 977 | else |
| 978 | { |
| 979 | final SequenceIterator<E> it = getIterator(position); |
| 980 | |
| 981 | while(it.hasPrevious()) |
| 982 | { |
| 983 | final E x = it.previous(); |
| 984 | final int itPosition = it.getPosition(); |
| 985 | |
| 986 | if(x == null ? e == null : x.equals(e)) |
| 987 | { |
| 988 | return itPosition; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | return -1; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | public boolean isEmpty() |
| 997 | { |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | public Iterator<E> iterator() |
| 1002 | { |
| 1003 | return getIterator(0); |
| 1004 | } |
| 1005 | |
| 1006 | public SequenceIterator<E> getIterator() |
| 1007 | { |
| 1008 | return getIterator(0); |
| 1009 | } |
| 1010 | |
| 1011 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
| 1012 | { |
| 1013 | if(position < 0 || position > length()) |
| 1014 | { |
| 1015 | throw new PositionOutOfBoundsException(position); |
| 1016 | } |
| 1017 | else |
| 1018 | { |
| 1019 | return new SequenceIterator<E>() |
| 1020 | { |
| 1021 | private int pos; |
| 1022 | |
| 1023 | { |
| 1024 | pos = position; |
| 1025 | } |
| 1026 | |
| 1027 | public final boolean hasNext() |
| 1028 | { |
| 1029 | return pos != length(); |
| 1030 | } |
| 1031 | |
| 1032 | public final E next() |
| 1033 | { |
| 1034 | if(pos == length()) |
| 1035 | { |
| 1036 | throw new NoSuchElementException(); |
| 1037 | } |
| 1038 | else if(pos < seq1.length()) |
| 1039 | { |
| 1040 | return seq1.get(pos++); |
| 1041 | } |
| 1042 | else |
| 1043 | { |
| 1044 | return seq2.get(pos++ - seq1.length()); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | public final void remove() |
| 1049 | { |
| 1050 | throw new UnsupportedOperationException(); |
| 1051 | } |
| 1052 | |
| 1053 | public boolean hasPrevious() |
| 1054 | { |
| 1055 | return pos != 0; |
| 1056 | } |
| 1057 | |
| 1058 | public E previous() throws NoSuchElementException |
| 1059 | { |
| 1060 | if(pos == 0) |
| 1061 | { |
| 1062 | throw new NoSuchElementException(); |
| 1063 | } |
| 1064 | else if(pos <= seq1.length()) |
| 1065 | { |
| 1066 | return seq1.get(--pos); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | return seq2.get(--pos - seq1.length()); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | public int getPosition() |
| 1075 | { |
| 1076 | return pos; |
| 1077 | } |
| 1078 | }; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | @Override |
| 1083 | public String toString() |
| 1084 | { |
| 1085 | if(ts == null) |
| 1086 | { |
| 1087 | final StringBuilder sb = new StringBuilder(); |
| 1088 | |
| 1089 | sb.append("["); |
| 1090 | |
| 1091 | final SequenceIterator<E> it = getIterator(); |
| 1092 | |
| 1093 | while(it.hasNext()) |
| 1094 | { |
| 1095 | sb.append(it.next()); |
| 1096 | |
| 1097 | if(it.hasNext()) |
| 1098 | { |
| 1099 | sb.append("]["); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | sb.append("]"); |
| 1104 | |
| 1105 | ts = sb.toString(); |
| 1106 | } |
| 1107 | |
| 1108 | return ts; |
| 1109 | } |
| 1110 | |
| 1111 | @Override |
| 1112 | public boolean equals(final Object o) |
| 1113 | { |
| 1114 | if(this == o) |
| 1115 | { |
| 1116 | return true; |
| 1117 | } |
| 1118 | else if(o == null || (!(o instanceof Sequence))) |
| 1119 | { |
| 1120 | return false; |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | final Sequence<?> s = (Sequence<?>)o; |
| 1125 | |
| 1126 | if(s.length() != length()) |
| 1127 | { |
| 1128 | return false; |
| 1129 | } |
| 1130 | else |
| 1131 | { |
| 1132 | final Iterator<?> it1 = iterator(); |
| 1133 | final Iterator<?> it2 = s.iterator(); |
| 1134 | |
| 1135 | while(it1.hasNext() && it2.hasNext()) |
| 1136 | { |
| 1137 | final Object se1 = it1.next(); |
| 1138 | final Object se2 = it2.next(); |
| 1139 | |
| 1140 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
| 1141 | { |
| 1142 | return false; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | return true; |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
| 1152 | public int hashCode() |
| 1153 | { |
| 1154 | if(!hcCalculated) |
| 1155 | { |
| 1156 | final int oddPrime = 461; |
| 1157 | int result = 73; |
| 1158 | |
| 1159 | for(E e : this) |
| 1160 | { |
| 1161 | if(e != null) |
| 1162 | { |
| 1163 | result = result * oddPrime + e.hashCode(); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | hc = result; |
| 1168 | hcCalculated = true; |
| 1169 | } |
| 1170 | |
| 1171 | return hc; |
| 1172 | } |
| 1173 | |
| 1174 | public Sequence<E> clone() |
| 1175 | { |
| 1176 | return new CompositeSequence<E>(seq1, seq2); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | private static final class SubSequence<E> implements Sequence<E> |
| 1181 | { |
| 1182 | private static final long serialVersionUID = 2L; |
| 1183 | |
| 1184 | private final Sequence<E> sequence; |
| 1185 | private final int fromPosition; |
| 1186 | private final int toPosition; |
| 1187 | private int hc; |
| 1188 | private boolean hcCalculated; |
| 1189 | private String ts; |
| 1190 | |
| 1191 | SubSequence(final Sequence<E> sequence, final int fromPosition, final int toPosition) |
| 1192 | { |
| 1193 | this.sequence = sequence; |
| 1194 | this.fromPosition = fromPosition; |
| 1195 | this.toPosition = toPosition; |
| 1196 | } |
| 1197 | |
| 1198 | public E get(final int index) throws IndexOutOfBoundsException |
| 1199 | { |
| 1200 | if(index < 0 || index >= length()) |
| 1201 | { |
| 1202 | throw new IndexOutOfBoundsException(); |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | return sequence.get(index + fromPosition); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | public int length() |
| 1211 | { |
| 1212 | return toPosition - fromPosition; |
| 1213 | } |
| 1214 | |
| 1215 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
| 1216 | { |
| 1217 | if(sequence == null) |
| 1218 | { |
| 1219 | throw new NullPointerException(); |
| 1220 | } |
| 1221 | else if(length() == 0) |
| 1222 | { |
| 1223 | return sequence; |
| 1224 | } |
| 1225 | else if(sequence.length() == 0) |
| 1226 | { |
| 1227 | return this; |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | return new CompositeSequence<E>(this, sequence); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 1236 | { |
| 1237 | if(fromPosition < 0) |
| 1238 | { |
| 1239 | throw new PositionOutOfBoundsException(fromPosition); |
| 1240 | } |
| 1241 | else if(toPosition > length()) |
| 1242 | { |
| 1243 | throw new PositionOutOfBoundsException(toPosition); |
| 1244 | } |
| 1245 | else if(fromPosition > toPosition) |
| 1246 | { |
| 1247 | throw new PositionOutOfBoundsException(fromPosition); |
| 1248 | } |
| 1249 | else |
| 1250 | { |
| 1251 | return new SubSequence<E>(this, fromPosition, toPosition); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
| 1256 | { |
| 1257 | if(sequence == null) |
| 1258 | { |
| 1259 | throw new NullPointerException(); |
| 1260 | } |
| 1261 | else |
| 1262 | { |
| 1263 | if(position < 0 || position > length()) |
| 1264 | { |
| 1265 | throw new PositionOutOfBoundsException(position); |
| 1266 | } |
| 1267 | else if(position == 0) |
| 1268 | { |
| 1269 | return sequence.append(this); |
| 1270 | } |
| 1271 | else if(position == length()) |
| 1272 | { |
| 1273 | return this.append(sequence); |
| 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | return new InsertSequence<E>(position, this, sequence); |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
| 1283 | { |
| 1284 | if(fromPosition < 0) |
| 1285 | { |
| 1286 | throw new PositionOutOfBoundsException(fromPosition); |
| 1287 | } |
| 1288 | else if(toPosition > length()) |
| 1289 | { |
| 1290 | throw new PositionOutOfBoundsException(toPosition); |
| 1291 | } |
| 1292 | else if(fromPosition > toPosition) |
| 1293 | { |
| 1294 | throw new PositionOutOfBoundsException(fromPosition); |
| 1295 | } |
| 1296 | else if(fromPosition == |