aboutsummaryrefslogtreecommitdiff
path: root/website/templates/features/GetterLazy.html
blob: e921712c714c6bf835600451eb5fb6c132087405 (plain)
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
<#import "_features.html" as f>

<@f.scaffold title="@Getter(lazy=true)" logline="Laziness is a virtue!">
	<@f.history>
		<code>@Getter(lazy=true)</code> was introduced in Lombok v0.10.
	</@f.history>
	
	<@f.overview>
		<p>
			You can let lombok generate a getter which will calculate a value once, the first time this getter is called, and cache it from then on. This can be useful if calculating the value takes a lot of CPU, or the value takes a lot of memory. To use this feature, create a <code>private final</code> variable, initialize it with the expression that's expensive to run, and annotate your field with <code>@Getter(lazy=true)</code>. The field will be hidden from the rest of your code, and the expression will be evaluated no more than once, when the getter is first called. There are no magic marker values (i.e. even if the result of your expensive calculation is <code>null</code>, the result is cached) and your expensive calculation need not be thread-safe, as lombok takes care of locking.
		</p>
		<p>
			If the initialization expression is complex, or contains generics, we recommend moving the code to a private (if possible static) method, and call that instead.
		</p>
	</@f.overview>

	<@f.snippets name="GetterLazy" />

	<@f.confKeys>
		<dt>
			<code>lombok.getter.lazy.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
		</dt><dd>
			Lombok will flag any usage of <code>@Getter(lazy=true)</code> as a warning or error if configured.
		</dd>
	</@f.confKeys>

	<@f.smallPrint>
		<p>
			You should never refer to the field directly, always use the getter generated by lombok, because the type of the field will be mangled into an <code>AtomicReference</code>. Do not try to directly access this <code>AtomicReference</code>; if it points to itself, the value has been calculated, and it is <code>null</code>. If the reference points to <code>null</code>, then the value has not been calculated. This behaviour may change in future versions. Therefore, <em>always</em> use the generated getter to access your field!
		</p><p>
			Other Lombok annotations such as <code>@ToString</code> always call the getter even if you use <code>doNotUseGetters=true</code>.
		</p>
	</@f.smallPrint>
</@f.scaffold>