robert trudel
robert trudel

Reputation: 5749

Create a new With method with multiple value

I create a record like this (there are more field...)

@Builder
@With
public record DepotRecord(Long id, String x, String, y, String z, String a)
}

A part of the generated code will look

public DepotRecord withId(final Long id) {
    return this.id == id ? this : new DepotRecord(id, this.x, this.y,  this.z, this.a);
}

In rare case, i need to change more then one field.

Maybe there is a way to have something generated who look like

public DepotRecord withIdXY(final Long id, String x, String y) {
    return this.id == id ? this : new DepotRecord(id, x, y, this.a);
}

Otherwise there a way to create a custom method?

Upvotes: 0

Views: 37

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70267

Assuming the fields are independent of one another, you already have it.

myDepotRecord.withId(id).withX(x).withY(y)

and you can wrap that in a method if you like

public DepotRecord withIdXY(final Long id, String x, String y) {
  return myDepotRecord.withId(id).withX(x).withY(y);
}

In general, it's going to be better to give your API users the tools to compose together into whatever they want, rather than trying to anticipate every possible use case and provide a one-liner that does exactly that. What can you build more with? A big box of legos, or a pre-made lego house superglued together?

Upvotes: 1

Related Questions