diff options
Diffstat (limited to 'src-ambient/com/android/phone/common/ambient/SingletonHolder.java')
-rw-r--r-- | src-ambient/com/android/phone/common/ambient/SingletonHolder.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src-ambient/com/android/phone/common/ambient/SingletonHolder.java b/src-ambient/com/android/phone/common/ambient/SingletonHolder.java new file mode 100644 index 0000000..bca6023 --- /dev/null +++ b/src-ambient/com/android/phone/common/ambient/SingletonHolder.java @@ -0,0 +1,56 @@ +package com.android.phone.common.ambient; + +/** + * Encapsulates a threadsafe singleton pattern. + * + * This class is designed to be used as a public constant, living within a class that has a private constructor. + * It defines a {@link #create(I)} method that will only ever be called once, upon the first call of {@link #get(I)}. + * That method is responsible for creating the actual singleton instance, and that instance will be returned for all + * future calls of {@link #get(I)}. + * + * Example: + * <code> + * public class FooSingleton { + * public static final SingletonHolder<FooSingleton, ParamObject> HOLDER = + * new SingletonHolder<FooSingleton, ParamObject>() { + * @Override + * protected FooSingleton create(ParamObject param) { + * return new FooSingleton(param); + * } + * }; + * + * private FooSingleton(ParamObject param) { + * + * } + * } + * + * // somewhere else + * FooSingleton.HOLDER.get(params).doStuff(); + * </code> + * @param <E> The type of the class to hold as a singleton. + * @param <I> A parameter object to use during creation of the singleton object. + */ +public abstract class SingletonHolder<E, I> { + private E mInstance; + private final Object LOCK = new Object(); + + public final E get(I initializer) { + if (null == mInstance) { + synchronized (LOCK) { + if (null == mInstance) { + mInstance = create(initializer); + } + } + } + + return mInstance; + } + + public final boolean isCreated() { + synchronized (LOCK) { + return mInstance != null; + } + } + + protected abstract E create(I initializer); +} |