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 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | /* * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package Torello.Java.ReadOnly; import Torello.Java.Additional.RemoveUnsupportedIterator; import java.util.*; import java.io.Serializable; import java.util.stream.Collector; import java.util.function.Function; import java.util.function.Supplier; import java.util.function.Predicate; /** * Immutable Wrapper for <CODE>java.util.TreeSet</CODE>, found in the "Java Collections * Framework". * * <EMBED CLASS=globalDefs DATA-JDK=TreeSet> * <EMBED CLASS='external-html' DATA-FILE-ID=MUCHOS_CONSTRUCTORS> * <EMBED CLASS='external-html' DATA-FILE-ID=DATA_CLASS> * * @param <E> the type of elements maintained by this set */ @Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JDHBI_MAIN") @SuppressWarnings("unchecked") public class ReadOnlyTreeSet<E> // extends AbstractReadOnlySet<E> implements ReadOnlyNavigableSet<E>, Serializable { // ******************************************************************************************** // ******************************************************************************************** // Protected & Private Fields, Methods, Statics // ******************************************************************************************** // ******************************************************************************************** /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; // Minor Optimization where new TreeSet's that have no contents always re-use this static // instance. Since this instance is completely empty, the Raw-Types things is irrelevant. @SuppressWarnings("rawtypes") private static final TreeSet EMPTY_TREE_SET = new TreeSet(); // Singleton & Empty ReadOnlyTreeSet, Uses the "Supplier Constructor" @SuppressWarnings("rawtypes") private static final ReadOnlyTreeSet EMPTY_READONLY_TREE_SET = new ReadOnlyTreeSet(null, () -> null); // The actual TreeSet used by this instance. private final TreeSet<E> treeSet; // TRUE => This was built using the class ROTreeSetBuilder // FALSE => This was built using the clone() of a standard java.util.TreeSet constructor private final boolean fromBuilderOrTreeSet; // Mimics the C++ Keyword/Concept of "Friend Class". Is "Friends With" ROTreeSetBuilder static class AccessBadge { private AccessBadge() { } } private static final AccessBadge friendClassBadge = new AccessBadge(); public static <T> ReadOnlyTreeSet<T> emptyROTS() { return (ReadOnlyTreeSet<T>) EMPTY_READONLY_TREE_SET; } // ******************************************************************************************** // ******************************************************************************************** // Basic Constructors // ******************************************************************************************** // ******************************************************************************************** // To all the readers out there following along: The "AccessBadge" thing is just a slightly // wimpier substitute for the C++ keyword / concept 'friend' or "Friend Class". It means this // constructor is (for all intents and purposes) a private-constructor, except for the class // ROTreeSetBuilder // // This is the Constructor used by the Builder. It has a "Zero-Size" Optimization ReadOnlyTreeSet(ROTreeSetBuilder<E> rotsb, ROTreeSetBuilder.AccessBadge badge) { Objects.requireNonNull(badge, "Access Badge is null. Requires Friend-Class Badge"); fromBuilderOrTreeSet = true; this.treeSet = rotsb; } /** * Copies parameter {@code 'c'} (and saves it) in order to guarantee that {@code 'this'} * instance is Read-Only, and shielded from outside modification. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param c The {@code TreeSet} to be copied and saved into this instance internal * and private {@code 'treeSet'} field. */ public ReadOnlyTreeSet(Collection<E> c) { fromBuilderOrTreeSet = false; this.treeSet = (c.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : new TreeSet<>(c); } /** * Copies parameter {@code 's'} (and saves it) in order to guarantee that {@code 'this'} * instance is Read-Only, and shielded from outside modification. * * @param s The {@code TreeSet} to be copied and saved into this instance internal * and private {@code 'treeSet'} field. */ public ReadOnlyTreeSet(SortedSet<E> s) { fromBuilderOrTreeSet = false; this.treeSet = (s.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : new TreeSet<>(s); } /** * Use a {@code Supplier<E>} to provide an arbitrary number of elements of type {@code 'E'} * directly to this constructor. This constructor will request elements from the * {@code Supplier} provided to parameter {@code 's'} until {@code 's'} returns null. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param s Any Java {@code Supplier<E>} */ public ReadOnlyTreeSet(Comparator<? super E> comparator, Supplier<E> s) { fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); E e; while ((e = s.get()) != null) treeSet.add(e); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } // ******************************************************************************************** // ******************************************************************************************** // Iterable & Array Based Constructors - **NO FILTER** // ******************************************************************************************** // ******************************************************************************************** /** * If only a small amount of processing needs to be done on the contents of some Java * Data-Type, and using an entire Builder-Class seems disproportionately complex - <I>this * constructor can convert any Java {@code Iterable} into a {@code ReadOnlyTreeSet}, using * a simple {@code 'mapper'}</I>. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=ITERABLE_TYPE_PARAM> * @param i Any Java {@code Iterable} * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=ITERABLE_MAPPER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * * @throws NullPointerException if either {@code 'i'} or {@code 'mapper'} are passed null. */ public <T> ReadOnlyTreeSet( Iterable<T> i, Function<? super T, ? extends E> mapper, Comparator<? super E> comparator ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (T t : i) treeSet.add(mapper.apply(t)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * If a Standard Java {@code Iterable} can be directly mapped into a {@code TreeSet} * (and, ergo, an entire Builder-Instance would be a bit excessive) - <I>this constructor will * seamlessly convert any Java {@code Iterable<E>} directly into a {@code ReadOnlyTreeSet<E>} * with just this single invocation</I>. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param i Any Java {@code Iterable<E>} * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> */ public ReadOnlyTreeSet(Iterable<E> i, Comparator<? super E> comparator) { fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (E element : i) treeSet.add(element); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param elementsType <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_TYPE> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=CCEX> * @throws NullPointerException If {@code 'elementsType'} is passed null. */ public ReadOnlyTreeSet (Class<E> elementsType, Comparator<? super E> comparator, Object... elements) { Objects.requireNonNull(elementsType, ROHelpers.NULL_MSG + "'elementsType'"); fromBuilderOrTreeSet = false; if (elements.length == 0) this.treeSet = (TreeSet<E>) EMPTY_TREE_SET; else { this.treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (Object element : elements) this.treeSet.add(elementsType.cast(element)); } } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=OBJ_E_MAPPER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=CCEX> * @throws NullPointerException If {@code 'mapper'} is passed null. */ public ReadOnlyTreeSet( Function<Object, ? extends E> mapper, Comparator<? super E> comparator, Object... elements ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); fromBuilderOrTreeSet = false; if (elements.length == 0) this.treeSet = (TreeSet<E>) EMPTY_TREE_SET; else { this.treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (Object element : elements) this.treeSet.add(mapper.apply(element)); } } // ******************************************************************************************** // ******************************************************************************************** // Iterable & Array Based Constructors - **INCLUDES ELEMENT FILTER** // ******************************************************************************************** // ******************************************************************************************** /** * If only a small amount of processing needs to be done on the contents of some Java * Data-Type, and using an entire Builder-Class seems disproportionately complex - <I>this * constructor can convert any Java {@code Iterable} into a {@code ReadOnlyTreeSet}, using * a simple {@code 'mapper'}</I>. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=ITERABLE_TYPE_PARAM> * @param i Any Java {@code Iterable<T>} * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=ITERABLE_MAPPER> * @param filter <EMBED CLASS='external-html' DATA-FTP=T DATA-FILE-ID=PREDICATE_FILTER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * * @throws NullPointerException if any of {@code 'i'}, {@code 'mapper'} or {@code 'filter'} are * passed null */ public <T> ReadOnlyTreeSet( Iterable<T> i, Function<? super T, ? extends E> mapper, Predicate<? super T> filter, Comparator<? super E> comparator ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (T t : i) if (filter.test(t)) treeSet.add(mapper.apply(t)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * If a Standard Java {@code Iterable} can be directly mapped into a {@code TreeSet} * (and, ergo, an entire Builder-Instance would be a bit excessive) - <I>this constructor will * seamlessly convert any Java {@code Iterable<E>} directly into a {@code ReadOnlyTreeSet<E>} * with just this single invocation</I>. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param i Any Java {@code Iterabler<E>} * @param filter <EMBED CLASS='external-html' DATA-FTP=E DATA-FILE-ID=PREDICATE_FILTER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * * @throws NullPointerException if either {@code 'i'} or {@code 'filter'} are passed null */ public ReadOnlyTreeSet (Iterable<E> i, Predicate<? super E> filter, Comparator<? super E> comparator) { fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (E element : i) if (filter.test(element)) treeSet.add(element); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param elementsType <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_TYPE> * @param filter <EMBED CLASS='external-html' DATA-FTP=E DATA-FILE-ID=PREDICATE_FILTER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=CCEX> * * @throws NullPointerException If either {@code 'elementsType'} or {@code 'filter'} are passed * null. */ public ReadOnlyTreeSet( Class<E> elementsType, Predicate<? super E> filter, Comparator<? super E> comparator, Object... elements ) { Objects.requireNonNull(elementsType, ROHelpers.NULL_MSG + "'elementsType'"); Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); E e; for (Object element : elements) if (filter.test(e = elementsType.cast(element))) treeSet.add(e); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=OBJ_E_MAPPER> * @param filter <EMBED CLASS='external-html' DATA-FTP=Object DATA-FILE-ID=PREDICATE_FILTER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=CCEX> * @throws NullPointerException If either {@code 'mapper'} or {@code 'filter'} are passed null */ public ReadOnlyTreeSet( Function<Object, ? extends E> mapper, Predicate<Object> filter, Comparator<? super E> comparator, Object... elements ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (Object element : elements) if (filter.test(element)) treeSet.add(mapper.apply(element)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } // ******************************************************************************************** // ******************************************************************************************** // @SafeVarargs / Variable-Arity / VarArgs: with Parameterized / Generic Type's // ******************************************************************************************** // ******************************************************************************************** /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> */ @SafeVarargs public ReadOnlyTreeSet(Comparator<? super E> comparator, E... elements) { fromBuilderOrTreeSet = false; if (elements.length == 0) this.treeSet = (TreeSet<E>) EMPTY_TREE_SET; else { this.treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (E e : elements) this.treeSet.add(e); } } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=VARARGS_TYPE_PARAM> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=T_ARR_E_MAPPER> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws NullPointerException If {@code 'mapper'} is passed null. */ @SafeVarargs public <T> ReadOnlyTreeSet( Comparator<? super E> comparator, Function<? super T, ? extends E> mapper, T... elements ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); fromBuilderOrTreeSet = false; if (elements.length == 0) this.treeSet = (TreeSet<E>) EMPTY_TREE_SET; else { this.treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (T t : elements) this.treeSet.add(mapper.apply(t)); } } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param filter <EMBED CLASS='external-html' DATA-FTP=E DATA-FILE-ID=PREDICATE_FILTER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws NullPointerException If {@code 'filter'} is passed null. */ @SafeVarargs public ReadOnlyTreeSet( Predicate<? super E> filter, Comparator<? super E> comparator, E... elements ) { Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (E e : elements) if (filter.test(e)) treeSet.add(e); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } /** * Builds {@code ReadOnlyTreeSet<E>} instance having Generic-Type {@code 'E'}, and contents * {@code 'elements'}. * * <EMBED CLASS='external-html' DATA-FILE-ID=TREESET_CMPR_WARN> * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=VARARGS_TYPE_PARAM> * @param filter <EMBED CLASS='external-html' DATA-FTP=T DATA-FILE-ID=PREDICATE_FILTER> * @param mapper <EMBED CLASS='external-html' DATA-FILE-ID=T_ARR_E_MAPPER> * @param comparator <EMBED CLASS='external-html' DATA-FILE-ID=COMPARATOR> * @param elements <EMBED CLASS='external-html' DATA-FILE-ID=ELEMENTS_VAR_ARGS> * * @throws NullPointerException If either {@code 'mapper'} or {@code 'filter'} are passed null */ @SafeVarargs public <T> ReadOnlyTreeSet( Predicate<? super T> filter, Function<? super T, ? extends E> mapper, Comparator<? super E> comparator, T... elements ) { Objects.requireNonNull(mapper, ROHelpers.NULL_MSG + "'mapper'"); Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> treeSet = (comparator != null) ? new TreeSet<>(comparator) : new TreeSet<>(); for (T t : elements) if (filter.test(t)) treeSet.add(mapper.apply(t)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (treeSet.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : treeSet; } // ******************************************************************************************** // ******************************************************************************************** // PRIMITIVE-ARRAY INPUT // ******************************************************************************************** // ******************************************************************************************** /** * Converts a Java Primitive-Array to a {@code ReadOnlyTreeSet<E>}, where {@code 'E'} is the * Java Boxed-Type which corresponds to the Primitive-Array's Type. * * <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CTOR_DESC> * @param primitiveArray <EMBED CLASS='external-html' DATA-FILE-ID=PRIMITIVE_ARRAY> * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CCEX> * @throws NullPointerException If {@code 'primitiveArray'} is passed null; */ public ReadOnlyTreeSet(Object primitiveArray, Comparator<? super E> comparator) { fromBuilderOrTreeSet = false; TreeSet<E> ts = ROHelpers.buildROListOrSet( primitiveArray, (int arrayLen) -> (comparator != null) ? new TreeSet<E>(comparator) : new TreeSet<E>(), null ); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (ts.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : ts; } /** * Converts a Java Primitive-Array to a {@code ReadOnlyTreeSet<E>}, where {@code 'E'} is the * Java Boxed-Type which corresponds to the Primitive-Array's Type - <I>but also accepts a * {@code 'filter'} that can remove any array-entries that need to be removed</I>. * * <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CTOR_DESC> * @param primitiveArray <EMBED CLASS='external-html' DATA-FILE-ID=PRIMITIVE_ARRAY> * @param filter <EMBED CLASS='external-html' DATA-FILE-ID=PRED_FILT_PRIM> * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CCEX> * @throws NullPointerException If {@code 'primitiveArray'} is passed null; */ public ReadOnlyTreeSet( Object primitiveArray, Comparator<? super E> comparator, Predicate<?> filter ) { Objects.requireNonNull(filter, ROHelpers.NULL_MSG + "'filter'"); fromBuilderOrTreeSet = false; TreeSet<E> ts = ROHelpers.buildROListOrSet( primitiveArray, (int arrayLen) -> (comparator != null) ? new TreeSet<E>(comparator) : new TreeSet<E>(), filter ); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeSet = (ts.size() == 0) ? ((TreeSet<E>) EMPTY_TREE_SET) : ts; } // ******************************************************************************************** // ******************************************************************************************** // java.util.stream.Stream HELPER // ******************************************************************************************** // ******************************************************************************************** /** * For use with a the Java Stream method {@code 'collect(Collector c)'}. * * <EMBED CLASS='external-html' DATA-ABBREV=ts DATA-FILE-ID=STREAM_COLLECTOR> * * @param <T> This is the Generic-Type of the Input-Stream. It will also be the Generic-Type * of the {@code ReadOnlyTreeSet} that's returned from the stream's {@code collect} method. * * @param characteristics Optional Characteristics List. See Java Stream-API Documentation on * {@code Collector.Characteristics} inner-class for more details. * * @return This returns a collector that may be piped into a stream's {@code 'collect'} method, * as in the example, above. */ public static <T> java.util.stream.Collector <T, ROTreeSetBuilder<T>, ReadOnlyTreeSet<T>> streamCollector(Collector.Characteristics... characteristics) { return Collector.of( ROTreeSetBuilder<T>::new, // The "Supplier" (builds a new ROTreeSetBuilder) ROTreeSetBuilder<T>::add, // The "Accumulator" (adds elements to the builder) // Oracle Making Life Difficult - It should be the line below, but, alas, it is not! // ROTreeSetBuilder<T>::addAll, // The "Combiner" (combines multiple ROTreeSetBuilders) // // In Stream.collect(), the 3rd parameter - the "combiner" - is a "BiConsumer<R, R>" // NOTE: A "BiConsumer" is a FunctionalInterface that does not return anything - it is // (obviously) a "void" return method! // // **BUT** // // In Collector.of, the 3rd parameter - the "combiner" - is a "BinaryOperation<R>" (ROTreeSetBuilder<T> rotsb1, ROTreeSetBuilder<T> rotsb2) -> { rotsb1.addAll(rotsb2); return rotsb1; }, ROTreeSetBuilder<T>::build, // The "Finisher" (Converts Builder to ReadOnlyTreeSet) characteristics ); } // ******************************************************************************************** // ******************************************************************************************** // Convert to java.util Types // ******************************************************************************************** // ******************************************************************************************** /** * Clone's {@code 'this'} instance internal {@code TreeSet<E>} field, and returns it. * <EMBED CLASS='external-html' DATA-TYPE=TreeSet DATA-FILE-ID=CLONE_TO> * * @return An independent, mutable copy of {@code 'this'} instance' internal {@code TreeSet<E>} * data-structure. */ public TreeSet<E> cloneToTreeSet() { return fromBuilderOrTreeSet ? new TreeSet<E>(this.treeSet) : (TreeSet<E>) this.treeSet.clone(); } /** * Invokes {@code java.util.Collections.unmodifiableSet} on the internal {@code TreeSet}. * <EMBED CLASS='external-html' DATA-RET_TYPE=Set DATA-FILE-ID=WRAP_TO_IMMUTABLE> * * @return A {@code Set} which adheres to the JDK interface {@code java.util.Set}, but throws * an {@code UnsupportedOperationException} if a user attempts to invoke a Mutator-Method on * the returned instance. */ public Set<E> wrapToImmutableSet() { return Collections.unmodifiableSet(this.treeSet); } // ******************************************************************************************** // ******************************************************************************************** // Original JDK Methods, java.util.TreeSet // ******************************************************************************************** // ******************************************************************************************** /** * Returns an iterator over the elements in this set in ascending order. * @return an iterator over the elements in this set in ascending order */ public RemoveUnsupportedIterator<E> iterator() { return new RemoveUnsupportedIterator<>(this.treeSet.iterator()); } /** * Returns an iterator over the elements in this set in descending order. * @return an iterator over the elements in this set in descending order */ public RemoveUnsupportedIterator<E> descendingIterator() { return new RemoveUnsupportedIterator<>(this.treeSet.descendingIterator()); } public ReadOnlyNavigableSet<E> descendingSet() { return InterfaceBuilder.toReadOnlyNavigableSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._descendingSet(friendClassBadge) : this.treeSet.descendingSet() ); } /** * Returns the number of elements in this set (its cardinality). * @return the number of elements in this set (its cardinality) */ public int size() { return this.treeSet.size(); } /** * Returns {@code TRUE} if this set contains no elements. * @return {@code TRUE} if this set contains no elements */ public boolean isEmpty() { return this.treeSet.isEmpty(); } /** * Returns {@code TRUE} if this set contains the specified element. More formally, returns * {@code TRUE} if and only if this set contains an element {@code e} such that * {@code Objects.equals(o, e)}. * * @param o object to be checked for containment in this set * @return {@code TRUE} if this set contains the specified element * * @throws ClassCastException if the specified object cannot be compared with the elements * currently in the set * * @throws NullPointerException if the specified element is null and this set uses natural * ordering, or its comparator does not permit null elements */ public boolean contains(Object o) { return this.treeSet.contains(o); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code fromElement} or {@code toElement} is null and this * set uses natural ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlyNavigableSet<E> subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive ) { return InterfaceBuilder.toReadOnlyNavigableSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._subSet (fromElement, fromInclusive, toElement, toInclusive, friendClassBadge) : this.treeSet.subSet(fromElement, fromInclusive, toElement, toInclusive) ); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code toElement} is null and this set uses natural * ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlyNavigableSet<E> headSet(E toElement, boolean inclusive) { return InterfaceBuilder.toReadOnlyNavigableSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._headSet (toElement, inclusive, friendClassBadge) : this.treeSet.headSet(toElement, inclusive) ); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code fromElement} is null and this set uses natural * ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlyNavigableSet<E> tailSet(E fromElement, boolean inclusive) { return InterfaceBuilder.toReadOnlyNavigableSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._tailSet (fromElement, inclusive, friendClassBadge) : this.treeSet.tailSet(fromElement, inclusive) ); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code fromElement} or {@code toElement} is null and this * set uses natural ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlySortedSet<E> subSet(E fromElement, E toElement) { return InterfaceBuilder.toReadOnlySortedSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._subSet (fromElement, toElement, friendClassBadge) : this.treeSet.subSet(fromElement, toElement) ); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code toElement} is null and this set uses natural * ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlySortedSet<E> headSet(E toElement) { return InterfaceBuilder.toReadOnlySortedSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._headSet(toElement, friendClassBadge) : this.treeSet.headSet(toElement) ); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if {@code fromElement} is null and this set uses natural * ordering, or its comparator does not permit null elements * * @throws IllegalArgumentException {@inheritDoc} */ public ReadOnlySortedSet<E> tailSet(E fromElement) { return InterfaceBuilder.toReadOnlySortedSet( fromBuilderOrTreeSet ? ((ROTreeSetBuilder<E>) this.treeSet)._tailSet(fromElement, friendClassBadge) : this.treeSet.tailSet(fromElement) ); } public Comparator<? super E> comparator() { return this.treeSet.comparator(); } /** @throws NoSuchElementException {@inheritDoc} */ public E first() { return this.treeSet.first(); } /** @throws NoSuchElementException {@inheritDoc} */ public E last() { return this.treeSet.last(); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if the specified element is null and this set uses natural * ordering, or its comparator does not permit null elements */ public E lower(E e) { return this.treeSet.lower(e); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if the specified element is null and this set uses natural * ordering, or its comparator does not permit null elements */ public E floor(E e) { return this.treeSet.floor(e); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if the specified element is null and this set uses natural * ordering, or its comparator does not permit null elements */ public E ceiling(E e) { return this.treeSet.ceiling(e); } /** * @throws ClassCastException {@inheritDoc} * * @throws NullPointerException if the specified element is null and this set uses natural * ordering, or its comparator does not permit null elements */ public E higher(E e) { return this.treeSet.higher(e); } /** * Utilizes the private, internal {@code java.util.TreeSet} field to generate a * {@code Spliterator}. * * @return a {@code Spliterator} over the elements in this set */ public Spliterator<E> spliterator() { return this.treeSet.spliterator(); } // ******************************************************************************************** // ******************************************************************************************** // Misc Stuff that is defined in a super-class - for java.util, but not for Java.ReadOnly // ******************************************************************************************** // ******************************************************************************************** public boolean containsAll(Collection<?> c) { return this.treeSet.containsAll(c); } public <T> T[] toArray(T[] a) { return this.treeSet.toArray(a); } public Object[] toArray() { return this.treeSet.toArray(); } // ******************************************************************************************** // ******************************************************************************************** // java.lang.Object // ******************************************************************************************** // ******************************************************************************************** /** * Returns a {@code String} representation of this {@code TreeSet}. The {@code String} * representation consists of a list of the collection's elements in the order they are * returned by its iterator, enclosed in square brackets ({@code "[]"}). Adjacent elements are * separated by the characters {@code ", "} (comma and space). Elements are converted to * {@code String's} as by {@code String.valueOf(Object)}. * * @return a {@code String} representation of this {@code TreeSet} */ public String toString() { return this.treeSet.toString(); } /** * Compares the specified Object with this Set for equality, as per the definition in the * class {@code java.util.TreeSet}. * * @param o object to be compared for equality with this {@code ReadOnlyTreeSet}. * @return {@code TRUE} if the specified Object is equal to this set */ public boolean equals(Object o) { return ROHelpers.roSetEq(this, o); } /** * Returns the hash code value for this Set as per the definition in the class * {@code java.util.TreeSet}. */ public int hashCode() { return this.treeSet.hashCode(); } } |