mjedynak / builder-generator-idea-plugin

MIT License
150 stars 74 forks source link

toBuilder() #72

Open i-zanis-simply opened 1 year ago

i-zanis-simply commented 1 year ago

Would it be possible to add a toBuilder() method?

mfunaro commented 6 months ago

I would also like to see something like this on the target class. I usually create two builder methods on my target class, a toBuilder(this) and a static builder() method. It makes it convenient to create builders quickly with context.

Example:

public class TargetClass {

  private double value;

  private Targetclass(double value){
    this.value = value;
  }

  public static InnerBuilder builder(){
    return new InnerBuilder();
  }

  public InnerBuilder toBuilder(){
    return new InnerBuilder(this);
  }

//----------------------
  public static final class InnerBuilder {

      private double value;

      private InnerBuilder() {
      }

      private InnerBuilder(TargetClass other) {
        this.value= other.val;ue;
      }

      public InnerBuilder value(double value) {
        this.value = value;
        return this;
      }

      public InnerBuilder build(){
         return new TargetClass(value);
      }
   }
}
kistlers commented 6 months ago

I just discovered this plugin (it's great, thanks!) and immediately I missed these two methods being generated ffor inner class builders. Would be even better if the builder would not generate the public staticaInner() {...}` method. Like the example from @mfunaro above that already did not include it.