-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjavaBuilder.mustache
82 lines (72 loc) · 2.55 KB
/
javaBuilder.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public static class Builder {{#parentModel}}extends {{classname}}.Builder {{/parentModel}}{
private {{classname}} instance;
public Builder() {
this(new {{classname}}());
}
protected Builder({{classname}} instance) {
{{#parentModel}}
super(instance);
{{/parentModel}}
this.instance = instance;
}
{{#vars}}
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = JsonNullable.<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}>of({{name}});
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{^vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = {{name}};
{{/vendorExtensions.x-is-jackson-optional-nullable}}
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{/vars}}
{{#parentVars}}
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) { // inherited: {{isInherited}}
super.{{name}}({{name}});
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{/parentVars}}
/**
* returns a built {{classname}} instance.
*
* The builder is not reusable.
*/
public {{classname}} build() {
try {
return this.instance;
} finally {
// ensure that this.instance is not reused{{#parentModel}}
super.build();{{/parentModel}}
this.instance = null;
}
}
@Override
public String toString() {
return getClass() + "=(" + instance + ")";
}
}
/**
* Create a builder with no initialized field.
*/
public static {{classname}}.Builder builder() {
return new {{classname}}.Builder();
}
/**
* Create a builder with a shallow copy of this instance.
*/
public {{classname}}.Builder toBuilder() {
return new {{classname}}.Builder(){{#allVars}}
.{{name}}({{getter}}()){{/allVars}};
}