@Documented
@Retention(value=SOURCE)
@Target(value=METHOD)
public @interface Memoized
@AutoValue classes for which the
generated subclass will memoize the
returned value.
Methods annotated with @Memoized cannot:
abstract (except for Annotation.hashCode() and Annotation.toString()), private, final, or static
void
If you want to memoize Annotation.hashCode() or Annotation.toString(), you can redeclare them,
keeping them abstract, and annotate them with @Memoize.
If a @Memoized method is annotated with an annotation whose simple name is Nullable, then null values will also be memoized. Otherwise, if the method returns
null, the overriding method will throw a NullPointerException.
The overriding method uses double-checked locking to ensure that the annotated method is called at most once.
@AutoValueabstract class Value { abstract String stringProperty();@MemoizedString derivedProperty() { return someCalculationOn(stringProperty()); } }@Generatedclass AutoValue_Value { // … private volatile String derivedProperty;OverrideString derivedProperty() { if (derivedProperty == null) { synchronized (this) { if (derivedProperty == null) { derivedProperty = super.derivedProperty(); } } } return derivedProperty; } }
Copyright © 2019 Google LLC. All Rights Reserved.