Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1551

EqualsBuilder with Lambdas

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 3.9
    • None
    • lang.builder.*
    • None

    Description

      Has it been considered to use lambdas to make it easier to write equals methods? 

      public boolean equals(final Object obj) {
        return new EqualsBuilder<MyClass>(this, obj)
          .append(it -> it.field)
          .appendSuper(super.equals(obj)
          .isEquals();
      }

      I know this is incompatible with the current EqualsBuilder so maybe a new class, or something like this would work instead:

      public boolean equals(final Object obj) {
       return EqualsBuilder.with(this, obj) // with returns a generic type similar to EqualsBuilder
         .append(it -> it.field)
         .appendSuper(super.equals(obj)
         .isEquals();
      }

      It helps eliminate all the boilerplate code of checking object types and could even skip calling the lambdas if it already knew it was not equal. 

      Attachments

        Activity

          People

            Unassigned Unassigned
            bruce.brouwer Bruce Brouwer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: