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
package Torello.Java.ReadOnly;

import java.util.Iterator;

class ROHelperEquals
{
    // Borrowed from java.util.AbstractList
    static boolean roListEq(ReadOnlyList<?> rol1, Object other)
    {
        if (rol1 == other) return true;

        if (! (other instanceof ReadOnlyList)) return false;

        ReadOnlyList<?> rol2 = (ReadOnlyList<?>) other;

        Iterator<?> e1 = rol1.iterator();
        Iterator<?> e2 = rol2.iterator();

        while (e1.hasNext() && e2.hasNext())
        {
            Object o1 = e1.next();
            Object o2 = e2.next();

            if (o1 == null)
                { if (o2 != null) return false; }

            else if (! o1.equals(o2))
                return false;
        }

        return !(e1.hasNext() || e2.hasNext());
    }

    // Lifted from java.util.AbstractSet
    static boolean roSetEq(ReadOnlySet<?> ros1, Object other)
    {
        if (ros1 == other) return true;

        if (! (other instanceof ReadOnlySet)) return false;

        ReadOnlySet<?> ros2 = (ReadOnlySet<?>) other;

        if (ros1.size() != ros2.size()) return false;

        try
            { return ros1.containsAND(ros2); }

        catch (ClassCastException | NullPointerException unused)
            { return false; }
    }

    // Lifted from java.util.AbstractMap
    static boolean roMapEq(ReadOnlyMap<?, ?> rom1, Object other)
    {
        if (rom1 == other) return true;

        if (! (other instanceof ReadOnlyMap)) return false;

        ReadOnlyMap<?, ?> rom2 = (ReadOnlyMap<?, ?>) other;

        if (rom1.size() != rom2.size()) return false;

        try
        {
            for (ReadOnlyMap.Entry<?, ?> e : rom1.entrySet())
            {
                Object key   = e.getKey();
                Object value = e.getValue();

                if (value == null)
                {
                    if (! rom2.containsKey(key))    return false;
                    if (rom2.get(key) != null)      return false;
                }

                else if (! value.equals(rom2.get(key))) return false;
            }
        }

        catch (ClassCastException | NullPointerException unused)
            { return false; }

        return true;
    }


    // Copied from Method above (ROHelper.roSetEq) - almost identical, but slightly less
    // "restrictive"
    //
    // This is not actually used anywhere amongst the "Main Classes".  Instead there is JUST ONE
    // place in the "InterfaceBuilder" (Wrapper thing) where this is used
    // 
    // In this one: toReadOnlyCollection(final java.util.Collection<X> c)

    static boolean roCollectionEq(Object objOther, ReadOnlyCollection<?> objThis)
    {
        if (objThis == objOther) return true;

        if (!(objOther instanceof ReadOnlyCollection)) return false;

        ReadOnlyCollection<?> roc = (ReadOnlyCollection<?>) objOther;

        if (roc.size() != objThis.size()) return false;

        try
            { return objThis.containsAND(roc); }

        catch (ClassCastException | NullPointerException unused)
            { return false; }
    }

}