java - What is the default for Optional data members in lombok using @Builder? -
suppose have class such :
@builder class anidentifier { @nonnull //to indicate required builder private string mandatoryidpart; private optional<string> optionalidpart1; private optional<string> optionalidpart2; }
when client creates object using builder generated lombok class , not set optional parts, set to? client have pass value wrapped in optional or value builder optional parts?
here's tried out :
public static void main(string[] args) { anidentifier id = anidentifier.builder().mandatoryidpart("added") .optionalidpart1(optional.of("abs")).build(); //line 1 id.optionalidpart2.ispresent(); //line2 }
firstly line2 generated npe optionalidpart2 set null not optional.empty()
secondly line1, setting optional value required putting string in optional, lombok doesn't take care of that.
so use constructor such annotation:
@builder public anidentifier(@nonnull string mandatoryidpart, string optionalidpart1, string optionalidpart2) { this.mandatoryidpart = mandatoryidpart; this.optionalidpart1 = optional.ofnullable(optionalidpart1); this.optionalidpart2 = optional.ofnullable(optionalidpart2); }
Comments
Post a Comment