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 | /* * Copyright (c) 1997, 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.io.Serializable; 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; import java.util.*; /** * Immutable Wrapper for <CODE>java.util.HashMap</CODE>, found in the "Java Collections * Framework". * * <EMBED CLASS=globalDefs DATA-JDK=HashMap DATA-ENDING=Map> * <EMBED CLASS='external-html' DATA-FILE-ID=DATA_CLASS> * * @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 ReadOnlyHashMap<K, V> implements ReadOnlyMap<K, V>, Serializable { // ******************************************************************************************** // ******************************************************************************************** // Protected & Private Fields, Methods, Statics // ******************************************************************************************** // ******************************************************************************************** /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; // Minor Optimization where new HashMap'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 HashMap EMPTY_HASH_MAP = new HashMap(0, 0.75f); // Singleton & Empty ReadOnlyHashMap, Uses the "Supplier Constructor" @SuppressWarnings("rawtypes") private static final ReadOnlyHashMap EMPTY_READONLY_HASH_MAP = new ReadOnlyHashMap(EMPTY_HASH_MAP); // The actual HashMap used by this instance. private final HashMap<K, V> hashMap; // TRUE => This was built using the class ROHashMapBuilder // FALSE => This was built using the clone() of a standard java.util.HashMap constructor private final boolean fromBuilderOrHashMap; // Mimics the C++ Keyword/Concept of "Friend Class". Is "Friends With" ROHashMapBuilder 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 ReadOnlyHashMap}. * @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> ReadOnlyHashMap<X, Y> emptyROHM() { return (ReadOnlyHashMap<X, Y>) EMPTY_READONLY_HASH_MAP; } // 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 // ROHashMapBuilder // // This is the Constructor used by the Builder. It has a "Zero-Size" Optimization ReadOnlyHashMap(ROHashMapBuilder<K, V> rohmb, ROHashMapBuilder.AccessBadge badge) { Objects.requireNonNull(badge, "Access Badge is null. Requires Friend-Class Badge"); fromBuilderOrHashMap = true; this.hashMap = rohmb; } // ******************************************************************************************** // ******************************************************************************************** // 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 'hashMap'} field. */ public ReadOnlyHashMap(Map<K, V> map) { fromBuilderOrHashMap = false; // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (map.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : new HashMap<>(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 ReadOnlyHashMap}, 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 ReadOnlyHashMap}. * * @param map Any Java {@code Map}. * * @param mapTranslator A function for mapping the iterated elements of Map-Types {@code 'X'} * and {@code 'Y'}, into the actual {@code HashMap'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. * * @throws IllegalArgumentException if the load factor is nonpositive */ public <X, Y> ReadOnlyHashMap( 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'"); fromBuilderOrHashMap = false; HashMap<K, V> hashMap = (loadFactor != null) ? new HashMap<>(map.size(), loadFactor) : new HashMap<>(map.size()); if (filter != null) for (Map.Entry<X, Y> entry : map.entrySet()) { if (! filter.test(entry)) continue; mapTranslator.accept(entry); hashMap.put(refHolder.a, refHolder.b); } else for (Map.Entry<X, Y> entry : map.entrySet()) { mapTranslator.accept(entry); hashMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } // ******************************************************************************************** // ******************************************************************************************** // New Constructors, January 2024 // ******************************************************************************************** // ******************************************************************************************** /** * Constructs an instance of {@code ReadOnlyHashMap} 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 ReadOnlyHashMap( 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'"); fromBuilderOrHashMap = false; HashMap<K, V> hashMap = new HashMap<>( ((sizeIfKnown == null) ? 16 : sizeIfKnown), ((loadFactor == null) ? 0.75f : loadFactor) ); if (filter == null) for (K key : keys) hashMap.put(key, valueMapper.apply(key)); else for (K key : keys) if (filter.test(key)) hashMap.put(key, valueMapper.apply(key)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } /** * Constructs an instance of {@code ReadOnlyHashMap} 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 ReadOnlyHashMap( Tuple2<K, V> refHolder, Runnable computeNextEntry, Float loadFactor, Integer sizeIfKnown ) { Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); fromBuilderOrHashMap = false; HashMap<K, V> hashMap = new HashMap<>( ((sizeIfKnown == null) ? 16 : sizeIfKnown), ((loadFactor == null) ? 0.75f : loadFactor) ); do { computeNextEntry.run(); if ((refHolder.a == null) && (refHolder.b == null)) break; hashMap.put(refHolder.a, refHolder.b); } while (true); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } /** * Populates an instance of {@code ReadOnlyHashMap} 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 HashMap}. * * <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 <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 'refHolder'}, {@code 'computeNextEntry'} or * {@code 'source'} are passed null. */ public <X> ReadOnlyHashMap( 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'"); fromBuilderOrHashMap = false; HashMap<K, V> hashMap = new HashMap<>( ((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()); hashMap.put(refHolder.a, refHolder.b); } else while (iter.hasNext()) if (filter.test(x = iter.next())) { computeNextEntry.accept(x); hashMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } // ******************************************************************************************** // ******************************************************************************************** // 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 ReadOnlyHashMap}. * * @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> ReadOnlyHashMap( 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.fromBuilderOrHashMap = false; HashMap<K, V> hashMap = new HashMap<>(elements.length, ((loadFactor == null) ? 0.75f : loadFactor)); if (filter == null) for (X e : elements) { computeNextEntry.accept(e); hashMap.put(refHolder.a, refHolder.b); } else for (X x : elements) if (filter.test(x)) { computeNextEntry.accept(x); hashMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } /** * Retrieves elements from the Java Primitive-Array parameter {@code 'primitiveArray'}, and * subsequently invokes the {@code 'computeNextEntry'} processor to populate this * {@code ReadOnlyHashMap}. * * @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> ReadOnlyHashMap( 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.fromBuilderOrHashMap = false; HashMap<K, V> hashMap = ROHelpers.buildROMap( primitiveArray, (int arrayLen) -> new HashMap<>(arrayLen, ((loadFactor == null) ? 0.75f : loadFactor)), filter, refHolder, computeNextEntry ); // Empty Optimization (throw away, completely, the reference, use static-constant) this.hashMap = (hashMap.size() == 0) ? ((HashMap<K, V>) EMPTY_HASH_MAP) : hashMap; } // ******************************************************************************************** // ******************************************************************************************** // Convert to java.util Types // ******************************************************************************************** // ******************************************************************************************** /** * Clone's {@code 'this'} instance internal {@code HashMap<K, V>} field, and returns it. * <EMBED CLASS='external-html' DATA-TYPE=HashMap DATA-FILE-ID=CLONE_TO> * * @return An independent, mutable copy of {@code 'this'} instance internal * {@code HashMap<K, V>} data-structure. */ public HashMap<K, V> cloneToHashMap() { if (! fromBuilderOrHashMap) return (HashMap<K, V>) this.hashMap.clone(); HashMap<K, V> ret = new HashMap<K, V>(); for (Map.Entry<K, V> e : ((ROHashMapBuilder<K, V>) this.hashMap)._entrySet(friendClassBadge)) ret.put(e.getKey(), e.getValue()); return ret; } /** * Invokes {@code java.util.Collections.unmodifiableMap} on the internal {@code HashMap}. * <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.hashMap); } // ******************************************************************************************** // ******************************************************************************************** // Original JDK Methods, java.util.HashMap // ******************************************************************************************** // ******************************************************************************************** /** * Returns the number of key-value mappings in this map. * @return the number of key-value mappings in this map */ public int size() { return this.hashMap.size(); } /** * Returns {@code TRUE} if this map contains no key-value mappings. * @return {@code TRUE} if this map contains no key-value mappings */ public boolean isEmpty() { return this.hashMap.isEmpty(); } /** * 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==null ? k==null : key.equals(k))}, then this method returns * {@code v}; otherwise it returns {@code null}. (There can be at most one such mapping.) * * <BR /><BR />A return value of {@code null} does not <i>necessarily</i> indicate that the map * contains no mapping for the key; it's also possible that the map explicitly maps the key to * {@code null}. The {@link #containsKey containsKey} operation may be used to distinguish * these two cases. */ public V get(Object key) { return this.hashMap.get(key); } /** * Returns {@code TRUE} if this map contains a mapping for the specified key. * @param key The key whose presence in this map is to be tested * @return {@code TRUE} if this map contains a mapping for the specified key. */ public boolean containsKey(Object key) { return this.hashMap.containsKey(key); } /** * Returns {@code TRUE} if this map maps one or more keys to the specified value. * @param value value whose presence in this map is to be tested * @return {@code TRUE} if this map maps one or more keys to the specified value */ public boolean containsValue(Object value) { return this.hashMap.containsValue(value); } // ******************************************************************************************** // ******************************************************************************************** // The Builder AccessDenied Issue // ******************************************************************************************** // ******************************************************************************************** /** * Returns a {@link ReadOnlySet} view of the keys contained in this map. * @return a set view of the keys contained in this map */ public ReadOnlySet<K> keySet() { return InterfaceBuilder.toReadOnlySet( fromBuilderOrHashMap ? ((ROHashMapBuilder<K, V>) this.hashMap)._keySet(friendClassBadge) : this.hashMap.keySet() ); } /** * Returns a {@link ReadOnlyCollection} view of the values contained in this map. * @return a view of the values contained in this map */ public ReadOnlyCollection<V> values() { return InterfaceBuilder.toReadOnlyCollection( fromBuilderOrHashMap ? ((ROHashMapBuilder<K, V>) this.hashMap)._values(friendClassBadge) : this.hashMap.values() ); } /** * Returns a {@link ReadOnlySet} view of the mappings contained in this map. * @return a set view of the mappings contained in this map */ public ReadOnlySet<ReadOnlyMap.Entry<K,V>> entrySet() { return ROHelpers.toReadOnlyEntrySet( fromBuilderOrHashMap ? ((ROHashMapBuilder<K, V>) this.hashMap)._entrySet(friendClassBadge) : this.hashMap.entrySet() ); } // ******************************************************************************************** // ******************************************************************************************** // Overrides of JDK8 Map extension methods / ReadOnlyMap // ******************************************************************************************** // ******************************************************************************************** @Override public V getOrDefault(Object key, V defaultValue) { return this.hashMap.getOrDefault(key, defaultValue); } @Override public void forEach(BiConsumer<? super K, ? super V> action) { this.hashMap.forEach(action); } // ******************************************************************************************** // ******************************************************************************************** // java.lang.Object // ******************************************************************************************** // ******************************************************************************************** /** * Returns a {@code String} representation of this map. The {@code String} representation * consists of a list of key-value mappings in the order returned by the map's entrySet view's * iterator, enclosed in braces ({@code "{}"}). Adjacent mappings are separated by the * characters {@code ", "} (comma and space). Each key-value mapping is rendered as the key * followed by an equals sign ({@code "="}) followed by the associated value. Keys and values * are converted to {@code String's} as by {@code String.valueOf(Object)}. * * @return a {@code String} representation of this {@code HashMap} */ public String toString() { return ROHelpers.toString( this.hashMap, // if the map contains itself, it is needed for printing purposes fromBuilderOrHashMap ? ((ROHashMapBuilder<K, V>) this.hashMap)._entrySet(friendClassBadge) : this.hashMap.entrySet() ); } /** * Compares the specified Object with this Map for equality, as per the definition in the * class {@code java.util.HashMap}. * * @param o object to be compared for equality with this {@code ReadOnlyHashMap}. * @return 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.HashMap}. */ public int hashCode() { return this.hashMap.hashCode(); } } |