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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 * 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.util.*;

import java.util.function.BiConsumer;
import java.io.Serializable;

/**
 * Immutable variant of Java Collections Framework interface
 * <CODE>java&#46;util&#46;Map&lt;K, V&gt;</CODE>.
 * 
 * <EMBED CLASS='external-html' DATA-JDK=ReadOnlyMap DATA-FILE-ID=INTERFACES>
 * 
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 */
@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JDHBI_INTERFACE")
public interface ReadOnlyMap<K, V>
{
    // ********************************************************************************************
    // ********************************************************************************************
    // Query Operations
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Returns the number of key-value mappings in this map.  If the map contains more than
     * {@code Integer.MAX_VALUE} elements, returns {@code Integer.MAX_VALUE}.
     *
     * @return the number of key-value mappings in this map
     */
    int size();

    /**
     * Returns {@code TRUE} if this map contains no key-value mappings.
     * @return {@code TRUE} if this map contains no key-value mappings
     */
    boolean isEmpty();

    /**
     * Returns {@code TRUE} if this map contains a mapping for the specified key.  More formally,
     * returns {@code TRUE} if and only if this map contains a mapping for a key {@code k} such
     * that {@code Objects.equals(key, k)}.  (There can be at most one such mapping.)
     *
     * @param key key whose presence in this map is to be tested
     * @return {@code TRUE} if this map contains a mapping for the specified key
     * 
     * @throws ClassCastException if the key is of an inappropriate type for this map
     * (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     * 
     * @throws NullPointerException if the specified key is null and this map does not permit null
     * keys (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     */
    boolean containsKey(Object key);

    /**
     * Returns {@code TRUE} if this map maps one or more keys to the specified value.  More
     * formally, returns {@code TRUE} if and only if this map contains at least one mapping to a
     * value {@code v} such that {@code Objects.equals(value, v)}.  This operation will probably
     * require time linear in the map size for most implementations of the {@code Map} interface.
     *
     * @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
     * 
     * @throws ClassCastException if the value is of an inappropriate type for
     * this map (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     * 
     * @throws NullPointerException if the specified value is null and this map does not permit
     * null values
     * (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     */
    boolean containsValue(Object value);

