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

Equals methods rely on catching ClassCastException rather than using instanceof check

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.1
    • Labels:
      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

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

              Dates

              • Created:
                Updated:
                Resolved: