Re: Bug in org.apache.commons.lang3.tuple.Pair
sebb <[email protected]> Thu, 26 Sep 2024 14:16:31 +0100
| Newsgroups | gmane.comp.jakarta.commons.user |
|---|---|
| Message-ID | <CAOGo0VaYepfLy6h4o6O3injYSqu5Vj6xhvFjRYsmcYH78-7LgA@mail.gmail.com> |
Looks like the equals implementation used to check for a Pair. AFAICT it was changed as part of this commit: https://github.com/apache/commons-lang/commit/37fa5d6fa470c2a9879caf64b47b8e535753ebdb On Thu, 26 Sept 2024 at 14:12, Ruby Paasche <[email protected]> wrote: > There is no magic, it simply fallback to Object.equals as usually. Which > compares the object references. So it will only return true of it is the > same exact instance. > > Dávid Szigecsán <[email protected]> schrieb am Do., 26. Sept. 2024, 14:41: > >> Well, we both were a bit mistaken. >> I did the following test. >> ``` >> @Test >> public void run() { >> Pair pair = Pair.of("a", "b"); >> Map.Entry<String, String> entry = new Map.Entry<String, String>() >> { >> public String getKey() { return "a"; } >> public String getValue() { return "b"; } >> public String setValue(String value) { return null; } >> }; >> Map.Entry<String, String> entry2 = new Map.Entry<String, >> String>() { >> public String getKey() { return "a"; } >> public String getValue() { return "b"; } >> public String setValue(String value) { return null; } >> }; >> Assert.assertTrue(pair.equals(entry)); // true >> Assert.assertTrue(pair.equals(entry2)); // true >> Assert.assertTrue(entry.equals(entry)); // true >> Assert.assertTrue(entry2.equals(entry2)); // true >> Assert.assertTrue(entry.equals(entry2)); // false >> Assert.assertTrue(entry.equals(pair)); // false >> } >> ``` >> You are right about that it is an interface and it seems it has no default >> implementation, but it does some magic. >> Every instance of Map.Entry equals to itself, but not equals of something >> that is created the same way. >> >> >> Gary D. Gregory <[email protected]> ezt írta (időpont: 2024. szept. >> 26., >> Cs, 14:26): >> >> > >> > >> ‑‑ >> Ruby Paasche >> Senior Entwicklerin >> [image: LinkedIn] <https://www.linkedin.com/company/pripares-gmbh> >> PRIPARES Software Solutions GmbH >> Ridlerstraße 57 >> 80339 München >> Tel: >> +49 (0)89 45 22 808 ‑ 30 >> Email: >> [email protected] >> Web: >> https://pripares.com >> Handelsregister: Registergericht München HRB 138701 >> Sitz der Gesellschaft: München >> Geschäftsführer: Aßmann Christoph, Ertl Andreas >> >> >> Diese E‑Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E‑Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und löschen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen sind nicht gestattet. >> >> >> This e‑mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e‑mail in error) >> >> please notify the sender immediately and delete this e‑mail. Any unauthorized copying, disclosure or distribution of the material in this e‑mail is strictly forbidden. >> > On 2024/09/26 11:36:49 Dávid Szigecsán wrote: >> > ... >> > > Map.Entry.equals() howewer checks if the other is an instance of the >> same >> > > class. >> > >> > Dávid, >> > >> > It does not, in Java 8 and 23, the method is abstract and does not >> define >> > code, but the Javadoc does define expected behavior. >> > >> > I imagine that it was not changed to a default method for compatibility. >> > >> > Gary >> > >> > So if we want them to be equals, we should change the Map.Entry, >> > > that is out of our limit of power. To make it symmetric we could check >> > for >> > > the instances of the same class also (and return false in this case), >> > but I >> > > think we don't want this. >> > > >> > > Dávid >> > > >> > > Alex Tsvetkov <[email protected]> ezt írta (időpont: 2024. >> szept. >> > 26., >> > > Cs 13:11): >> > > >> > > > Hi. >> > > > >> > > > I found a bug in the implementation of the method `equals` of class >> > `Pair`. >> > > > >> > > > Implementation must be symmetric. Current implementation is not. >> > > > >> > > > Her test showing the problem: >> > > > >> > > > ``` >> > > > >> > > > @Test >> > > > void run() { >> > > > var pair = Pair.of("a", "b"); >> > > > var entry = new Map.Entry<String, String>() { >> > > > public String getKey() { return "a"; } >> > > > public String getValue() { return "b"; } >> > > > public String setValue(String value) { return null; } >> > > > }; >> > > > assertTrue(pair.equals(entry)); // true >> > > > assertTrue(entry.equals(pair)); // false >> > > > } >> > > > ``` >> > > > >> > > >> > >> > --------------------------------------------------------------------- >> > To unsubscribe, e-mail: [email protected] >> > For additional commands, e-mail: [email protected] >> > >> > >> >