On Fri, 9 Dec 2022 18:35:57 GMT, John Hendrikx <jhendr...@openjdk.org> wrote:

>> It's safe as you're never actually using the type `E`, but I suppose the 2nd 
>> list can also be `List<?>` as only `equals` is called on it.  The 2nd 
>> `ListIterator` would then also be `ListIterator<?>`.
>> 
>> Casting to a specific generic type (like `List<E>`) never verifies if all 
>> the elements are of that type, you're just telling the compiler that they 
>> are.
>> 
>> Only when you do something like:
>> 
>>     E e = list.get(0);
>> 
>> ... you may get a CCE here (but the compiler allowed it because you casted 
>> it to `List<E>` earlier).  That's why I say it is safe because the follow up 
>> code does not do any such action (it in fact does `Object o2 = e2.next()` -- 
>> it would be bad if it was `E o2 = e2.next`).
>> 
>> Looking at it again, I think making the 2nd `List` a `List<?>` is better, as 
>> we don't know yet at that stage that they all are of type `E` (even though 
>> it doesn't matter for the code following it).
>
> It also removes the need for the `SuppressWarnings` here.

I suggest the following method:


    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof List<?> list)) {
            return false;
        }

        if (size() != list.size()) {
            return false;
        }

        ListIterator<E> e1 = listIterator();
        ListIterator<?> e2 = list.listIterator();
        while (e1.hasNext() && e2.hasNext()) {
            if (!Objects.equals(e1.next(), e2.next())) {
                return false;
            }
        }
        return true;
    }


Technically, there's no need to check `hasNext` on both since they have the 
same length, but it doesn't matter.

I also don't like the names `e1` and `e2` for list iterators :)

-------------

PR: https://git.openjdk.org/jfx/pull/969

Reply via email to