title: Get-or-create pattern in Java 7?
body: <p>I am trying to write a generic functionality that does thread-safe optional lazy initialization on demand. I cannot use the standard pattern, as the value is not final and could have been set via a setter already.</p>
  
  <p>In Java 8 I solved that by writing a generic <code>LazyInitializer</code> with a supplier:</p>
  
  <pre><code>public class LazyInitializer&lt;T&gt; {
  
    protected final Supplier&lt;T&gt; initializer;
    protected AtomicReference&lt;T&gt; value = new AtomicReference&lt;&gt;();
  
    public LazyInitializer(final Supplier&lt;T&gt; initializer) {
      this.initializer = initializer;
    }
  
    public T get() {
      T result = this.value.get();
      if (result == null) {
        this.value.compareAndSet(null, this.initializer.get());
        result = this.value.get();
      }
      return result;
    }
  
    public void setValue(final T value) {
      this.value.set(value);
    }
  
  }
  </code></pre>
  
  <p>You would then use this class like this:</p>
  
  <pre><code>final LazyInitializer&lt;List&lt;String&gt;&gt; value = new LazyInitializer&lt;&gt;(ArrayList&lt;String&gt;::new);
  </code></pre>
  
  <p>This is thread-safe, can handle setters, has a very low overhead and especially: requires little boilerplate code. </p>
  
  <p>Now however I am forced to use Java 7 and I cannot seem to find an equally elegant solution, as Java 7 cannot use Suppliers and therefore requires to write a lot of ugly code. Also generics do not allow to instanciate them unless you supply the exact class, which is a problem if you use generic class values (like <code>ArrayList&lt;String&gt;</code>).</p>
  
  <p>As far as I can tell I am either forced to write ugly code or do reflection magic beyond my capabilites, or is there any way to write the <code>LazyInitializer</code> class in an elegant way in Java 7 that I am missing?</p>
  
  <p><strong>Edit:</strong> Using the Answer from Jorn Vernee I modified the class to work with Java 7 as follows:</p>
  
  <pre><code>public class LazyInitializer&lt;T&gt; {
  
    protected final Class&lt;?&gt; clazz;
    protected AtomicReference&lt;T&gt; value = new AtomicReference&lt;&gt;();
  
    public LazyInitializer(final Class&lt;?&gt; clazz) {
      this.clazz = clazz;
    }
  
    public T get() {
      T result = this.value.get();
      if (result == null) {
        this.value.compareAndSet(null, constructNew());
        result = this.value.get();
      }
      return result;
    }
  
    public void setValue(final T value) {
      this.value.set(value);
    }
  
    protected T constructNew() {
      try {
        return (T) clazz.newInstance();
      } catch (InstantiationException | IllegalAccessException ex) {
        throw new IllegalStateException(ex);
      }
    }
  }
  </code></pre>
  
  <p>Which can then be (once again) elegantly called like this:</p>
  
  <pre><code>final LazyInitializer&lt;List&lt;String&gt;&gt; value = new LazyInitializer&lt;&gt;(ArrayList.class);
  </code></pre>
  
  <p><strong>However</strong> this class can no longer verify if the supplied class actually matches (because Generics) and it only works with default constructors. But at least it solves my case.</p>