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 | package Torello.Java.ReadOnly; import java.util.Map; import java.util.Set; import java.util.Iterator; class ROHelperEntrySet { // Copied directly from java.util.AbstractMap (on GitHub) // // NOTE: I didn't exactly get it, but when I switched from the <?, ?> to the <A, B> Type // variables it seemed to correct a certain error. // I don't like it when I cannot figure out what just happened with Generics, but this // time, I really COULD NOT figure out why javac like <A, B> better than <?, ?> static <A, B> String toString(Map<A, B> m, Set<Map.Entry<A, B>> entrySet) { Iterator<Map.Entry<A, B>> i = entrySet /* () */.iterator(); if (! i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append('{'); for (;;) { Map.Entry<A, B> e = i.next(); A key = e.getKey(); B value = e.getValue(); sb.append(key == /* this */ m ? "(this Map)" : key); sb.append('='); sb.append(value == /* this */ m ? "(this Map)" : value); if (! i.hasNext()) return sb.append('}').toString(); sb.append(',').append(' '); } } // *** Java-HTML: This thing arbitrarily uses a HashSet // Currently this is used **ONCE** inside InterfaceBuilder // It is also used in each of the 4 ReadOnlyMap's: TreeMap, HashMap, Hashtable, Properties // There it is used to help override the original "entrySet()" method. static <A, B> ReadOnlySet<ReadOnlyMap.Entry<A, B>> toReadOnlyEntrySet(Set<Map.Entry<A, B>> set) { return new ReadOnlyHashSet<ReadOnlyMap.Entry<A, B>> (set, e -> new EntryImpl<>(e), set.size(), null); // The new series of constructors makes the above line a little nicer, I guess? // Below is what was done, previously /* ROHashSetBuilder<ReadOnlyMap.Entry<A, B>> b = new ROHashSetBuilder<>(); for (Map.Entry<A, B> e : set) b.add(new EntryImpl<>(e)); return b.build(); */ } } |