Skip to content
peichhorn edited this page Jun 30, 2012 · 1 revision

@LazyGetter

Overview

See lombok issue 160

Example

With Lombok

import lombok.LazyGetter;

public class LazyGetterExample {
  @LazyGetter
  private final double[] cached = expensive();

  private double[] expensive() {
    double[] result = new double[1000000];
    for (int i = 0; i < result.length; i++) {
      result[i] = Math.asin(i);
    }
    return result;
  }
}

Vanilla Java

public class LazyGetterExample {
  private volatile boolean $cachedInitialized;
  private final Object[] $cachedLock = new Object[0];
  private double[] cached;

  public double[] getCached() {
    if (!this.$cachedInitialized) {
      synchronized (this.$cachedLock) {
        if (!this.$cachedInitialized) {
          this.cached = expensive();
          this.$cachedInitialized = true;
        }
      }
      return this.cached;
    }
  }

  private double[] expensive() {
    double[] result = new double[1000000];
    for (int i = 0; i < result.length; i++) {
      result[i] = Math.asin(i);
    }
    return result;
  }
}

Behind the Scenes

(Documentation pending)

Configuration

Nothing to configure yet.