Top Description Methods
com.google.auto.value.extension.memoized

public @Interface Memoized

extends Annotation
Annotations
@Documented
@Retention:CLASS
@Target:METHOD
Static Imports
java.lang.annotation.ElementType.METHOD, .RetentionPolicy.CLASS

Annotates methods in @AutoValue classes for which the generated subclass will memoize the returned value.

Methods annotated with @Memoized cannot:

If you want to memoize hashCode() or toString(), you can redeclare them, keeping them abstract, and annotate them with @Memoized.

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.

Example

  @AutoValue
  abstract class Value {
    abstract String stringProperty();

    @Memoized
    String derivedProperty() {
      return someCalculationOn(stringProperty());
    }
  }

  @Generated
  class AutoValue_Value {
    // …

    private volatile String derivedProperty;

    Override
    String derivedProperty() {
      if (derivedProperty == null) {
        synchronized (this) {
          if (derivedProperty == null) {
            derivedProperty = super.derivedProperty();
          }
        }
      }
      return derivedProperty;
    }
  }

Method Summary

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString