Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-337

Equals methods rely on catching ClassCastException rather than using instanceof check

Rank to TopRank to BottomBulk Copy AttachmentsBulk Move AttachmentsVotersWatch issueWatchersConvert to sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0
    • 2.1
    • None
    • None

    Description

      Several of the equals methods rely on catching ClassCastException rather than using an instanceof check.

      For example:

      SimplexTableau.equals(Object){
        if (this == other) {
          return true;
        }
        if (other == null) {
          return false;
        }
        try {
            SimplexTableau rhs = (SimplexTableau) other;
            etc.
        } catch (ClassCastException ex) {
            return false;
        }
      }
      

      This is likely to be significantly slower if the cast fails. It would be cheaper to do the following:

      SimplexTableau.equals(Object){
        if (this == other) {
          return true;
        }
        if (!(other instanceof SimplexTableau)) {
          return false;
        }
        SimplexTableau rhs = (SimplexTableau) other;
        etc.
      }
      

      Note that the null check is no longer needed; it is replaced by the instanceof check.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            sebb Sebb
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment