mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-11 12:19:00 +00:00
refactor: improve description
Added constraints for using @Getter(lazy=true) in documentation.
This commit is contained in:
1 parent
04876622d9
commit
ee82cda229
1 file changed
+10
-1
@@ -7,7 +7,16 @@
|
||||
|
||||
<@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.
|
||||
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>.
|
||||
<p>
|
||||
The following constraints apply when using <code>@Getter(lazy=true)</code>:
|
||||
</p>
|
||||
<ul>
|
||||
<li>The field must be <code>private</code> and <code>final</code>.</li>
|
||||
<li>The field must have an initializer expression.</li>
|
||||
<li>The field must not be declared <code>transient</code>.</li>
|
||||
</ul>
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user