-
Notifications
You must be signed in to change notification settings - Fork 23
/
factory.html
281 lines (226 loc) · 21.4 KB
/
factory.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"-->
<link rel="stylesheet" href="/gfx/bootstrap.min.css">
<link rel="stylesheet" href="/gfx/main.css">
<link rel="stylesheet" href="/gfx/code.css">
<title>Factory builders</title>
</head>
<body class="page">
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-PMJSKV"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PMJSKV');</script>
<!-- End Google Tag Manager -->
<header>
<div class="container">
<a href="/">Immutables</a> ←
<h1>Factory builders <iframe src="https://ghbtns.com/github-btn.html?user=immutables&repo=immutables&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
</h1>
</div>
</header>
<aside id="toc"></aside>
<section class="documentation">
<h2 id="overview">Overview</h2>
<p>Do not repeat yourself writing builders for your APIs, with all that overhead of fluent structure and checking.
Generate builders for static factory methods as easily as you would generate builders for <a href="/immutable.html">immutable values</a>.
A factory builder can be seen as a form of partial function application - Supply named parameters one by one and evaluate the function at the end.</p>
<p>This handles the common case of constructing immutable objects. However, if extra processing is required during the construction
(whilst values are being provided to the builder), then factory builders may not be suitable.</p>
<ul>
<li>In addition to this guide, see the JavaDocs of <a href="https://github.com/immutables/immutables/blob/master/builder/src/org/immutables/builder/Builder.java">org.immutables.builder.Builder</a></li>
</ul>
<p>Compile dependencies:</p>
<ul>
<li><a href="https://search.maven.org/artifact/org.immutables/value/2.10.1/jar">org.immutables:value:2.10.1</a></li>
<li><a href="https://search.maven.org/artifact/org.immutables/builder/2.10.1/jar">org.immutables:builder:2.10.1</a></li>
</ul>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>value<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>builder<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
</code></pre></div></div>
<h3 id="factory">Factory</h3>
<p>Place a <code class="language-plaintext highlighter-rouge">@org.immutables.builder.Builder.Factory</code> annotation on a static factory method to generate a builder in the same package.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Builder</span><span class="o">.</span><span class="na">Factory</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nf">sum</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">,</span> <span class="kt">int</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="kt">int</span> <span class="n">sumOf1and2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SumBuilder</span><span class="o">()</span>
<span class="o">.</span><span class="na">a</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">b</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>By default (when not using a custom <a href="/style.html">style</a>), the name of the builder and its visibility modifier are derived from the annotated static factory methods.
The build method invokes static factory methods and returns a value.
As with builders generated for immutable objects, builders for factory methods take care of parameter null-checking, conversion to immutable collections, and verify that all attributes are set.</p>
<p>Use a <code class="language-plaintext highlighter-rouge">@Value.Style(newBuilder)</code> naming template to customize the name of a builder constuctor.
By default <code class="language-plaintext highlighter-rouge">newBuilder = "new"</code>, which will result in the generated factory builder having a plain <code class="language-plaintext highlighter-rouge">public</code> constructor.
It is sometimes desirable, depending on your organization’s code standards, to instead have a static factory method that produces builders.
Styles can be applied to classes or packages, but not to individual methods.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// put styles on enclosing class or package</span>
<span class="nd">@Value</span><span class="o">.</span><span class="na">Style</span><span class="o">(</span><span class="n">newBuilder</span> <span class="o">=</span> <span class="s">"newBuilder"</span><span class="o">)</span>
<span class="kd">class</span> <span class="nc">Factories</span> <span class="o">{</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Factory</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nf">sum</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">,</span> <span class="kt">int</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="kt">int</span> <span class="n">sumOf1and2</span> <span class="o">=</span> <span class="nc">SumBuilder</span><span class="o">.</span><span class="na">newBuilder</span><span class="o">()</span>
<span class="o">.</span><span class="na">a</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">b</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>Other features and styles work similarly for factory builders as for immutable objects:</p>
<ul>
<li><a href="/immutable.html##collection">Array, Collection and Map parameters</a></li>
<li><a href="/immutable.html#optional">Optional</a> and <a href="/immutable.html#nullable">Nullable</a> parameters</li>
<li><a href="/immutable.html#strict-builder">Strict builders</a></li>
<li>Various naming <a href="/style.html">style customizations</a></li>
</ul>
<p>Note that <code class="language-plaintext highlighter-rouge">@Value.Default</code>, <code class="language-plaintext highlighter-rouge">@Value.Lazy</code>, <code class="language-plaintext highlighter-rouge">@Value.Derived</code>, etc, annotations are <strong>not applicable</strong> to the parameters of factory methods.
Moreover, they usually don’t make sense for factory builders in the way that they do for immutable value objects.</p>
<p>To have default parameters, use <code class="language-plaintext highlighter-rouge">Optional</code> parameters:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Builder</span><span class="o">.</span><span class="na">Factory</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nf">sum</span><span class="o">(</span><span class="nc">Optional</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="n">a</span><span class="o">,</span> <span class="nc">Optional</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">a</span><span class="o">.</span><span class="na">or</span><span class="o">(</span><span class="mi">0</span><span class="o">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">.</span><span class="na">or</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div></div>
<h3 id="pojo-constructors">POJO Constructors</h3>
<p>You can generate builders for POJO constructors in the same way as for the factory methods: put <code class="language-plaintext highlighter-rouge">@Builder.Constructor</code> annotation on a public constructor. <em>Note: it’s only supported for top-level types.</em></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Sum</span> <span class="o">{</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">result</span><span class="o">;</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Constructor</span>
<span class="nc">Sum</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">,</span> <span class="kt">int</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">result</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">...</span> <span class="c1">// use generated builder</span>
<span class="nc">Sum</span> <span class="n">sum</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SumBuilder</span><span class="o">()</span>
<span class="o">.</span><span class="na">a</span><span class="o">(</span><span class="mi">111</span><span class="o">)</span>
<span class="o">.</span><span class="na">b</span><span class="o">(</span><span class="mi">222</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>You can even generate builders for preexisting POJOs from different packages using <code class="language-plaintext highlighter-rouge">Include</code> annotation. Discovery routine is not very smart, so it will use the first public constructor found.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// use package-info.java for packages</span>
<span class="c1">// or place Include on a class</span>
<span class="c1">// within package</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Include</span><span class="o">({</span><span class="n">other</span><span class="o">.</span><span class="na">pack</span><span class="o">.</span><span class="na">PojoOne</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">other</span><span class="o">.</span><span class="na">pack</span><span class="o">.</span><span class="na">PojoAnother</span><span class="o">.</span><span class="na">class</span><span class="o">})</span>
<span class="kn">package</span> <span class="nn">my.pack</span><span class="o">;</span>
</code></pre></div></div>
<p>Style annotations put on a class or a package can be used to customize generated builders.</p>
<h3 id="parameters">Parameters</h3>
<p>Sometimes, it might be preferable to have some factory parameters turned into parameters of the generated builder’s constructor.
Annotate parameters with <code class="language-plaintext highlighter-rouge">@Builder.Parameter</code> to achieve this. Generated builders will then accept the annotated parameters via the
generated constructor instead of as parameter methods:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Builder</span><span class="o">.</span><span class="na">Factory</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Pet</span> <span class="nf">pet</span><span class="o">(</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Parameter</span> <span class="nc">AnimalKind</span> <span class="n">kind</span><span class="o">,</span>
<span class="nc">String</span> <span class="n">name</span><span class="o">,</span>
<span class="kt">int</span> <span class="n">age</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">Pet</span> <span class="n">pet</span> <span class="o">=</span> <span class="o">...</span>
<span class="k">return</span> <span class="n">pet</span><span class="o">;</span>
<span class="o">}</span>
<span class="nc">Pet</span> <span class="n">p</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">PetBuilder</span><span class="o">(</span><span class="nc">AnimalKind</span><span class="o">.</span><span class="na">DOG</span><span class="o">)</span> <span class="c1">// Parameter</span>
<span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"Fluffy"</span><span class="o">)</span>
<span class="o">.</span><span class="na">age</span><span class="o">(</span><span class="mi">3</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>This is invaluable for statically guaranteeing that required parameters are set.</p>
<h3 id="switch-parameters">Switch parameters</h3>
<p>Switch parameters are a way to provide some syntactic sugar for builders.
Let’s say you can create objects with different modes, and want the builder to have expressive methods to switch modes.
Modes could be simple as a <code class="language-plaintext highlighter-rouge">boolean</code> <code class="language-plaintext highlighter-rouge">ON|OFF</code>, or could be an enumeration such as <code class="language-plaintext highlighter-rouge">LIQUID|SOLID|GAS|PLASMA</code>.</p>
<p>Use <code class="language-plaintext highlighter-rouge">@Builder.Switch</code> on parameters of <code class="language-plaintext highlighter-rouge">enum</code> types to turn regular initialization methods into switch methods:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">enum</span> <span class="nc">Color</span> <span class="o">{</span><span class="no">RED</span><span class="o">,</span> <span class="no">YELLOW</span><span class="o">,</span> <span class="no">GREEN</span><span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">TrafficLight</span> <span class="nf">trafficLight</span><span class="o">(</span><span class="nd">@Builder</span><span class="o">.</span><span class="na">Switch</span> <span class="nc">Color</span> <span class="n">light</span><span class="o">,</span> <span class="kt">boolean</span> <span class="n">blink</span><span class="o">)</span> <span class="o">{</span>
<span class="o">...</span>
<span class="o">}</span>
<span class="k">new</span> <span class="nf">TrafficLightBuilder</span><span class="o">()</span>
<span class="o">.</span><span class="na">redLight</span><span class="o">()</span> <span class="c1">// a switch method</span>
<span class="o">.</span><span class="na">blink</span><span class="o">(</span><span class="kc">false</span><span class="o">)</span> <span class="c1">// a regular initializer</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>In the above example <code class="language-plaintext highlighter-rouge">.redLight()</code>, <code class="language-plaintext highlighter-rouge">.yellowLight()</code> or <code class="language-plaintext highlighter-rouge">.greenLight()</code> should be invoked to set the color switch parameter.
Names of switch methods are derived from the combined names of the enum constants and corresponding parameter names.</p>
<p>The builder will check that any corresponding light switch method has been invoked at least once (only once for <a href="/immutable.html#strict-builder">strict builders</a>).
To make one of the switch states the default, use the <code class="language-plaintext highlighter-rouge">enum</code> constant in the annotation’s <code class="language-plaintext highlighter-rouge">defaultName</code> attribute:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">enum</span> <span class="nc">Color</span> <span class="o">{</span><span class="no">RED</span><span class="o">,</span> <span class="no">YELLOW</span><span class="o">,</span> <span class="no">GREEN</span><span class="o">}</span>
<span class="kd">enum</span> <span class="nc">Blink</span> <span class="o">{</span><span class="no">NONE</span><span class="o">,</span> <span class="no">START</span><span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">TrafficLight</span> <span class="nf">trafficLight</span><span class="o">(</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Switch</span> <span class="nc">Color</span> <span class="n">light</span><span class="o">,</span>
<span class="nd">@Builder</span><span class="o">.</span><span class="na">Switch</span><span class="o">(</span><span class="n">defaultName</span> <span class="o">=</span> <span class="s">"NONE"</span><span class="o">)</span> <span class="nc">Blink</span> <span class="n">blinking</span><span class="o">)</span> <span class="o">{</span>
<span class="o">...</span>
<span class="o">}</span>
<span class="k">new</span> <span class="nf">TrafficLightBuilder</span><span class="o">()</span>
<span class="o">.</span><span class="na">yellowLight</span><span class="o">()</span>
<span class="o">.</span><span class="na">startBlinking</span><span class="o">()</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div></div>
<p>If the call to <code class="language-plaintext highlighter-rouge">.startBlinking()</code> is omitted, the default value <code class="language-plaintext highlighter-rouge">Blink.NONE</code> will be passed to factory.</p>
</section>
<footer class="jumbotron">
<div class="container">
<h2 id="guides">Guides</h2>
<ul>
<li><a href="/getstarted.html">Get started!</a></li>
<li><a href="/intro.html">Inception</a></li>
<li><a href="/immutable.html">Immutable objects</a></li>
<li><a href="/factory.html">Factory builders</a></li>
<li><a href="/functional.html">Functions and Predicates (for Java 7)</a></li>
<li><a href="/style.html">Style customization</a></li>
<li><a href="/json.html">JSON serialization</a></li>
<li><a href="/criteria.html">Criteria</a></li>
<li><a href="/mongo.html">MongoDB repositories</a></li>
<li><a href="/dynamodb.html">DynamoDB integration</a></li>
<li><a href="/encoding.html">Encoding: Customizing attributes and builders (experimental)</a></li>
<li><a href="/apt.html">Using annotation processor in IDE</a></li>
</ul>
<h2 id="get-involved">Get involved</h2>
<ul>
<li>Clone source repository, contribute bug reports and fixes on <a href="https://github.com/immutables/immutables">GitHub immutables/immutables</a></li>
<li>Issue reports, questions and feedback is welcome on issue tracker <a href="https://github.com/immutables/immutables/issues">GitHub immutables/immutables/issues</a></li>
<li>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li>
</ul>
<p><a href="/license.html">Apache License 2.0</a></p>
<!--<div><h2>Posts</h2>
<ul>
</ul>
</div>-->
</div>
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script defer src="/gfx/jquery.toc.min.js"></script>
<script>
$(function() {
$('#toc').toc({
container: '.documentation',
selectors: 'h1,h2,h3,h4',
anchorName: function(i, heading, prefix) {
heading = $(heading).text();
if (heading.trim) heading = heading.trim();
return heading.toLowerCase().replace(/ /g, '-').replace(/[^a-z^\-]+/g, '');
},
})
})
</script>
</body>
</html>