Annotation Type Memoized
-
@Documented @Retention(CLASS) @Target(METHOD) public @interface MemoizedAnnotates methods in@AutoValueclasses for which the generated subclass will memoize the returned value.Methods annotated with
@Memoizedcannot:- be
abstract(except forAnnotation.hashCode()andAnnotation.toString()),private,final, orstatic - return
void - have any parameters
If you want to memoize
Annotation.hashCode()orAnnotation.toString(), you can redeclare them, keeping themabstract, and annotate them with@Memoize.If a
@Memoizedmethod is annotated with an annotation whose simple name isNullable, thennullvalues will also be memoized. Otherwise, if the method returnsnull, the overriding method will throw aNullPointerException.The overriding method uses double-checked locking to ensure that the annotated method is called at most once.
Example
@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; } } - be