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

public class AutoCloser<T> extends Object implements 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.
  • Constructor Details

    • AutoCloser

      public AutoCloser(T obj, Runnable closer)
      Parameters:
      obj - The object to pass to the closer when close() is called
      closer - a function that will be called upon close.
    • AutoCloser

      public AutoCloser(Runnable init, Runnable closer)
      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 as init
    • AutoCloser

      public AutoCloser(T obj, Consumer<T> closer)
      Parameters:
      obj - An arbitrary object to be passed to the closer when close() is called
      closer - A function that will receive the obj at close.
  • Method Details

    • get

      public final T get()
      Returns:
      If constructed with AutoCloser(Object, Runnable) or AutoCloser(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 interface AutoCloseable