You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Axel Howind (Jira)" <ji...@apache.org> on 2023/12/04 04:24:00 UTC

[jira] [Comment Edited] (PDFBOX-5724) CharStringCommand.equals() does not conform to the contract of Object.equals

    [ https://issues.apache.org/jira/browse/PDFBOX-5724?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17792626#comment-17792626 ] 

Axel Howind edited comment on PDFBOX-5724 at 12/4/23 4:23 AM:
--------------------------------------------------------------

Yes, since both keywords are enums, the if-block can be simplified to

    return that.type1Keyword == type1Keyword && that.type2Keyword == type2Keyword;

That would be a correct implementation according to Java contentions. I am just reluctant because whoever wrote the code seemed to have something else on his mind and I don’t know if there’s any reason  buried in the pdf spec to consider two instances equal maybe if one only has one of the keywords set. Anyway once equals() is changed, we need to look at hashCode() too.


was (Author: axh):
Yes, since both keywords are evens, the if-block can be simplified to

    return that.type1Keyword == type1Keyword && that.type2Keyword == type2Keyword;

That would be a correct implementation according to Java contentions. I am just reluctant because whoever wrote the code seemed to have something else on his mind and I don’t know if there’s any reason  buried in the pdf spec to consider two instances equal maybe if one only has one of the keywords set. Anyway once equals() is changed, we need to look at hashCode() too.

> CharStringCommand.equals() does not conform to the contract of Object.equals
> ----------------------------------------------------------------------------
>
>                 Key: PDFBOX-5724
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5724
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 4.0.0
>            Reporter: Axel Howind
>            Priority: Critical
>
> The equals() method in CharStringCommand breaks the contract of Object.equals(). From the Object.equals() Javadoc:
>  
> {quote}The {{equals}} method implements an equivalence relation on non-null object references:
>  * It is {_}reflexive{_}: for any non-null reference value {{{}x{}}}, {{x.equals(x)}} should return {{{}true{}}}.
>  * It is {_}symmetric{_}: for any non-null reference values {{x}} and {{{}y{}}}, {{x.equals(y)}} should return {{true}} if and only if {{y.equals(x)}} returns {{{}true{}}}.
>  * It is {_}transitive{_}: for any non-null reference values {{{}x{}}}, {{{}y{}}}, and {{{}z{}}}, if {{x.equals(y)}} returns {{true}} and {{y.equals(z)}} returns {{{}true{}}}, then {{x.equals(z)}} should return {{{}true{}}}.
>  * It is {_}consistent{_}: for any non-null reference values {{x}} and {{{}y{}}}, multiple invocations of {{x.equals(y)}} consistently return {{true}} or consistently return {{{}false{}}}, provided no information used in {{equals}} comparisons on the objects is modified.
>  * For any non-null reference value {{{}x{}}}, {{x.equals(null)}} should return {{{}false{}}}.{quote}
> This is the current implementation:
> {code:java}
>     @Override
>     public boolean equals(Object object)
>     {
>         if (object instanceof CharStringCommand)
>         {
>             CharStringCommand that = (CharStringCommand) object;
>             if (type1KeyWord != null && type1KeyWord == that.getType1KeyWord())
>             {
>                 return true;
>             }
>             if (type2KeyWord != null && type2KeyWord == that.getType2KeyWord())
>             {
>                 return true;
>             }
>             if (type1KeyWord == null && type2KeyWord == null)
>             {
>                 return true;
>             }
>         }
>         return false;
>     }
> {code}
> If type1Keyword==null and type2Keyword!=null, true is returned without checking the values of that.getType1Keyword() and that.getType2Keyword().
> Now imagine a has both fields set to null and b has not. Then a.equals(b)==true and b.equals(a)!=true.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org