Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2332

GString's equals and compareTo behavior is not equivalent

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Won't Fix
    • 1.1-rc-3
    • None
    • None
    • None

    Description

      def g = 'g'
      def string = 'groovy'
      def gString = "${g}roovy"
      println gString.equals(string) // false
      println gString == string // true
      

      For classes implementing the Comparable interface, the result of == is based on the value of the compareTo method, not on the result of the equals method.

      Beside the fact that gString.equals(string) not being true for the example above is unintuitive and leads to obvious problems with collections, the fact the compareTo and thus gString == string behaves differently is a BIG problem. It does not follow the strong recommendation of the Comparable interface (http://java.sun.com/j2se/1.5.0/docs/api/) which can lead to big problems.

      Here is the code from the GString class.

       //   
          public boolean equals(Object that) {
              if (that instanceof GString) {
                  return equals((GString) that);
              }
              return false;
          }
      
          public boolean equals(GString that) {
              return toString().equals(that.toString());
          }
      
          public int compareTo(Object that) {
              return toString().compareTo(that.toString());
          }
      
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            hans_d Hans Dockter
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: