From 5a3e9bd8049469169410107011ad0e26b3b629e3 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 31 May 2013 01:03:38 +0200 Subject: Added @NonNull on parameters feature (issue 514), including docs and changelog. --- website/features/GetterLazy.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'website/features/GetterLazy.html') diff --git a/website/features/GetterLazy.html b/website/features/GetterLazy.html index bc5ecb0c..f70c8e78 100644 --- a/website/features/GetterLazy.html +++ b/website/features/GetterLazy.html @@ -46,7 +46,7 @@
-- cgit From 91bfd390e05ed3d04dff6438ffd1bc9e01eb1fff Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 7 Jul 2013 21:49:23 +0200 Subject: updated docs for new desugaring for getter(lazy=true) --- doc/changelog.markdown | 1 + usage_examples/GetterLazyExample_post.jpage | 19 ++++++++++--------- website/features/GetterLazy.html | 10 ++-------- 3 files changed, 13 insertions(+), 17 deletions(-) (limited to 'website/features/GetterLazy.html') diff --git a/doc/changelog.markdown b/doc/changelog.markdown index e922f8c9..c9dafc61 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,6 +2,7 @@ Lombok Changelog ---------------- ### v0.11.9 (Edgy Guinea Pig) +* CHANGE: The desugaring of @Getter(lazy=true) is now less object creation intensive. Documentation has been updated to reflect what the new desugaring looks like. [@Getter(lazy=true) documentation](http://projectlombok.org/features/GetterLazy.html). * PROMOTION: `@Value` has been promoted from experimental to the main package with no changes. The 'old' experimental one is still around but is deprecated, and is an alias for the new main package one. [@Value documentation](http://projectlombok.org/features/Value.html). * FEATURE: {Experimental} `@Builder` support. One of our earliest feature request issues, [Issue #16](https://code.google.com/p/projectlombok/issues/detail?id=16), has finally been addressed. [@Builder documentation](http://projectlombok.org/features/experimental/Builder.html). * FEATURE: `@NonNull` on a method or constructor parameter now generates a null-check statement at the start of your method. This nullcheck will throw a `NullPointerException` with the name of the parameter as the message. [Issue #514](https://code.google.com/p/projectlombok/issues/detail?id=514) diff --git a/usage_examples/GetterLazyExample_post.jpage b/usage_examples/GetterLazyExample_post.jpage index 9f4b1ba3..afed1748 100644 --- a/usage_examples/GetterLazyExample_post.jpage +++ b/usage_examples/GetterLazyExample_post.jpage @@ -1,18 +1,19 @@ public class GetterLazyExample { - private double[] $lombok$lazy1v; - private volatile boolean $lombok$lazy1i; - private final Object $lombok$lazyLock = new Object[0]; + private final java.util.concurrent.AtomicReference cached = new java.util.concurrent.AtomicReference(); public double[] getCached() { - if (!this.$lombok$lazy1i) { - synchronized (this.$lombok$lazyLock) { - if (!this.$lombok$lazy1i) { - this.$lombok$lazy1v = expensive(); - this.$lombok$lazy1i = true; + java.lang.Object value = this.cached.get(); + if (value == null) { + synchronized(value) { + value = this.cached.get(); + if (value == null) { + final double[] actualValue = expensive(); + value = actualValue == null ? this.cached : actualValue; + this.cached.set(value); } } } - return this.$lombok$lazy1v; + return (double[])(value == this.cached ? null : value); } private double[] expensive() { diff --git a/website/features/GetterLazy.html b/website/features/GetterLazy.html index f70c8e78..c6d21f01 100644 --- a/website/features/GetterLazy.html +++ b/website/features/GetterLazy.html @@ -15,12 +15,7 @@

Overview

- NEW IN Lombok 0.10: 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 private final variable, - initialize it with the expression that's expensive to run, and annotate your field with @Getter(lazy=true). 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 null, the result is cached) and your expensive calculation need not be thread-safe, as lombok - takes care of locking. + NEW IN Lombok 0.10: 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 private final variable, initialize it with the expression that's expensive to run, and annotate your field with @Getter(lazy=true). 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 null, the result is cached) and your expensive calculation need not be thread-safe, as lombok takes care of locking.

@@ -38,8 +33,7 @@

Small print

- Lombok actually creates a few fields all prefixed with $lombok$ 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, always use the generated getter. + 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 AtomicReference. Do not try to directly access this AtomicReference; if it points to itself, the value has been calculated, and it is null. If the reference points to null, then the value has not been calculated. This behaviour may change in future versions. Therefore, always use the generated getter to access your field!

Other Lombok annotations such as @ToString always call the getter even if you use doNotUseGetters=true.

-- cgit