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 | /* * Copyright (c) 1994, 2023, 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 java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Function; import Torello.Java.Additional.Tuple2; /** * Immutable Wrapper for <CODE>java.util.Hashtable</CODE>, found in the "Java Collections * Framework". * * <EMBED CLASS=globalDefs DATA-JDK=Hashtable DATA-ENDING=table> * <EMBED CLASS='external-html' DATA-FILE-ID=DATA_CLASS> * <EMBED CLASS='external-html' DATA-FILE-ID=RO_SYNCHRONIZED> * * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */ @Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JDHBI_MAIN") @SuppressWarnings("unchecked") public class ReadOnlyHashtable<K, V> implements ReadOnlyDictionary<K, V>, ReadOnlyMap<K, V>, Cloneable, java.io.Serializable { // ******************************************************************************************** // ******************************************************************************************** // Protected & Private Fields, Methods, Statics // ******************************************************************************************** // ******************************************************************************************** /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; // Minor Optimization where new Hashtable'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 Hashtable EMPTY_HASH_TABLE = new Hashtable(0, 0.75f); // Singleton & Empty ReadOnlyHashtable, Uses the "Supplier Constructor" @SuppressWarnings("rawtypes") private static final ReadOnlyHashtable EMPTY_READONLY_HASH_TABLE = new ReadOnlyHashtable(EMPTY_HASH_TABLE); // The actual Hashtable used by this instance. private final Hashtable<K, V> hashTable; // TRUE => This was built using the class ROHashtableBuilder // FALSE => This was built using the clone() of a standard java.util.Hashtable constructor private final boolean fromBuilderOrHashtable; // Mimics the C++ Keyword/Concept of "Friend Class". Is "Friends With" ROHashtableBuilder static class AccessBadge { private AccessBadge() { } } private static final AccessBadge friendClassBadge = new AccessBadge(); // ******************************************************************************************** // ******************************************************************************************** // Builder, Acess-Badge Constructor - and Static "Empty" getter (which is used by the builder) // ******************************************************************************************** // ******************************************************************************************** /** * Returns an empty, <B STYLE='color: red;'>singleton</B>, instance of * {@code ReadOnlyHashtable}. * * @param <X> Returned {@link ReadOnlyMap}'s Key-Type. * @param <Y> Returned {@link ReadOnlyMap}'s Value-Type. * * @return An empty map. Since this map is both empty & read-only, a raw-type singleton * will suffice for all operations offered by this clas. */ public static <X, Y> ReadOnlyHashtable<X, Y> emptyROHT() { return (ReadOnlyHashtable<X, Y>) EMPTY_READONLY_HASH_TABLE; } // Used by the **Builder** // 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 // ROHashtableBuilder // // This is the Constructor used by the Builder. It has a "Zero-Size" Optimization ReadOnlyHashtable(ROHashtableBuilder<K, V> rohtb, ROHashtableBuilder.AccessBadge badge) { Objects.requireNonNull(badge, "Access Badge is null. Requires Friend-Class Badge"); this.fromBuilderOrHashtable = true; this.hashTable = rohtb; } // SPECIAL CASE: ReadOnlyProperties inherits ReadOnlyHashtable // NOTE: ROPropertiesBuilder **DOES NOT** inherit ROHashtableBuilder // // The parent class of ReadOnlyProperties (a.k.a. this class) is completely unused, and the // private fields are not important. For inheritance purposes, though, this is the parent // class, and as such, it has to be initialized with something! ReadOnlyHashtable(ReadOnlyProperties.AccessBadge badge) { this.fromBuilderOrHashtable = true; // This is unused this.hashTable = null; // This is unused } // ******************************************************************************************** // ******************************************************************************************** // Modified-Original Constructors // ******************************************************************************************** // ******************************************************************************************** /** * Copies the contents of parameter {@code 'map'}, and saves saves it, thereby guaranteeing * {@code 'this'} instance is Read-Only and fully-shielded from outside modification. * * @param map The {@code map} to be copied into {@code 'this'} instance internal and private * {@code 'hashTable'} field. */ public ReadOnlyHashtable(Map<K, V> map) { this.fromBuilderOrHashtable = false; this.hashTable = (map.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : new Hashtable<>(map); } /** * If only a small amount of processing needs to be done on the contents of some Java * Map, and using an entire Builder-Class seems disproportionately complex - <I>this * constructor can convert any Java {@code Map} into a {@code ReadOnlyHashtable}, using * a simple {@code 'mapTranslator'}</I>. * * @param <X> The Key-Type of the User-Provided {@code Map}. * @param <Y> The Value-Type of the User-Provided {@code Map}. * * @param refHolder This must a non-null instance of {@link Tuple2}. The provided * {@code Consumer} is just that, a {@code 'Consumer'} rather than a {@code 'Function'}, since * the results of each translation must be assigned to the values inside this tuple in order * for them to be inserted into this {@code ReadOnlyHashtable}. * * @param map Any Java {@code Map}. * * @param mapTranslator A consumer for mapping the iterated elements of Map-Types {@code 'X'} * and {@code 'Y'}, into the actual {@code Hashtable's} Key-Type {@code 'K'}, and Value-Type * {@code 'V'}. The results of this translation must be placed into the fields inside * {@code 'refHolder'}. * * <BR /><BR />If this parameter is passed null, this method will throw a * {@code NullPointerException}. * * @param filter An optional filter that can be used to prevent & prohibit any chosen * elements from input {@code 'map'} from being inserted into {@code 'this'} * {@code ReadOnlyTreeMap}. * * <BR /><BR />This parameter may be passed null, and if it is, it will be silently ignored, * and all entries present inside {@code 'map'} will be processed and inserted into * {@code 'this'} * * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * * @throws NullPointerException if either parameter {@code 'i'} or parameter * {@code 'mapTranslator'} is passed null. */ public <X, Y> ReadOnlyHashtable( Tuple2<K, V> refHolder, Map<X, Y> map, Consumer<Map.Entry<X, Y>> mapTranslator, Predicate<Map.Entry<X, Y>> filter, Float loadFactor ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(mapTranslator, ROHelpers.NULL_MSG + "'mapTranslator'"); fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = (loadFactor != null) ? new Hashtable<>(map.size(), loadFactor) : new Hashtable<>(map.size()); if (filter == null) for (Map.Entry<X, Y> entry : map.entrySet()) { mapTranslator.accept(entry); hashTable.put(refHolder.a, refHolder.b); } else for (Map.Entry<X, Y> entry : map.entrySet()) { if (! filter.test(entry)) continue; mapTranslator.accept(entry); hashTable.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } // ******************************************************************************************** // ******************************************************************************************** // New Stuff, January 2024 // ******************************************************************************************** // ******************************************************************************************** /** * Constructs an instance of {@code ReadOnlyHashtable} that contains the keys present in * parameter {@code 'keys'}, and values generated by {@code 'valueMapper'} - using each of the * {@code 'keys'} as input. * * <EMBED CLASS='external-html' DATA-FILE-ID=LOOK_AT_IT> * * @param keys Any Java {@code Iterable} instance. * @param valueMapper A user provided function to compute a map value, based on a map key. * @param filter <EMBED CLASS='external-html' DATA-FILE-ID=MAP_ITERABLE_FILTER> * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * @param sizeIfKnown <EMBED CLASS='external-html' DATA-FILE-ID=MAP_TABLE_ICAPACITY> * * @throws NullPointerException if either {@code 'keys'} or {@code 'valueMapper'} are passed * null. */ public ReadOnlyHashtable( Iterable<? extends K> keys, Function<? super K, ? extends V> valueMapper, Predicate<? super K> filter, Float loadFactor, Integer sizeIfKnown ) { Objects.requireNonNull(keys, ROHelpers.NULL_MSG + "'keys'"); Objects.requireNonNull(valueMapper, ROHelpers.NULL_MSG + "'valueMapper'"); fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = new Hashtable<>( ((sizeIfKnown == null) ? 16 : sizeIfKnown), ((loadFactor == null) ? 0.75f : loadFactor) ); if (filter == null) for (K key : keys) hashTable.put(key, valueMapper.apply(key)); else for (K key : keys) if (filter.test(key)) hashTable.put(key, valueMapper.apply(key)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } /** * Constructs an instance of {@code ReadOnlyHashtable} that has been populated by the Key-Value * Pairs left in {@code 'refHolder'} by each invocation of the {@code Runnable} parameter * {@code 'computeNextEntry'}. Key-Value Pairs are inserted until an invocation of the * {@code Runnable} leaves null in {@code refHolder.a} and {@code refHolder.b} * * <EMBED CLASS='external-html' DATA-FILE-ID=LOOK_AT_IT> * * @param refHolder <EMBED CLASS='external-html' DATA-FILE-ID=REF_HOLDER> * @param computeNextEntry <EMBED CLASS='external-html' DATA-FILE-ID=COMPUTE_NEXT_RUN> * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * @param sizeIfKnown <EMBED CLASS='external-html' DATA-FILE-ID=MAP_TABLE_ICAPACITY> * * @throws NullPointerException if either {@code 'refHolder'} or {@code 'computeNextEntry'} are * passed null. */ public ReadOnlyHashtable( Tuple2<K, V> refHolder, Runnable computeNextEntry, Float loadFactor, Integer sizeIfKnown ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = new Hashtable<>( ((sizeIfKnown == null) ? 16 : sizeIfKnown), ((loadFactor == null) ? 0.75f : loadFactor) ); do { computeNextEntry.run(); if ((refHolder.a == null) && (refHolder.b == null)) break; hashTable.put(refHolder.a, refHolder.b); } while (true); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } /** * Populates an instance of {@code ReadOnlyHashtable} by iterating the input {@code 'source'} * iterable, and passing each value returned by that {@code Iterator} to the * {@code 'computeNextEntry'} Java {@code Consumer}. * * <BR /><BR />It is the programmer's responsibility to properly place each Key-Value Pair * that is intending to be inserted into the final {@code Map} instance into the * {@code 'refHolder'} instance. After each invocation of {@code 'computeNextEntry'}, this * constructor's logic will retrieve the values within {@code 'refHolder.a'} and * {@code 'refHolder.b'} and insert them into this instance internal {@code Hashtable}. * * <EMBED CLASS='external-html' DATA-FILE-ID=LOOK_AT_IT> * * @param <X> The type of the elements inside {@code 'source'} * @param source Any Java {@code Iterable} instance. * @param refHolder <EMBED CLASS='external-html' DATA-FILE-ID=REF_HOLDER> * @param computeNextEntry <EMBED CLASS='external-html' DATA-FILE-ID=COMPUTE_NEXT_CONS> * @param filter May be used to filter out some of the elements of {@code 'source'} * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * @param sizeIfKnown <EMBED CLASS='external-html' DATA-FILE-ID=MAP_TABLE_ICAPACITY> * * @throws NullPointerException if either {@code 'refHolder'}, {@code 'computeNextEntry'} or * {@code 'source'} are passed null. */ public <X> ReadOnlyHashtable( Iterable<X> source, Tuple2<K, V> refHolder, Consumer<? super X> computeNextEntry, Predicate<? super X> filter, Float loadFactor, Integer sizeIfKnown ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = new Hashtable<>( ((sizeIfKnown == null) ? 16 : sizeIfKnown), ((loadFactor == null) ? 0.75f : loadFactor) ); X x; // temp var Iterator<X> iter = source.iterator(); if (filter == null) while (iter.hasNext()) { computeNextEntry.accept(iter.next()); hashTable.put(refHolder.a, refHolder.b); } else while (iter.hasNext()) if (filter.test(x = iter.next())) { computeNextEntry.accept(x); hashTable.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } // ******************************************************************************************** // ******************************************************************************************** // Array Constructors, March 2024 // ******************************************************************************************** // ******************************************************************************************** /** * Retrieves elements from the VarArgs Generic-Array parameter {@code 'elements'}, and * subsequently invokes the {@code 'computeNextEntry'} processor to populate this * {@code ReadOnlyHashtable}. * * @param <X> The type of array parameter {@code 'elements'} * @param refHolder <EMBED CLASS='external-html' DATA-FILE-ID=REF_HOLDER> * @param computeNextEntry <EMBED CLASS='external-html' DATA-FILE-ID=ARR_COMPUTE_NEXT_CONS> * @param filter <EMBED CLASS='external-html' DATA-FILE-ID=MAP_ARRAY_FILTER> * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * @param elements Any Generic VarArgs-Array * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CCEX> * * @throws NullPointerException if either {@code 'refHolder'} or {@code 'computeNextEntry'} * are passed null */ public <X> ReadOnlyHashtable( Tuple2<K, V> refHolder, Consumer<? super X> computeNextEntry, Predicate<? super X> filter, Float loadFactor, X... elements ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); this.fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = new Hashtable<>(elements.length, ((loadFactor == null) ? 0.75f : loadFactor)); if (filter == null) for (X e : elements) { computeNextEntry.accept(e); hashTable.put(refHolder.a, refHolder.b); } else for (X x : elements) if (filter.test(x)) { computeNextEntry.accept(x); hashTable.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } /** * Retrieves elements from the Java Primitive-Array parameter {@code 'primitiveArray'}, and * subsequently invokes the {@code 'computeNextEntry'} processor to populate this * {@code ReadOnlyHashtable}. * * @param filter <EMBED CLASS='external-html' DATA-FILE-ID=PRED_FILT_PRIM> * @param computeNextEntry <EMBED CLASS='external-html' DATA-FILE-ID=ARR_COMPUTE_NEXT_CONS> * @param refHolder <EMBED CLASS='external-html' DATA-FILE-ID=REF_HOLDER> * @param loadFactor <EMBED CLASS='external-html' DATA-FILE-ID=HASH_LOAD_FACTOR> * @param primitiveArray Any Java Primitive-Array * @throws ClassCastException <EMBED CLASS='external-html' DATA-FILE-ID=PRIM_ARR_CCEX> * * @throws NullPointerException if either {@code 'refHolder'} or {@code 'computeNextEntry'} * are passed null * */ public <X> ReadOnlyHashtable( Predicate<?> filter, Consumer<?> computeNextEntry, Tuple2<K, V> refHolder, Float loadFactor, Object primitiveArray ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); this.fromBuilderOrHashtable = false; Hashtable<K, V> hashTable = ROHelpers.buildROMap( primitiveArray, (int arrayLen) -> new Hashtable<>(arrayLen, ((loadFactor == null) ? 0.75f : loadFactor)), filter, refHolder, computeNextEntry ); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashTable = (hashTable.size() == 0) ? ((Hashtable<K, V>) EMPTY_HASH_TABLE) : hashTable; } // ******************************************************************************************** // ******************************************************************************************** // Convert to java.util Types // ******************************************************************************************** // ******************************************************************************************** /** * Clone's {@code 'this'} instance internal {@code Hashtable<K, V>} field, and returns it. * <EMBED CLASS='external-html' DATA-TYPE=Hashtable DATA-FILE-ID=CLONE_TO> * * @return An independent, mutable copy of {@code 'this'} instance internal * {@code Hashtable<K, V>} data-structure. */ public Hashtable<K, V> cloneToHashtable() { if (! fromBuilderOrHashtable) return (Hashtable<K, V>) this.hashTable.clone(); Hashtable<K, V> ret = new Hashtable<K, V>(); for (Map.Entry<K, V> e : ((ROHashtableBuilder<K, V>) this.hashTable)._entrySet(friendClassBadge)) ret.put(e.getKey(), e.getValue()); return ret; } /** * Invokes {@code java.util.Collections.unmodifiableMap} on the internal {@code Hashtable}. * <EMBED CLASS='external-html' DATA-RET_TYPE=Map DATA-FILE-ID=WRAP_TO_IMMUTABLE> * * @return A {@code Map} which adheres to the JDK interface {@code java.util.Map}, but throws * an {@code UnsupportedOperationException} if a user attempts to invoke a Mutator-Method on * the returned instance. */ public Map<K, V> wrapToImmutableMap() { return Collections.unmodifiableMap(this.hashTable); } // ******************************************************************************************** // ******************************************************************************************** // Original JDK Methods, java.util.Hashtable // ******************************************************************************************** // ******************************************************************************************** /** * Returns the number of keys in this {@code Hashtable}. * @return the number of keys in this {@code Hashtable}. */ public int size() { return this.hashTable.size(); } /** * Tests if this {@code Hashtable} maps no keys to values. * * @return {@code TRUE} if this {@code Hashtable} maps no keys to values; {@code FALSE} * otherwise. */ public boolean isEmpty() { return this.hashTable.isEmpty(); } /** * Returns an enumeration of the keys in this {@code Hashtable}. Use the Enumeration methods * on the returned object to fetch the keys sequentially. * * @return an enumeration of the keys in this {@code Hashtable}. * * @see #elements() * @see #keySet() * @see ReadOnlyMap */ public Enumeration<K> keys() { return this.hashTable.keys(); } /** * Returns an enumeration of the values in this {@code Hashtable}. Use the Enumeration methods * on the returned object to fetch the elements sequentially. * * @return an enumeration of the values in this {@code Hashtable}. * * @see #keys() * @see #values() * @see ReadOnlyMap */ public Enumeration<V> elements() { return this.hashTable.elements(); } /** * Tests if some key maps into the specified value in this {@code Hashtable}. This operation * is more expensive than the {@link #containsKey containsKey} method. * * <BR /><BR />Note that this method is identical in functionality to * {@link #containsValue containsValue}, (which is part of the {@link ReadOnlyMap} interface. * * @param value a value to search for * * @return {@code TRUE} if and only if some key maps to the {@code value} argument in this * {@code Hashtable} as determined by the {@code equals} method; {@code false} otherwise. * * @throws NullPointerException if the value is {@code null} */ public boolean contains(Object value) { return this.hashTable.contains(value); } /** * Returns {@code TRUE} if this {@code Hashtable} maps one or more keys to this value. * * <BR /><BR />Note that this method is identical in functionality to {@link #contains}. * * @param value value whose presence in this {@code Hashtable} is to be tested * @return {@code TRUE} if this map maps one or more keys to the specified value * @throws NullPointerException if the value is {@code null} */ public boolean containsValue(Object value) { return this.hashTable.contains(value); } /** * Tests if the specified object is a key in this {@code Hashtable}. * * @param key possible key * * @return {@code TRUE} if and only if the specified object is a key in this {@code Hashtable}, * as determined by the {@code equals} method; {@code false} otherwise. * * @throws NullPointerException if the key is {@code null} * * @see #contains(Object) */ public boolean containsKey(Object key) { return this.hashTable.containsKey(key); } /** * Returns the value to which the specified key is mapped, or {@code null} if this map contains * no mapping for the key. * * <BR /><BR />More formally, if this map contains a mapping from a key {@code k} to a value * {@code v} such that {@code (key.equals(k))}, then this method returns {@code v}; otherwise * it returns {@code null}. (There can be at most one such mapping.) * * @param key the key whose associated value is to be returned * * @return the value to which the specified key is mapped, or {@code null} if this map contains * no mapping for the key * * @throws NullPointerException if the specified key is null */ public V get(Object key) { return this.hashTable.get(key); } // ******************************************************************************************** // ******************************************************************************************** // Views // ******************************************************************************************** // ******************************************************************************************** /** * Returns a {@link ReadOnlySet} view of the keys contained in this map. The set is backed by * the map, so changes to the map are reflected in the set, and vice-versa. */ public ReadOnlySet<K> keySet() { return InterfaceBuilder.toReadOnlySet( fromBuilderOrHashtable ? ((ROHashtableBuilder<K, V>) this.hashTable)._keySet(friendClassBadge) : this.hashTable.keySet() ); } /** Returns a {@link ReadOnlySet} view of the mappings contained in this map. */ public ReadOnlySet<ReadOnlyMap.Entry<K, V>> entrySet() { return ROHelpers.toReadOnlyEntrySet( fromBuilderOrHashtable ? ((ROHashtableBuilder<K, V>) this.hashTable)._entrySet(friendClassBadge) : this.hashTable.entrySet() ); } /** Returns a {@link ReadOnlyCollection} view of the values contained in this map. */ public ReadOnlyCollection<V> values() { return InterfaceBuilder.toReadOnlyCollection( fromBuilderOrHashtable ? ((ROHashtableBuilder<K, V>) this.hashTable)._values(friendClassBadge) : this.hashTable.values() ); } // ******************************************************************************************** // ******************************************************************************************** // Comparison and hashing // ******************************************************************************************** // ******************************************************************************************** public V getOrDefault(Object key, V defaultValue) { return this.hashTable.getOrDefault(key, defaultValue); } public void forEach(BiConsumer<? super K, ? super V> action) { this.hashTable.forEach(action); } // ******************************************************************************************** // ******************************************************************************************** // java.lang.Object // ******************************************************************************************** // ******************************************************************************************** /** * Returns a string representation of this {@code Hashtable} object in the form of a set of * entries, enclosed in braces and separated by the ASCII characters {@code " , "} (comma and * space). Each entry is rendered as the key, an equals sign {@code '='}, and the associated * element, where the {@code toString} method is used to convert the key and element to * {@code String's}. * * @return a {@code String} representation of this {@code Hashtable} */ public String toString() { return ROHelpers.toString( this.hashTable, // if the map contains itself, it is needed for printing purposes fromBuilderOrHashtable ? ((ROHashtableBuilder<K, V>) this.hashTable)._entrySet(friendClassBadge) : this.hashTable.entrySet() ); } /** * Compares the specified Object with this Map for equality, as per the definition in the * class {@code java.util.Hashtable}. * * @param o object to be compared for equality with this {@code ReadOnlyHashtable}. * @return {@code TRUE} if the specified Object is equal to this Map */ public boolean equals(Object o) { return ROHelpers.roMapEq(this, o); } /** * Returns the hash code value for this Map as per the definition in the class * {@code java.util.Hashtable}. */ public int hashCode() { return this.hashTable.hashCode(); } } |