groovy
  1. groovy
  2. GROOVY-5429

Add excludes to @EqualsAndHashCode

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-beta-3, 1.8.7
    • Component/s: groovy-jdk
    • Labels:
      None
    • Number of attachments :
      0

      Description

      It would be useful to specify the fields to include or exclude in the @EqualsAndHashCode transformation. An example can be found in Project Lombok:

      http://projectlombok.org/features/EqualsAndHashCode.html

        Issue Links

          Activity

          Hide
          Paul King added a comment -

          Is there anything missing from the current includes and excludes that you need?

          Show
          Paul King added a comment - Is there anything missing from the current includes and excludes that you need?
          Hide
          Uri Moszkowicz added a comment -

          Maybe I'm missing something but the documentation doesn't indicate that the includes and excludes can be specified:

          http://groovy.codehaus.org/gapi/groovy/transform/EqualsAndHashCode.html

          It would be useful to be able to specify which members are excluded as in the example:

          @EqualsAndHashCode(exclude={"id", "shape"})
          04 public class EqualsAndHashCodeExample {
          05   private transient int transientVar = 10;
          06   private String name;
          07   private double score;
          08   private Shape shape = new Square(5, 10);
          09   private String[] tags;
          10   private int id;
          11   
          12   public String getName() {
          13     return this.name;
          14   }
          15   
          16   @EqualsAndHashCode(callSuper=true)
          17   public static class Square extends Shape {
          18     private final int width, height;
          19     
          20     public Square(int width, int height) {
          21       this.width = width;
          22       this.height = height;
          23     }
          24   }
          25 }
          
          Show
          Uri Moszkowicz added a comment - Maybe I'm missing something but the documentation doesn't indicate that the includes and excludes can be specified: http://groovy.codehaus.org/gapi/groovy/transform/EqualsAndHashCode.html It would be useful to be able to specify which members are excluded as in the example: @EqualsAndHashCode(exclude={"id", "shape"}) 04 public class EqualsAndHashCodeExample { 05 private transient int transientVar = 10; 06 private String name; 07 private double score; 08 private Shape shape = new Square(5, 10); 09 private String[] tags; 10 private int id; 11 12 public String getName() { 13 return this.name; 14 } 15 16 @EqualsAndHashCode(callSuper=true) 17 public static class Square extends Shape { 18 private final int width, height; 19 20 public Square(int width, int height) { 21 this.width = width; 22 this.height = height; 23 } 24 } 25 }
          Hide
          Paul King added a comment -

          Actually, you have found a glitch in GroovyDoc (which would be good to have a separate issue for). If you look here you will see the available methods:

          http://groovy.codehaus.org/api/groovy/transform/EqualsAndHashCode.html
          

          You can try it out yourself:

          import groovy.transform.*
          @EqualsAndHashCode(excludes="z")
          @TupleConstructor
          public class Point2D {
            int x, y, z
          }
          
          println new Point2D(1, 1, 1).equals(new Point2D(1, 1, 2))
          println new Point2D(1, 1, 1).equals(new Point2D(2, 1, 1))
          

          Perhaps this issue should just change to include an example like above in the main class JavaDoc.

          Show
          Paul King added a comment - Actually, you have found a glitch in GroovyDoc (which would be good to have a separate issue for). If you look here you will see the available methods: http://groovy.codehaus.org/api/groovy/transform/EqualsAndHashCode.html You can try it out yourself: import groovy.transform.* @EqualsAndHashCode(excludes= "z" ) @TupleConstructor public class Point2D { int x, y, z } println new Point2D(1, 1, 1).equals( new Point2D(1, 1, 2)) println new Point2D(1, 1, 1).equals( new Point2D(2, 1, 1)) Perhaps this issue should just change to include an example like above in the main class JavaDoc.
          Hide
          Paul King added a comment -

          Perhaps it is also worth considering an array of String version rather than just a comma delimited String.

          Show
          Paul King added a comment - Perhaps it is also worth considering an array of String version rather than just a comma delimited String.
          Hide
          Uri Moszkowicz added a comment -

          Sounds good. Thanks!

          Show
          Uri Moszkowicz added a comment - Sounds good. Thanks!
          Hide
          Paul King added a comment -

          Split out the idea of an array of Strings into GROOVY-5431.

          Show
          Paul King added a comment - Split out the idea of an array of Strings into GROOVY-5431 .
          Hide
          Paul King added a comment -

          fixed, thanks for the suggestion

          Show
          Paul King added a comment - fixed, thanks for the suggestion

            People

            • Assignee:
              Paul King
              Reporter:
              Uri Moszkowicz
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: