package edu.hm.hafner.util;

import java.util.function.Supplier;

import edu.umd.cs.findbugs.annotations.CheckForNull;

/**
 * Deferred element wrapper of a singleton instance. Creates a constant instance when it is accessed the first time.
 *
 * @param <T>
 *         type of the instance
 */
public final class Deferred<T> {
    private final Supplier<T> supplier;

    @SuppressWarnings("PMD.AvoidUsingVolatile")
    @CheckForNull
    private volatile T object;

    /**
     * Creates a new instance that creates a singleton when it is accessed the first time.
     *
     * @param supplier
     *         the supplier to create the instance
     */
    public Deferred(final Supplier<T> supplier) {
        this.supplier = supplier;
    }

    /**
     * Returns the instance. If not yet created the instance will be created.
     *
     * @return the instance
     */
    public T get() {
        if (object == null) {
            object = supplier.get();
        }
        if (object == null) {
            throw new IllegalStateException("No value set");
        }
        return object;
    }
}

@@ -892,34 +892,55 @@
 ) %7B%0A        
-if (object
+T tmp = object;%0A        if (tmp
  == null) %7B%0A
@@ -951,22 +951,19 @@
         
-object
+tmp
  = suppl
@@ -995,20 +995,16 @@
     
-if (
 object =
 = nu
@@ -1003,88 +1003,13 @@
 ct =
-= null) %7B%0A            throw new IllegalStateException(%22No value set%22);%0A        %7D
+ tmp;
 %0A   
@@ -1020,22 +1020,19 @@
  return 
-object
+tmp
 ;%0A    %7D%0A

Make spotbugs happy.