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

Equals methods rely on catching ClassCastException rather than using instanceof check

    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

          People

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

            Dates

              Created:
              Updated:
              Resolved: