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

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


/**
 * Simple threadsafe lazy init object.
 * 
 * usage:
 * 
 * LazyInit<MyObject> obj = new LazyInit<MyObject>() {
 * 	@Override
 * 	public MyObject init() {
 * 		return new MyObject();
 * 	}
 * }
 * 
 * 
 * MyObject my = obj.get();
 * 
 * @author Dustin Norlander
 * @created Aug 31, 2011
 * 
 */
public abstract class LazyInitObject<T> {

	protected Log log = LogFactory.getLog(LazyInitObject.class);
	
	T object;
	LazyInit lock = new LazyInit();
	
	public abstract T init();
	
	public T get() {
		if (lock.start()) {
			try {
				this.object = this.init();
			} finally {
				lock.end();
			}
		}
		return object;
	}
	
	/**
	 * will reset this object to initialize again (on the next get() method call)
	 */
	public void reset() {
		lock.reset();
	}
}

@@ -42,16 +42,69 @@
 rrent;%0A%0A
+import java.util.concurrent.atomic.AtomicReference;%0A%0A
 import o
@@ -134,16 +134,16 @@
 ng.Log;%0A
-
 import o
@@ -589,17 +589,34 @@
 ss);%0A%09%0A%09
-T
+AtomicReference%3CT%3E
  object;
@@ -744,19 +744,21 @@
 s.object
- = 
+.set(
 this.ini
@@ -760,16 +760,17 @@
 s.init()
+)
 ;%0A%09%09%09%7D f
@@ -818,16 +818,22 @@
 n object
+.get()
 ;%0A%09%7D%0A%09%0A%09
@@ -943,16 +943,16 @@
 set() %7B%0A
-
 %09%09lock.r
@@ -962,10 +962,221 @@
 t();%0A%09%7D%0A
+%09%0A%09/**%0A%09 * atomically sets the reference and sets the init to not run.%0A%09 * @param object%0A%09 */%0A%09public void set(T object) %7B%0A%09%09lock.start();%0A%09%09try %7B%0A%09%09%09this.object.set(object);%0A%09%09%7D finally %7B%0A%09%09%09lock.end();%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Add set to lazy init object, to bypass init code if needed