1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 | package Torello.HTML.NodeSearch; import java.util.*; import java.util.function.Predicate; import Torello.HTML.*; import Torello.Java.LV; /** * Abstract parent-class for both of the types of HTML-{@code Iterator's} * * <EMBED CLASS='external-html' DATA-FILE-ID=AbstractHNLI> * * @param <E> This must be either {@link TagNode}, {@link TextNode} or {@link CommentNode}. * This is type of the <I><B>query-specifier {@code Predicate}</B></I> being used. * * @param <F> This is the actual-type that will be iterated. For instances of {@link HNLI}, * both type-parameters {@code 'E'} and {@code 'F'} are identical. The {@code Predicate's} * will be querrying for a certain type of {@code HTMLNode}, and the {@code Iterator} will be * iterating that type of {@code HTMLNode} too. * * <BR /><BR />However, for instances of {@link HNLIInclusive}, the query-specifier is * (automatically-required) to be set to {@code TagNode}, and the {@code Iterator} shall be * returning instances of {@code Vector<HTMLNode>}. Generic Type-Parameter {@code 'F'} is * automatically set to {@code Vector<HTMLNode>} for instances of {@code HNLIInclusive}. */ @SuppressWarnings("unchecked") @Torello.JavaDoc.JDHeaderBackgroundImg public abstract class AbstractHNLI<E extends HTMLNode, F> implements ListIterator<F> { // ******************************************************************************************** // ******************************************************************************************** // FIELDS // ******************************************************************************************** // ******************************************************************************************** /** The internal, underlying instance of {@code Vector} that this {@code Iterator} is using. */ @SuppressWarnings("rawtypes") protected Vector v; /** This {@code Predicate} is the test for identifying {@code Iterator} node-matches */ protected Predicate<E> p; /** This is the type of {@code HTMLNode} being iterated */ protected Class<E> c; /** * Internal {@code 'cursor'} field. * * <BR /><BR />It is initialized to -1, which allows the iterator to identify whether it has * been used yet. */ protected int cursor = -1; /** * This identifies whether one of the {@code Iterator's} html modification methods * ({@code set, remove, add}) have been invoked. Multiple invocations of these methods * after a single iteration causes a {@code SecondModificationException} * * <BR /><BR />Initializing this to {@code TRUE} prevents the programmer from calling any of * the modification methods without first finding a match. */ protected boolean modifiedSince = true; /** * The maximum boundary for the {@code Cursor Boundary Window}. * * <BR /><BR />Initialized to {@code '-1'} means the default setting is for the * {@code Iterator} is not to use a {@code Cursor Boundaries}. One may be assigned * using the {@code restrictCursor(...)} methods. * * @see #restrictCursor(DotPair) * @see #restrictCursor(int, int) * @see #clearCursorBounds() */ protected int maxCursor = -1; /** * The minimum boundary for the {@code Cursor Boundary Window}. * * <BR /><BR />Initialized to {@code '-1'} means the default setting is for the * {@code Iterator} is not to use a {@code Cursor Boundaries}. One may be assigned * using the {@code restrictCursor(...)} methods. * * @see #restrictCursor(DotPair) * @see #restrictCursor(int, int) * @see #clearCursorBounds() */ protected int minCursor = -1; /** This is an attempt to monitor outside modifications to an HTML {@code Vector} */ protected int expectedSize; // ******************************************************************************************** // ******************************************************************************************** // Only Constructor, and 2 Abstract-Methods // ******************************************************************************************** // ******************************************************************************************** /** * Constructs an instance of this class using an HTML {@code Vector}, and a * {@code Predicate} for testing the nodes in that {@code Vector} for matches. * The third parameter {@code 'c'} is only necessary because of Java's type-erasure * problem. * * @param html Any vectorized HTML page or sub-page. * * @param p A {@code java.util.function.Predicate} for testing the nodes in the html * {@code Vector} for matches. * * @param c The sub-class of {@code HTMLNode} that this {@code HNLI} shall be searching. * The values for parameter {@code 'c'} include: {@code TagNode.class, TextNode}, and * {@code CommentNode.class} */ protected AbstractHNLI(Vector<?> html, Predicate<E> p, Class<E> c) { this.expectedSize = html.size(); this.v = html; this.p = p; this.c = c; } // Implemented by both sub-classes abstract void RESET_MATCHES(); // Implemented by both sub-classes abstract int REMOVE(); // ******************************************************************************************** // ******************************************************************************************** // Stuff // ******************************************************************************************** // ******************************************************************************************** protected final void MODIFIED() { modifiedSince = true; expectedSize = v.size(); RESET_MATCHES(); } /** * This removes the last match that was returned by the {@code Iterator} out of the * underlying {@code Vector}. * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS() * @see #MODIFIED() */ public void remove() { CHECK_EXCEPTIONS(); int numRemoved = REMOVE(); if (maxCursor != -1) maxCursor -= numRemoved; MODIFIED(); } // ******************************************************************************************** // ******************************************************************************************** // Cursor-Methods // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #moveCursor(int)} * <BR />Uses minimum cursor-bound for parameter {@code 'location'}, which would * be zero, unless a cursor-window has been set. */ public void moveCursorToStart() { moveCursor((minCursor == -1) ? 0 : minCursor); } /** * Convenience Method. * <BR />Invokes: {@link #moveCursor(int)} * <BR />Uses minimum cursor-bound for parameter {@code 'location'}, which would * be {@code v.size() - 1}, unless a cursor-window has been set. */ public void moveCursorToEnd() { moveCursor((maxCursor == -1) ? (v.size() - 1) : maxCursor); } /** * Sets the internal state of this {@code Iterator's} cursor location. The next invocation of * {@code next, previous}, etc... will return the nearest match to this {@code Vector}-index. * * <EMBED CLASS='external-html' DATA-FILE-ID=CMERESET> * * @param location Any index into the underlying HTML {@code Vector} * * @throws IndexOutOfBoundsException This shall throw if the value passed to parameter * {@code 'location'} is negative, or past the end of the underlying html {@code Vector}. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> */ public void moveCursor(int location) { if (location < 0) throw new IndexOutOfBoundsException ("You have passed a negative value to the cursor location: [" + location + "]."); if (location > v.size()) throw new IndexOutOfBoundsException( "You have passed a cursor location value [" + location + "] that is larger than the " + "size of the underlying Vector [" + v.size() + "]" ); CursorException.check(minCursor, maxCursor, location); cursor = location; modifiedSince = true; expectedSize = v.size(); RESET_MATCHES(); } /** * Convenience Method. * <BR />Accepts: {@code DotPair} * <BR />Invokes: {@link #restrictCursor(int, int)} */ public void restrictCursor(DotPair cursorBounds) { restrictCursor(cursorBounds.start, cursorBounds.end); } /** * Convenience Method. * <BR />Accepts: {@code LV} * <BR />Invokes: {@link #restrictCursor(int, int)} */ public void restrictCursor(LV loopVariable) { restrictCursor(loopVariable.start, loopVariable.end - 1); } /** * Restrict the internal {@code cursor} boundaries to a window defined by the parameters * {@code 'maxCursorBounds'}, and {@code 'minCursorBounds'}. When the {@code cursor} * bounds are restricted, any matches that would be returned by this {@code Iterator} * which lay outside the boundaries of this window shall be skipped or avoided. * * <BR /><BR />If the {@code cursor} is currently located outside the boundaries of the * given window, it shall be moved to within it. * * <EMBED CLASS='external-html' DATA-FILE-ID=CMERESET> * * @param minCursorBounds This shall set a minimum {@code cursor}-index value that restricts * the returned matches generated by this {@code HTML Node List Iterator} to indices less * than {@code minCursorBounds}. This is an <I><B>inclusive bound</B></I>. Matches falling * directly on this index may be included in a result set. * * @param maxCursorBounds This shall set a maximum {@code cursor}-index value that restricts * the returned matches generated by this {@code HTML Node List Iterator} to indices greater * than {@code maxCursorBounds}. This is <B>*also*</B> an <I><B>inclusive bound</B></I>. * Matches falling directly on this index may be included in a result set. * * @throws IndexOutOfBoundsException If the bounds provided by the {@code 'cursorBounds'} * input parameter extend past the end of the current underlying {@code Vector}, then this * exception shall throw. */ public void restrictCursor(int minCursorBounds, int maxCursorBounds) { if (minCursorBounds < 0) throw new IndexOutOfBoundsException ("The value for parameter 'minPos' [" + minCursorBounds + "] cannot be negative"); if (maxCursorBounds >= v.size()) throw new IndexOutOfBoundsException( "The value for parameter 'maxPos' [" + maxCursorBounds + "] is larger than " + "(or equal to) the size of the underlying Vector [" + v.size() + "]." ); this.minCursor = minCursorBounds; this.maxCursor = maxCursorBounds; if (this.cursor < this.minCursor) { RESET_MATCHES(); this.cursor = this.minCursor; } else if (this.cursor > this.maxCursor) { RESET_MATCHES(); this.cursor = this.maxCursor; } } /** Eliminates the <B>Cursor Boundary Window</B>, if one had been set. */ public void clearCursorBounds() { this.maxCursor = this.minCursor = -1; } /** * Retrieves the current location of the cursor. * * @return The current location of {@code 'this' Iterator's} cursor. If {@code hasNext()} * or {@code hasPrevious()} has just been called, for example, the cursor will be pointing * to the next (or previous) node-match in the HTML {@code Vector}. */ public int cursorLocation() { return cursor; } // ******************************************************************************************** // ******************************************************************************************** // ADD // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #addHTMLNode(HTMLNode)} */ public void add(E e) { addHTMLNode(e); } /** * This provides a way to <B><I>insert</I></B> an HTML node into the HTML-{@code Vector}. The * node is at the current {@code cursor}-index. The cursor should be pointing to the location * of the last returned match. * * @param n <EMBED CLASS='external-html' DATA-FILE-ID=HNLIN> * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS() * @see #MODIFIED() */ public void addHTMLNode(HTMLNode n) { CHECK_EXCEPTIONS(); v.add(cursor, n); if (maxCursor != -1) maxCursor++; cursor++; MODIFIED(); } /** * This provides a way to <B><I>insert</I></B> {@code String}-represented HTML. The HTML will * placed in the underlying HTML-{@code Vector} directly before the current * {@code cursor}-index. The {@code cursor} is situated at the location of the last match * returned by the {@code Iterator}. * * <EMBED CLASS='external-html' DATA-FILE-ID=HTMLSTR> * * @param html This is any valid, parse-able HTML section represented as a * {@code java.lang.String}. The {@code String} will be converted to vectorized-html before * insertion. * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see HTMLPage#getPageTokens(CharSequence, boolean) */ public void add(String html) { add(HTMLPage.getPageTokens(html, false)); } /** * Adds vectorized-HTML at the current {@code cursor} position. * * @param html This may be any vectorized HTML page or sub-page. It will be inserted at the * the location of the last returned match. * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS() * @see #MODIFIED() */ public void add(Vector<HTMLNode> html) { CHECK_EXCEPTIONS(); v.addAll(cursor, html); cursor += html.size(); if (maxCursor != -1) maxCursor += html.size(); MODIFIED(); } // ******************************************************************************************** // ******************************************************************************************** // SET // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #setHTMLNode(HTMLNode)} */ public void set(E e) { setHTMLNode(e); } /** * This provides a way to <B><I>replace</I></B> the previous match with an instance of * {@code HTMLNode.} The match that is replaced is the one that was last returned from a * call to any of: {@code next(), previous(), nextIndex(), previousIndex(),} etc... * * @param n <EMBED CLASS='external-html' DATA-FILE-ID=HNLIN> * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #remove() * @see #cursor * @see #expectedSize * @see #maxCursor */ public void setHTMLNode(HTMLNode n) { remove(); v.add(cursor, n); cursor++; expectedSize++; if (maxCursor != -1) maxCursor++; } /** * Convenience Method. * <BR />Invokes: (Parses HTML) {@link HTMLPage#getPageTokens(CharSequence, boolean)} * <BR />And-Then: {@link #set(Vector)} * <BR /><BR /><B CLASS=JDDescLabel>Efficiency Warning:</B> * <BR />This method will will parse (and re-parse) the HTML inside parameter {@code 'html'} * everytime this method is invoked! */ public void set(String html) { set(HTMLPage.getPageTokens(html, false)); } /** * Replaces the last returned match with the contents of parameter {@code 'html'}. * * @param html This may be any-sized html page, or sub-page. * * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * * @see Util.Remove#range(Vector, DotPair) * @see #remove() * @see #cursor * @see #expectedSize * @see #maxCursor */ public void set(Vector<HTMLNode> html) { remove(); v.addAll(cursor, html); cursor += html.size(); expectedSize += html.size(); if (maxCursor != -1) maxCursor += + html.size(); } // ******************************************************************************************** // ******************************************************************************************** // INSERT-AT // ******************************************************************************************** // ******************************************************************************************** /** * Inserts parameter {@code HTMLNode n} into the underlying vectorized-html at * {@code Vector}-index {@code 'pos'}. * * @param n <EMBED CLASS='external-html' DATA-FILE-ID=HNLIN> * * @param pos This is the location in the underlying {@code Vector} being iterated where the * passed parameter {@code 'n'} shall be inserted. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS(int) * @see #MODIFIED() */ public void insertAt(HTMLNode n, int pos) { CHECK_EXCEPTIONS(pos); v.add(pos, n); if (pos < cursor) cursor++; if (maxCursor != -1) maxCursor++; MODIFIED(); } /** * Convenience Method. * <BR />Invokes: (Parses HTML) {@link HTMLPage#getPageTokens(CharSequence, boolean)} * <BR />And-Then: {@link #insertAt(Vector, int)}. * <BR /><BR /><B CLASS=JDDescLabel>Efficiency Warning:</B> * <BR />This method will will parse (and re-parse) the HTML inside parameter {@code 'html'} * everytime this method is invoked! */ public void insertAt(String html, int pos) { insertAt(HTMLPage.getPageTokens(html, false), pos); } /** * Insert the contents of vectorized-html parameter {@code 'html'} into the underlying html * {@code Vector}, beginning at position {@code 'pos'}. * * @param html This may be any HTML page or sub-page. * * @param pos This is the location in the underlying {@code Vector} being iterated where the * passed parameter {@code 'html'} shall be inserted. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS(int) * @see #MODIFIED() */ public void insertAt(Vector<? extends HTMLNode> html, int pos) { CHECK_EXCEPTIONS(pos); v.addAll(pos, html); if (pos < cursor) cursor += html.size(); if (maxCursor != -1) maxCursor += html.size(); MODIFIED(); } // ******************************************************************************************** // ******************************************************************************************** // REPLACE // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #replaceRange(DotPair, Vector)} * <BR /><BR /><B CLASS=JDDescLabel>Off-By-One Mistake:</B> * <BR />{@code ePos} is decremented by 1, since {@code DotPair.end} is always an * <I>inclusive</I> value, while {@code ePos} is always <I>exclusive</I>. */ public void replaceRange(int sPos, int ePos, Vector<HTMLNode> newNodes) { replaceRange(new DotPair(sPos, ePos - 1), newNodes); } /** * This replaces a specified sub-range of nodes in the underlying vectorized-html with the * nodes in {@code 'newNodes'}. * * @param range This is the sub-range or "sub-section" of the underlying vectorized-html page * that is going to be replaced by the {@code 'newNodes'}. * * @param newNodes These are the nodes to be inserted into the location where the old range is. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see Util.Remove#range(Vector, DotPair) * @see DotPair#isInside(int) * @see DotPair#size() * @see #CHECK_EXCEPTIONS(DotPair) * @see #MODIFIED() */ public void replaceRange(DotPair range, Vector<HTMLNode> newNodes) { CHECK_EXCEPTIONS(range); Util.replaceRange(v, range, newNodes); int sizeChange = newNodes.size() - range.size(); if (range.isInside(cursor)) cursor = range.start; else if(cursor > range.end) cursor += sizeChange; if (maxCursor != -1) maxCursor += sizeChange; MODIFIED(); } // ******************************************************************************************** // ******************************************************************************************** // REMOVE // ******************************************************************************************** // ******************************************************************************************** /** * This removes the node at a specified index in the underlying vectorized-html. * * @param pos This is the index into the underlying vectorized-html page to be removed. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_EXCEPTIONS(int) * @see #MODIFIED() */ public void removeElementAt(int pos) { CHECK_EXCEPTIONS(pos); v.removeElementAt(pos); if (pos < cursor) cursor--; if (maxCursor != -1) maxCursor--; MODIFIED(); } /** * Convenience Method. * <BR />Invokes: {@link #removeRange(int, int)} * <BR /><BR /><B CLASS=JDDescLabel>Off-By-One Mistake:</B> * <BR />{@code ePos} is decremented by 1, since {@code DotPair.end} is always an * <I>inclusive</I> value, while {@code ePos} is always <I>exclusive</I>. */ public void removeRange(int sPos, int ePos) { removeRange(new DotPair(sPos, ePos-1)); } /** * This removes a specified sub-range of nodes in the underlying vectorized-html. * * @param range This is the sub-range or "sub-section" of the underlying vectorized-html page; * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see Util.Remove.range(Vector, DotPair) * @see DotPair#isInside(int) * @see DotPair#size() * @see #CHECK_EXCEPTIONS(DotPair) * @see #MODIFIED() */ public void removeRange(DotPair range) { CHECK_EXCEPTIONS(range); Util.Remove.range(v, range); if (range.isInside(cursor)) cursor = range.start; else if(cursor > range.end) cursor -= range.size(); if (maxCursor != -1) maxCursor -= range.size(); MODIFIED(); } /** * This will remove every {@code Vector}-element that is identified by the {@code 'posArr'} * {@code Vector}-position {@code int[]} array. * * @param posArr This is a list of nodes that shall be removed from the underlying * html-{@code Vector}. * * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see Util.Remove.nodes(boolean, Vector, int[]) * @see #CHECK_EXCEPTIONS(int[]) * @see #MODIFIED() */ public void removeElements(int... posArr) { CHECK_EXCEPTIONS(posArr); Util.Remove.nodes(false, v, posArr); // false --> sorts the input int[] array int ORIGINAL_CURSOR = cursor; for (int pos : posArr) if (pos < ORIGINAL_CURSOR) cursor--; else break; if (maxCursor != -1) maxCursor -= posArr.length; MODIFIED(); } // ******************************************************************************************** // ******************************************************************************************** // Exception Checking // ******************************************************************************************** // ******************************************************************************************** /** * Checks the input position {@code 'pos'} parameter to verify it is not out of the * bounds of the underlying HTML-{@code Vector}, nor out of the bounds of the {@code Cursor * Boundary Window} - <I>if one has been set.</I> * * <BR /><BR />This shall throw exceptions if the passed {@code 'range'} is out of either of * these bounds. * * <BR /><BR />Upon completion of this test, the rest of the standard Exception check-throws * are performed. * * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * * @see #CHECK_EXCEPTIONS() */ protected void CHECK_EXCEPTIONS(int pos) { IteratorOutOfBoundsException.check(v, pos); CursorException.check(minCursor, maxCursor, pos); CHECK_EXCEPTIONS(); } /** * Checks the input position {@code 'range'} parameter to verify the provided {@code range} * values are not out of the bounds of the underlying HTML-{@code Vector}, nor out of the * bounds of the {@code Cursor Boundary Window} - <I>if one has been set.</I> * * <BR /><BR />This shall throw exceptions if the passed {@code 'range'} is out of either of * these bounds. * * <BR /><BR />Upon completion of this test, the rest of the standard Exception check-throws * are performed. * * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * * @see #CHECK_EXCEPTIONS() */ protected void CHECK_EXCEPTIONS(DotPair range) { IteratorOutOfBoundsException.check(v, range); CursorException.check(minCursor, maxCursor, range); CHECK_EXCEPTIONS(); } /** * Checks the input position-array {@code 'posArr'} parameter to verify that none of the * indices listed in this position-array are out of the bounds of the underlying * HTML-{@code Vector}, nor out of the bounds of the {@code Cursor Boundary Window} - <I>if * one has been set.</I> * * <BR /><BR />This shall throw exceptions if the indices in this input array extend past * either of these boundaries. * * <BR /><BR />Upon completion of this test, the rest of the standard Exception check-throws * are performed. * * @throws IteratorOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=IOOB_EX> * @throws CursorException <EMBED CLASS='external-html' DATA-FILE-ID=CURSOR_EX> * * @see #CHECK_EXCEPTIONS() */ protected void CHECK_EXCEPTIONS(int[] posArr) { IteratorOutOfBoundsException.check(v, posArr); CursorException.check(minCursor, maxCursor, posArr); CHECK_EXCEPTIONS(); } /** * Does a check regarding whether any exceptions should throw. * * @throws IllegalStateException <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_ILST_EX> * @throws SecondModificationException <EMBED CLASS='external-html' DATA-FILE-ID=SEC_MOD_EX> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @see #CHECK_CME() */ protected void CHECK_EXCEPTIONS() { CHECK_CME(); if (cursor == -1) throw new IllegalStateException( "Neither next, nor previous have been called since initializing the iterator with a " + "constructor." ); if (modifiedSince) throw new SecondModificationException( "One of the remove, add, or set methods - which are this Iterator's Update (Modifier) " + "Operations - has already been called since the prior call to a next, previous, first, " + "or last (Inspection) method. The Modifier and Inspection methods / API of this " + "Iterator may only be used ONCE PER CALL (or, rather, on A ONE-TO-ONE BASIS) with " + "each-other. In other words, after an invocation of an 'Inspection Method' (such as " + "'next'), only one invocation of a 'Modifier Method' (such as 'set') will be allowed " + "until another inspection is invoked." ); } /** * Does a check regarding whether an exception should throw. * * @throws ConcurrentModificationException Checks for, and throws if necessary, Java's * {@code ConcurrentModificationException}. */ protected void CHECK_CME() // Because HNLI<E> is an interface, this is actually 'protected' { if (expectedSize != v.size()) throw new ConcurrentModificationException( "The expected size of the underlying vector was: [" + expectedSize + "], but the " + "encountered size was: [" + v.size() + "]. This implies that the Vector was modified " + "outside of this HNLI Iterator's provided update & modify API. This is not allowed, " + "unless followed by a call to: first, last, firstIndex, or lastIndex - all of which " + "'reset' the outside-modification monitor-logic (as do the cursor-movement and cursor " + "bounds methods)." ); } } |