    /**
     * 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 Objects.equals(key, k)}, then this method returns {@code v};
     * otherwise it returns {@code null}.  (There can be at most one such mapping.)
     *
     * <BR /><BR />If this map permits null values, then 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.
     *
     * @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 ClassCastException if the key is of an inappropriate type for this map
     * (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     * 
     * @throws NullPointerException if the specified key is null and this map does not permit null
     * keys (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     */
    V get(Object 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.
     * 
     * @return a set view of the keys contained in this map
     */
    ReadOnlySet<K> keySet();

    /**
     * Returns a {@link ReadOnlyCollection} view of the values contained in this map.
     * @return a collection view of the values contained in this map
     */
    ReadOnlyCollection<V> values();

    /**
     * Returns a {@link ReadOnlySet} view of the mappings contained in this map.
     * @return a set view of the mappings contained in this map
     */
    ReadOnlySet<ReadOnlyMap.Entry<K, V>> entrySet();

    /**
     * A map entry (key-value pair). 
     * 
     * <BR /><BR />An Entry may also be obtained from a map's entry-set view by other means, for
     * example, using the
     * {@link ReadOnlySet#parallelStream parallelStream},
     * {@link ReadOnlySet#stream stream},
     * {@link ReadOnlySet#spliterator spliterator}
     * methods, any of the
     * {@link ReadOnlySet#toArray toArray}
     * overloads, or by copying the entry-set view into another collection.
     * 
     * <BR /><BR />In addition, an Entry may be obtained directly from a map, for example via calls
     * to methods directly on the {@link ReadOnlyNavigableMap} interface.
     * 
     * @param <K> the type of the key
     * @param <V> the type of the value
     *
     * @see ReadOnlyMap#entrySet()
     */
    interface Entry<K, V>
    {
        /**
         * Returns the key corresponding to this entry.
         * @return the key corresponding to this entry
         */
        K getKey();

        /**
         * Returns the value corresponding to this entry.
         * @return the value corresponding to this entry
         */
        V getValue();

        /**
         * Compares the specified object with this entry for equality.
         * Returns {@code TRUE} if the given object is also a map entry and
         * the two entries represent the same mapping.  More formally, two
         * entries {@code e1} and {@code e2} represent the same mapping
         * if
         * 
         * <BR /><DIV CLASS=SNIP>{@code
         * (e1.getKey()==null
         *      ? e2.getKey()==null
         *      : e1.getKey().equals(e2.getKey()))
         * &amp;&amp;
         * (e1.getValue()==null
         *      ? e2.getValue()==null
         *      : e1.getValue().equals(e2.getValue()))
         * }</DIV>
         * 
         * <BR /><BR />This ensures that the {@code equals} method works properly across different
         * implementations of the {@code ReadOnlyMap.Entry} interface.
         *
         * @param o object to be compared for equality with this map entry
         * @return {@code TRUE} if the specified object is equal to this map entry
         */
        boolean equals(Object o);

        /**
         * Returns the hash code value for this map entry.  The hash code of a map entry {@code e}
         * is defined to be:
         * 
         * <BR /><DIV CLASS=SNIP>{@code
         * (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
         * (e.getValue()==null ? 0 : e.getValue().hashCode())
         * }</DIV>
         * 
         * <BR /><BR />This ensures that {@code e1.equals(e2)} implies that
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries {@code e1} and {@code e2}, as
         * required by the general contract of {@code Object.hashCode}.
         *
         * @return the hash code value for this map entry
         * @see #equals(Object)
         */
        int hashCode();

        /**
         * Returns a comparator that compares {@link ReadOnlyMap.Entry} in natural order on key.
         *
         * <BR /><BR />The returned comparator is serializable and throws
         * {@code NullPointerException} when comparing an entry with a null key.
         *
         * @param  <K> the {@link Comparable} type of then map keys
         * @param  <V> the type of the map values
         * @return a comparator that compares {@link ReadOnlyMap.Entry} in natural order on key.
         */
        public static <K extends Comparable<? super K>, V>
            Comparator<ReadOnlyMap.Entry<K, V>>
            comparingByKey()
        {
            return (Comparator<ReadOnlyMap.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
        }

        /**
         * Returns a comparator that compares {@link ReadOnlyMap.Entry} in natural order on value.
         *
         * <BR /><BR />The returned comparator is serializable and throws
         * {@code NullPointerException} when comparing an entry with null values.
         *
         * @param <K> the type of the map keys
         * @param <V> the {@code Comparable} type of the map values
         * @return a comparator that compares {@link ReadOnlyMap.Entry} in natural order on value.
         * @see Comparable
         */
        public static <K, V extends Comparable<? super V>>
            Comparator<ReadOnlyMap.Entry<K, V>>
            comparingByValue()
        {
            return (Comparator<ReadOnlyMap.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
        }

        /**
         * Returns a comparator that compares {@link ReadOnlyMap.Entry} by key using the given
         * {@code Comparator}.
         *
         * <BR /><BR />The returned comparator is serializable if the specified comparator is also
         * serializable.
         *
         * @param  <K> the type of the map keys
         * @param  <V> the type of the map values
         * @param  cmp the key {@code Comparator}
         * @return a comparator that compares {@link ReadOnlyMap.Entry} by the key.
         */
        public static <K, V>
            Comparator<ReadOnlyMap.Entry<K, V>>
            comparingByKey(Comparator<? super K> cmp)
        {
            Objects.requireNonNull(cmp);
            return (Comparator<ReadOnlyMap.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
        }

        /**
         * Returns a comparator that compares {@link ReadOnlyMap.Entry} by value using the given
         * {@code Comparator}.
         * 
         * <BR /><BR />The returned comparator is serializable if the specified comparator is also
         * serializable.
         * 
         * @param  <K> the type of the map keys
         * @param  <V> the type of the map values
         * @param  cmp the value {@link Comparator}
         * @return a comparator that compares {@link ReadOnlyMap.Entry} by the value.
         */
        public static <K, V>
            Comparator<ReadOnlyMap.Entry<K, V>>
            comparingByValue(Comparator<? super V> cmp)
        {
            Objects.requireNonNull(cmp);
            return (Comparator<ReadOnlyMap.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
        }
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Comparison and hashing
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Compares the specified object with this map for equality.  Returns {@code TRUE} if the given
     * object is also a map and the two maps represent the same mappings.  More formally, two maps
     * {@code m1} and {@code m2} represent the same mappings if
     * {@code m1.entrySet().equals(m2.entrySet())}.  This ensures that the {@code equals} method
     * works properly across different implementations of the {@code Map} interface.
     * 
     * @param o object to be compared for equality with this map
     * @return {@code TRUE} if the specified object is equal to this map
     */
    boolean equals(Object o);

    /**
     * Returns the hash code value for this map.  The hash code of a map is defined to be the sum
     * of the hash codes of each entry in the map's {@code entrySet()} view.  This ensures that
     * {@code m1.equals(m2)} implies that {@code m1.hashCode()==m2.hashCode()} for any two maps
     * {@code m1} and {@code m2}, as required by the general contract of {@code Object.hashCode}.
     *
     * @return the hash code value for this map
     * @see ReadOnlyMap.Entry#hashCode()
     * @see #equals(Object)
     */
    int hashCode();


    // ********************************************************************************************
    // ********************************************************************************************
    // Defaultable methods
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Returns the value to which the specified key is mapped, or {@code defaultValue} if this map
     * contains no mapping for the key.
     *
     * @param key the key whose associated value is to be returned
     * @param defaultValue the default mapping of the key
     * 
     * @return the value to which the specified key is mapped, or {@code defaultValue} if this map
     * contains no mapping for the key
     * 
     * @throws ClassCastException if the key is of an inappropriate type for this map
     * (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     * 
     * @throws NullPointerException if the specified key is null and this map does not permit null
     * keys 
     * (<A HREF='ReadOnlyCollection.html#optional-restrictions'>optional-restrictions</A>)
     */
    default V getOrDefault(Object key, V defaultValue)
    {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

    /**
     * Performs the given action for each entry in this map until all entries have been processed
     * or the action throws an exception.   Unless otherwise specified by the implementing class,
     * actions are performed in the order of entry set iteration (if an iteration order is
     * specified.) Exceptions thrown by the action are relayed to the caller.
     * 
     * <BR /><BR />The default implementation is equivalent to, for this {@code map}:
     * 
     * <BR /><DIV CLASS=SNIP>{@code
     * for (ReadOnlyMap.Entry<K, V> entry : map.entrySet())
     *     action.accept(entry.getKey(), entry.getValue());
     * }</DIV>
     * 
     * <BR /><BR />The default implementation makes no guarantees about synchronization or
     * atomicity properties of this method. Any implementation providing atomicity guarantees must
     * override this method and document its concurrency properties.
     * 
     * @param action The action to be performed for each entry
     * @throws NullPointerException if the specified action is null
     */
    default void forEach(BiConsumer<? super K, ? super V> action)
    {
        Objects.requireNonNull(action);

        for (ReadOnlyMap.Entry<K, V> entry : entrySet())
        {
            K k;
            V v;

            try
            {
                k = entry.getKey();
                v = entry.getValue();
            }

            catch (IllegalStateException ise)
            {
                // this usually means the entry is no longer in the map.
                // throw new ConcurrentModificationException(ise);

                throw new Torello.Java.UnreachableError();
            }

            action.accept(k, v);
        }
    }

    /**
     * Copies the contents of {@code 'this'} ReadOnlyMap into the user-provided {@code 'map'}.
     * Utilizes the {@code java.util.Map} method {@code put(K key, V value)}.
     * 
     * @param map Any instance of java.util.Map
     * @see #forEach(BiConsumer)
     */
    default void copyIntoMap(Map<? super K, ? super V> map)
    { this.forEach((K k, V v) -> map.put(k, v)); }


    // ********************************************************************************************
    // ********************************************************************************************
    // MAP-KEYS: contains - using Var-Args Arrays
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains a matching key for every one of the elements</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * every element in {@code 'elements'}
     */
    default boolean containsKeyAND(Object... elements)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object elem : elements) if (! keys.contains(elem)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>does not contain any key that matches any of the elements</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * none of the elements in {@code 'elements'}
     */
    default boolean containsKeyNAND(Object... elements)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object elem : elements) if (keys.contains(elem)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains at least one key that matches at least one element</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * one or more of the elements in {@code 'elements'}
     */
    default boolean containsKeyOR(Object... elements)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object elem : elements) if (keys.contains(elem)) return true;
        return false;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains exactly one key that matches an element</B> in Var-Args
     * Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * precisely one element that is also in {@code 'elements'}
     */
    default boolean containsKeyXOR(Object... elements)
    {
        ReadOnlySet<K>  keys    = this.keySet();
        boolean         found   = false;

        for (Object elem : elements)

            if (keys.contains(elem))
            {
                if (found) return false;
                else found = true;
            }

        return found;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // MAP-KEYS: contains - using java.lang.Iterable
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains a matching key for every one of the elements</B> in
     * {@code Iterable} parameter {@code 'i'}.
     * 
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * every element in {@code 'i'}
     */
    default boolean containsKeyAND(Iterable<?> i)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object o: i) if (! keys.contains(o)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>does not contain any key that matches any of the elements</B> in
     * {@code Iterable} parameter {@code 'i'}
     *
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * none of the elements in {@code 'i'}
     */
    default boolean containsKeyNAND(Iterable<?> i)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object o: i) if (keys.contains(o)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains at least one key that matches at least one element</B> in
     * {@code Iterable} parameter {@code 'i'}
     *
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * one or more of the elements in {@code 'i'}
     */
    default boolean containsKeyOR(Iterable<?> i)
    {
        ReadOnlySet<K> keys = this.keySet();
        for (Object o: i) if (keys.contains(o)) return true;
        return false;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains exactly one key that matches an element</B> in 
     * {@code Iterable} parameter {@code 'i'}
     *
     * @param i any Java {@code Iterable}
     *  
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'keySet()'} contains
     * precisely one element that is also in {@code 'i'}
     */
    default boolean containsKeyXOR(Iterable<?> i)
    {
        ReadOnlySet<K>  keys    = this.keySet();
        boolean         found   = false;

        for (Object o: i)

            if (keys.contains(o))
            {
                if (found) return false;
                else found = true;
            }

        return found;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // MAP-VALUES: contains - using Var-Args Arrays
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains a matching value for every one of the elements</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * every element in {@code 'elements'}
     */
    default boolean containsValueAND(Object... elements)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object elem : elements) if (! values.contains(elem)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>does not contain any value that matches any of the elements</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * none of the elements in {@code 'elements'}
     */
    default boolean containsValueNAND(Object... elements)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object elem : elements) if (values.contains(elem)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains at least one value that matches at least one element</B> in
     * Var-Args Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * one or more of the elements in {@code 'elements'}
     */
    default boolean containsValueOR(Object... elements)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object elem : elements) if (values.contains(elem)) return true;
        return false;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains exactly one value that matches an element</B> in Var-Args
     * Parameter {@code 'elements'}
     * 
     * @param elements a list of elements
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * precisely one element that is also in {@code 'elements'}
     */
    default boolean containsValueXOR(Object... elements)
    {
        ReadOnlyCollection<V>   values  = this.values();
        boolean                 found   = false;

        for (Object elem : elements)

            if (values.contains(elem))
            {
                if (found) return false;
                else found = true;
            }

        return found;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // MAP-VALUES: contains - using java.lang.Iterable
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains a matching value for every one of the elements</B> in
     * {@code Iterable} parameter {@code 'i'}
     * 
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * every element in {@code 'i'}
     */
    default boolean containsValueAND(Iterable<?> i)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object o: i) if (! values.contains(o)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>does not contain any value that matches any of the elements</B> in
     * {@code Iterable} parameter {@code 'i'}
     * 
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * none of the elements in {@code 'i'}
     */
    default boolean containsValueNAND(Iterable<?> i)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object o: i) if (values.contains(o)) return false;
        return true;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains at least one value that matches at least one element</B> in
     * {@code Iterable} parameter {@code 'i'}
     *
     * @param i any Java {@code Iterable}
     * 
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * one or more of the elements in {@code 'i'}
     */
    default boolean containsValueOR(Iterable<?> i)
    {
        ReadOnlyCollection<V> values = this.values();
        for (Object o: i) if (values.contains(o)) return true;
        return false;
    }

    /**
     * Checks to ensure that {@code 'this'} instance of {@code ReadOnlyMap}
     * <B STYLE='color: red;'>contains exactly one value that matches an element</B> in 
     * {@code Iterable} parameter {@code 'i'}
     *
     * @param i any Java {@code Iterable}
     *  
     * @return {@code TRUE} If and only if {@code 'this'} instance' {@code 'values()'} contains
     * precisely one element that is also in {@code 'i'}
     */
    default boolean containsValueXOR(Iterable<?> i)
    {
        ReadOnlyCollection<V>   values  = this.values();
        boolean                 found   = false;

        for (Object o: i)

            if (values.contains(o))
            {
                if (found) return false;
                else found = true;
            }

        return found;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // "of" ...
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Returns an unmodifiable map containing zero mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @return an empty {@code Map}
     */
    @SuppressWarnings("unchecked")
    static <K, V> ReadOnlyMap<K, V> of()
    { return InterfaceBuilder.toReadOnlyMap(Map.of()); }

    /**
     * Returns an unmodifiable map containing a single mapping.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the mapping's key
     * @param v1 the mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mapping
     * @throws NullPointerException if the key or the value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(K k1, V v1)
    { return InterfaceBuilder.toReadOnlyMap(Map.of(k1, v1)); }

    /**
     * Returns an unmodifiable map containing two mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if the keys are duplicates
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(K k1, V v1, K k2, V v2)
    { return InterfaceBuilder.toReadOnlyMap(Map.of(k1, v1, k2, v2)); }

    /**
     * Returns an unmodifiable map containing three mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3)
    { return InterfaceBuilder.toReadOnlyMap(Map.of(k1, v1, k2, v2, k3, v3)); }

    /**
     * Returns an unmodifiable map containing four mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
    { return InterfaceBuilder.toReadOnlyMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4)); }

    /**
     * Returns an unmodifiable map containing five mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     *
     */
    static <K, V> ReadOnlyMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
    { return InterfaceBuilder.toReadOnlyMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); }

    /**
     * Returns an unmodifiable map containing six mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * @param k6 the sixth mapping's key
     * @param v6 the sixth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(
            K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
            K k6, V v6
        )
    {
        return InterfaceBuilder.toReadOnlyMap
            (Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6));
    }

    /**
     * Returns an unmodifiable map containing seven mappings.
     * 
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * @param k6 the sixth mapping's key
     * @param v6 the sixth mapping's value
     * @param k7 the seventh mapping's key
     * @param v7 the seventh mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(
            K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
            K k6, V v6, K k7, V v7
        )
    {
        return InterfaceBuilder.toReadOnlyMap
            (Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7));
    }

    /**
     * Returns an unmodifiable map containing eight mappings.
     * 
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * @param k6 the sixth mapping's key
     * @param v6 the sixth mapping's value
     * @param k7 the seventh mapping's key
     * @param v7 the seventh mapping's value
     * @param k8 the eighth mapping's key
     * @param v8 the eighth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(
            K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
            K k6, V v6, K k7, V v7, K k8, V v8
        )
    {
        return InterfaceBuilder.toReadOnlyMap
            (Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8));
    }

    /**
     * Returns an unmodifiable map containing nine mappings.
     * 
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * @param k6 the sixth mapping's key
     * @param v6 the sixth mapping's value
     * @param k7 the seventh mapping's key
     * @param v7 the seventh mapping's value
     * @param k8 the eighth mapping's key
     * @param v8 the eighth mapping's value
     * @param k9 the ninth mapping's key
     * @param v9 the ninth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(
            K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
            K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9
        )
    {
        return InterfaceBuilder.toReadOnlyMap
            (Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9));
    }

    /**
     * Returns an unmodifiable map containing ten mappings.
     *
     * @param <K> the {@code Map}'s key type
     * @param <V> the {@code Map}'s value type
     * @param k1 the first mapping's key
     * @param v1 the first mapping's value
     * @param k2 the second mapping's key
     * @param v2 the second mapping's value
     * @param k3 the third mapping's key
     * @param v3 the third mapping's value
     * @param k4 the fourth mapping's key
     * @param v4 the fourth mapping's value
     * @param k5 the fifth mapping's key
     * @param v5 the fifth mapping's value
     * @param k6 the sixth mapping's key
     * @param v6 the sixth mapping's value
     * @param k7 the seventh mapping's key
     * @param v7 the seventh mapping's value
     * @param k8 the eighth mapping's key
     * @param v8 the eighth mapping's value
     * @param k9 the ninth mapping's key
     * @param v9 the ninth mapping's value
     * @param k10 the tenth mapping's key
     * @param v10 the tenth mapping's value
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * @throws NullPointerException if any key or value is {@code null}
     */
    static <K, V> ReadOnlyMap<K, V> of(
            K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
            K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10
        )
    {
        return InterfaceBuilder.toReadOnlyMap(
            Map.of(
                k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6,
                k7, v7, k8, v8, k9, v9, k10, v10
            ));
    }

    /**
     * Returns an unmodifiable map containing keys and values extracted from the given entries.
     * The entries themselves are not stored in the map.
     *
     * <BR /><BR />It is convenient to create the map entries using the
     * {@link ReadOnlyMap#entry ReadOnlyMap.entry()} method.  For example,
     *
     * <DIV CLASS=EXAMPLE>{@code
     * import static Torello.Java.ReadOnly.ReadOnlyMap.entry;
     *
     * ReadOnlyMap<Integer,String> map = ReadOnlyMap.ofEntries(
     *      entry(1, "a"),
     *      entry(2, "b"),
     *      entry(3, "c"),
     *      ...
     *      entry(26, "z"));
     * }</DIV>
     *
     * @param <K> the {@link ReadOnlyMap}'s key type
     * @param <V> the {@link ReadOnlyMap}'s value type
     * 
     * @param entries {@code ReadOnlyMap.Entry}s containing the keys and values from which the map
     * is populated
     * 
     * @return a {@link ReadOnlyMap} containing the specified mappings
     * @throws IllegalArgumentException if there are any duplicate keys
     * 
     * @throws NullPointerException if any entry, key, or value is {@code null}, or if the
     * {@code entries} array is {@code null}
     *
     * @see ReadOnlyMap#entry Map.entry()
     */
    @SafeVarargs
    @SuppressWarnings("varargs")
    static <K, V>
        ReadOnlyMap<K, V>
        ofEntries
        (ReadOnlyMap.Entry<? extends K, ? extends V>... entries)
    {
        // *** Java-HTML: This arbitrarily uses TreeMap
        ROTreeMapBuilder<K, V> b = new ROTreeMapBuilder<>();

        for (ReadOnlyMap.Entry<? extends K, ? extends V> e : entries)
            b.put(e.getKey(), e.getValue());

        return b.build();
    }

    /**
     * Returns an unmodifiable {@link Entry} containing the given key and value.
     * These entries are suitable for populating {@code ReadOnlyMap} instances using the
     * {@link ReadOnlyMap#ofEntries ReadOnlyMap.ofEntries()} method.
     * 
     * @param <K> the key's type
     * @param <V> the value's type
     * @param k the key
     * @param v the value
     * @return an {@code Entry} containing the specified key and value
     * @throws NullPointerException if the key or value is {@code null}
     *
     * @see ReadOnlyMap#ofEntries Map.ofEntries()
     */
    static <K, V> ReadOnlyMap.Entry<K, V> entry(K k, V v)
    {
        // KeyValueHolder checks for nulls
        // return new KeyValueHolder<>(k, v);
        return new EntryImpl<>(Objects.requireNonNull(k), Objects.requireNonNull(v));
    }
}