mirror of
https://github.com/tiennm99/lombok.git
synced 2026-06-03 06:12:18 +00:00
66 lines
3.3 KiB
HTML
66 lines
3.3 KiB
HTML
<!DOCTYPE html>
|
|
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<link rel="stylesheet" type="text/css" href="../logi/reset.css" />
|
|
<link rel="stylesheet" type="text/css" href="features.css" />
|
|
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
|
|
<meta name="description" content="Spice up your java" />
|
|
<title>@Getter(lazy=true)</title>
|
|
</head><body><div id="pepper">
|
|
<div class="minimumHeight"></div>
|
|
<div class="meat">
|
|
<div class="header"><a href="../index.html">Project Lombok</a></div>
|
|
<h1>@Getter(lazy=true)</h1>
|
|
<div class="byline">Laziness is a virtue!</div>
|
|
<div class="overview">
|
|
<h3>Overview</h3>
|
|
<p>
|
|
<em>NEW IN Lombok 0.10: </em>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>
|
|
</div>
|
|
<div class="snippets">
|
|
<div class="pre">
|
|
<h3>With Lombok</h3>
|
|
<div class="snippet">@HTML_PRE@</div>
|
|
</div>
|
|
<div class="sep"></div>
|
|
<div class="post">
|
|
<h3>Vanilla Java</h3>
|
|
<div class="snippet">@HTML_POST@</div>
|
|
</div>
|
|
</div>
|
|
<div style="clear: left;"></div>
|
|
<div class="overview">
|
|
<h3>Small print</h3><div class="smallprint">
|
|
<p>
|
|
Lombok actually creates a few fields all prefixed with <code>$lombok$</code> to cache the value. You should not rely on the exact type, name, and structure
|
|
of these fields as future implementations may change them. To access the lazily initialized value, <em>always</em> use the generated getter.
|
|
</p><p>
|
|
Other Lombok annotations such as <code>@ToString</code> always call the getter even if you use <code>doNotUseGetters=true</code>.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="footer">
|
|
<a href="index.html">Back to features</a> | <a href="GetterSetter.html">Previous feature (@Getter / @Setter)</a> | <a href="ToString.html">Next feature (@ToString)</a><br />
|
|
<a href="../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright © 2009-2011 The Project Lombok Authors, licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</span>
|
|
</div>
|
|
<div style="clear: both;"></div>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
|
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
|
</script>
|
|
<script type="text/javascript">
|
|
try {
|
|
var pageTracker = _gat._getTracker("UA-9884254-1");
|
|
pageTracker._trackPageview();
|
|
} catch(err) {}
|
|
</script>
|
|
</body></html>
|