Class LazyFactory<T>
java.lang.Object
io.github.cowwoc.pouch10.core.LazyFactory<T>
- Type Parameters:
T- the type of the value
- All Implemented Interfaces:
Factory<T>,Reference<T>,Closeable,AutoCloseable
A factory that initializes a value on demand.
Instances of LazyFactory are not safe for use by multiple threads. If such synchronization is
required then it is recommended that ConcurrentLazyFactory be used.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Disposes the Factory and the value.static <T extends AutoCloseable>
LazyFactory<T> Creates a newLazyFactorythat disposes its value by invokingclose().static <T> LazyFactory<T> Creates a newLazyFactory.protected abstract TCreates the value.protected abstract voiddisposeValue(T value) Disposes the value.final TgetValue()Returns the value.booleanIndicates if the value was initialized.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.github.cowwoc.pouch10.core.Reference
isInitialized
-
Constructor Details
-
LazyFactory
protected LazyFactory()Creates a new instance.
-
-
Method Details
-
create
Creates a newLazyFactory.- Type Parameters:
T- the type of value returned by the factory- Parameters:
supplier- supplies the factory valuedisposer- implementsdisposeValue(T)- Returns:
- a new
LazyFactory
-
create
Creates a newLazyFactorythat disposes its value by invokingclose(). Ifclose()throws a checked exception, it is wrapped in aRuntimeExceptionor an exception that extends it.- Type Parameters:
T- the type of value returned by the factory- Parameters:
supplier- supplies the factory value- Returns:
- a new
LazyFactory
-
disposeValue
-
getValue
Returns the value. Subsequent invocations of this method return the same value.- Specified by:
getValuein interfaceReference<T>- Returns:
- an object of type
<T> - Throws:
IllegalStateException- if the factory is closed
-
close
public final void close()Description copied from interface:FactoryDisposes the Factory and the value. Subsequent invocations of this method have no effect. Invoking any other method after this one results inIllegalStateExceptionbeing thrown. -
toString
-
createValue
Creates the value. This method is invoked the first timeReference.getValue()is invoked.- Returns:
- the value
-
isInitialized
public boolean isInitialized()Description copied from interface:ReferenceIndicates if the value was initialized.A value may get initialized immediately after this method returns
falsebut once it returnstrueit will continue to do so indefinitely.- Specified by:
isInitializedin interfaceReference<T>- Returns:
trueif the value was initialized
-