uk.ac.starlink.util

## Class Cleaner

- java.lang.Object
- - uk.ac.starlink.util.Cleaner

-     public class Cleaner
      extends java.lang.Object

  Manages a set of object references and corresponding cleaning actions. It is used to manage end-of-life actions for an object, in particular to replace use of finalizers, which at Java 21 are deprecated for removal (and which are generally frowned upon).

  This is a more-or-less drop-in replacement for the `java.lang.ref.Cleaner` class introduced in Java 9. If the target platform for this package gets upgraded to Java 9 or greater, this class can be retired in favour of the JRE implementation.

  The `getInstance()` method lazily constructs a singleton instance, which is probably suitable for most purposes. But you can construct your own if you want.

  Since:
    25 Jul 2024

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `Cleaner.Cleanable` Cleanable represents an object and a cleaning action registered in a Cleaner.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Cleaner()` Constructor.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static Cleaner` | `getInstance()` Returns a lazily constructed singleton instance of this class.
    `Cleaner.Cleanable` | `register(java.lang.Object obj, java.lang.Runnable action)` Registers an object and a cleaning action to run when the object becomes phantom reachable.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Cleaner

          public Cleaner()

      Constructor.

  - ### Method Detail

    - #### register

          public Cleaner.Cleanable register(java.lang.Object obj,
                                            java.lang.Runnable action)

      Registers an object and a cleaning action to run when the object becomes phantom reachable.
      Parameters:
        `obj` - the object to monitor
        `action` - a Runnable to invoke when the object becomes phantom reachable
      Returns:
        a Cleanable instance

    - #### getInstance

          public static Cleaner getInstance()

      Returns a lazily constructed singleton instance of this class.
      Returns:
        singleton instance