/*
 * The MIT License
 *
 * (c) 2015 Mike Chaberski.
 *
 * See LICENSE in base directory for distribution terms.
 *
 */
package com.novetta.ibg.common.dbhelp;

import java.sql.SQLException;

/**
 * Interface for a supplier that may throw an SQL exception when it
 * supplies an instance of a class. 
 * @author mchaberski
 * @param <T> the type of object to be supplied
 */
public interface SqlSupplier<T> {

    /**
     * Retrieves an instance of the appropriate type. The returned object may or
     * may not be a new instance, depending on the implementation.
     *
     * @return an instance of the appropriate type
     * @throws java.sql.SQLException if getting the instance fails
     */
    T get() throws SQLException;

    /**
     * Supplier that memoizes its delegate. 
     * This is copied from Guava: see Suppliers.memoize method.
     * @param <T>  the type of object to be memo-supplied
     * @author the Guava team
     */
    public static class MemoizingSqlSupplier<T> implements SqlSupplier<T> {

        final SqlSupplier<T> delegate;
        transient volatile boolean initialized;
        // "value" does not need to be volatile; visibility piggy-backs
        // on volatile read of "initialized".
        transient T value;

        MemoizingSqlSupplier(SqlSupplier<T> delegate) {
            this.delegate = delegate;
        }

        @Override
        public T get() throws SQLException {
            // A 2-field variant of Double Checked Locking.
            if (!initialized) {
                synchronized (this) {
                    if (!initialized) {
                        T t = delegate.get();
                        value = t;
                        initialized = true;
                        return t;
                    }
                }
            }
            return value;
        }

        @Override
        public String toString() {
            return "MemoizedSqlSupplier{" + delegate + "}";
        }

        private static final long serialVersionUID = 0;
    }
}

@@ -188,32 +188,82 @@
 n;%0A%0A
-/**%0A * Interface for a s
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A/**%0A * S
 uppl
@@ -275,17 +275,16 @@
 hat 
-may throw
+declares
  an 
@@ -301,52 +301,29 @@
 ion 
-when it%0A * supplies an instance of a class. 
+on the supply method.
 %0A * 
@@ -1282,16 +1282,23 @@
         
+public 
 Memoizin
@@ -1365,24 +1365,50 @@
 egate = 
-delegate
+checkNotNull(delegate, %22delegate%22)
 ;%0A      

make SqlSupplier constructor public