/**
 * 
 */
package com.trendrr.oss.concurrent;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 
 * Class to allow lazy initializations, fully threadsafe without any synchronization.
 * 
 * 
 * usage: 
 * 
 * if (lazyInit.start()) {
 * 	try {
 * 		//initialization code here.
 *  } finally {
 *  	lazyInit.end();
 *  }
 * }
 * 
 * 
 * 
 * 
 * @author Dustin Norlander
 * @created Jan 31, 2011
 * 
 */
public class LazyInit {

	protected static Log log = LogFactory.getLog(LazyInit.class);
	
	protected AtomicBoolean once = new AtomicBoolean(false);
	
	protected ReentrantLock lock = new ReentrantLock();

	/**
	 * will return true on the first time it is called, false on every subsequent call.  
	 * it will block only if another thread is running the initialization code. 
	 * 
	 * 
	 * @return
	 */
	public boolean start() {
		if (!once.get()) {
			//now lock..
			lock.lock();
			//check if a previous thread already initialized
			if (once.get()) {
				lock.unlock();
			}
		}
		return !once.get();
	}

	public void end() {
		once.set(true);
		lock.unlock();
	}

	public void reset() {
		once.set(false);
	}
}

@@ -1253,10 +1253,285 @@
 se);%0A%09%7D%0A
+%09%0A%09/**%0A%09 * passively checks if this lock has been initialized.%0A%09 * Will return true if the lock is currently held.%0A%09 * %0A%09 * @return%0A%09 */%0A%09public boolean isInited() %7B%0A%09%09if (once.get()) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%09%09if (lock.isLocked()) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%09%09return once.get();%0A%09%7D%0A
 %7D%0A

add isInited check to see if lock has already been used