Package net.uptheinter.interceptify.util
Class AutoCloser<T>
java.lang.Object
net.uptheinter.interceptify.util.AutoCloser<T>
- Type Parameters:
T
- The type to be held by this AutoCloser instance
- All Implemented Interfaces:
AutoCloseable
This is a generic type for facilitating a try-with-resources
on any operation where something should be done at the end of
the scope.
What that happens to be can either just be a pair of function calls or an actual object; it depends on the constructor you use.
What that happens to be can either just be a pair of function calls or an actual object; it depends on the constructor you use.
-
Constructor Summary
ConstructorsConstructorDescriptionAutoCloser
(Runnable init, Runnable closer) AutoCloser
(T obj, Runnable closer) AutoCloser
(T obj, Consumer<T> closer) -
Method Summary
-
Constructor Details
-
AutoCloser
- Parameters:
obj
- The object to pass to the closer whenclose()
is calledcloser
- a function that will be called upon close.
-
AutoCloser
- Parameters:
init
- A function to be called immediately. Mainly for convenience so that you can pass method references to whatever does initialisation.closer
- A function to be called at close, convenient in the same way asinit
-
AutoCloser
- Parameters:
obj
- An arbitrary object to be passed to thecloser
whenclose()
is calledcloser
- A function that will receive theobj
at close.
-
-
Method Details
-
get
- Returns:
- If constructed with
AutoCloser(Object, Runnable)
orAutoCloser(Object, Consumer)
, the object. otherwise,null
.
-
close
public final void close()Invokes the closer. This is intended to be called by the JVM through try-with-resources. If you're calling it yourself, you probably shouldn't be using this class at all.- Specified by:
close
in interfaceAutoCloseable
-