Class Destroyer

  • All Implemented Interfaces:
    Destroyable

    public class Destroyer
    extends java.lang.Object
    implements Destroyable
    Manages a collection of Destroyable objects and provides a single method to destroy them all.

    This class is thread-safe and ensures that the destruction process is initiated only once. It also provides convenience add methods to register AutoCloseable and ScheduledFuture objects, which will be wrapped as Destroyable instances.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      java.lang.String name  
    • Constructor Summary

      Constructors 
      Constructor Description
      Destroyer​(java.lang.String name)
      Constructs a new Destroyer with a given name.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(Destroyable destroyable)
      Adds a Destroyable item to the destruction queue.
      void add​(java.lang.AutoCloseable os)
      Adds an AutoCloseable to the destruction queue.
      void add​(java.util.concurrent.ScheduledFuture<?> os)
      Adds a ScheduledFuture to the destruction queue.
      AFuture destroy​(boolean force)
      Initiates the destruction process for all registered items.
      boolean isDestroyed()
      Checks if the destruction process has been initiated.
      void remove​(Destroyable destroyable)
      Removes a specific Destroyable instance from the queue.
      void remove​(java.lang.AutoCloseable os)
      Finds and removes the wrapper for the specified AutoCloseable.
      void remove​(java.util.concurrent.ScheduledFuture<?> os)
      Finds and removes the wrapper for the specified ScheduledFuture.
      java.lang.String toString()
      Returns the name of this destroyer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • name

        public final java.lang.String name
    • Constructor Detail

      • Destroyer

        public Destroyer​(java.lang.String name)
        Constructs a new Destroyer with a given name.
        Parameters:
        name - The debug name for this destroyer, used in logging.
    • Method Detail

      • isDestroyed

        public boolean isDestroyed()
        Checks if the destruction process has been initiated.
        Returns:
        true if destroy(boolean) has been called, false otherwise.
      • toString

        public java.lang.String toString()
        Returns the name of this destroyer.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The name.
      • add

        public void add​(Destroyable destroyable)
        Adds a Destroyable item to the destruction queue.
        Parameters:
        destroyable - The item to be destroyed later.
      • add

        public void add​(java.util.concurrent.ScheduledFuture<?> os)
        Adds a ScheduledFuture to the destruction queue. The future will be wrapped, and its cancel(force) method will be called during destruction.
        Parameters:
        os - The ScheduledFuture to add.
      • add

        public void add​(java.lang.AutoCloseable os)
        Adds an AutoCloseable to the destruction queue. The item will be wrapped, and its close() method will be called during destruction.
        Parameters:
        os - The AutoCloseable item to add.
      • remove

        public void remove​(Destroyable destroyable)
        Removes a specific Destroyable instance from the queue.

        Note: This only works for items added via add(Destroyable d). It will not remove items added via add(AutoCloseable os) or add(ScheduledFuture<?> os) as they are wrapped.

        Parameters:
        destroyable - The exact Destroyable instance to remove.
      • remove

        public void remove​(java.util.concurrent.ScheduledFuture<?> os)
        Finds and removes the wrapper for the specified ScheduledFuture. This method iterates the queue to find the Destroyer.ScheduledFutureDestroyable wrapper that contains the given future and removes it.
        Parameters:
        os - The ScheduledFuture to remove.
      • remove

        public void remove​(java.lang.AutoCloseable os)
        Finds and removes the wrapper for the specified AutoCloseable. This method iterates the queue to find the Destroyer.AutoCloseableDestroyable wrapper that contains the given item and removes it.
        Parameters:
        os - The AutoCloseable item to remove.
      • destroy

        public AFuture destroy​(boolean force)
        Initiates the destruction process for all registered items.

        This method is idempotent. If called multiple times, it will only execute the destruction logic once and return the same future. It iterates through all items in the queue, calls their destroy(force) method, and returns a future that completes when all items have finished destroying.

        Specified by:
        destroy in interface Destroyable
        Parameters:
        force - If true, forces the destruction (e.g., future.cancel(true)); otherwise, attempts a graceful shutdown.
        Returns:
        An AFuture that completes when all items are destroyed.