Package io.aether.utils
Class Destroyer
- java.lang.Object
-
- io.aether.utils.Destroyer
-
- All Implemented Interfaces:
Destroyable
public class Destroyer extends java.lang.Object implements Destroyable
Manages a collection ofDestroyableobjects 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
addmethods to registerAutoCloseableandScheduledFutureobjects, which will be wrapped asDestroyableinstances.
-
-
Field Summary
Fields Modifier and Type Field Description java.lang.Stringname
-
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 voidadd(Destroyable destroyable)Adds aDestroyableitem to the destruction queue.voidadd(java.lang.AutoCloseable os)Adds anAutoCloseableto the destruction queue.voidadd(java.util.concurrent.ScheduledFuture<?> os)Adds aScheduledFutureto the destruction queue.AFuturedestroy(boolean force)Initiates the destruction process for all registered items.booleanisDestroyed()Checks if the destruction process has been initiated.voidremove(Destroyable destroyable)Removes a specificDestroyableinstance from the queue.voidremove(java.lang.AutoCloseable os)Finds and removes the wrapper for the specifiedAutoCloseable.voidremove(java.util.concurrent.ScheduledFuture<?> os)Finds and removes the wrapper for the specifiedScheduledFuture.java.lang.StringtoString()Returns the name of this destroyer.
-
-
-
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:
toStringin classjava.lang.Object- Returns:
- The name.
-
add
public void add(Destroyable destroyable)
Adds aDestroyableitem to the destruction queue.- Parameters:
destroyable- The item to be destroyed later.
-
add
public void add(java.util.concurrent.ScheduledFuture<?> os)
Adds aScheduledFutureto the destruction queue. The future will be wrapped, and itscancel(force)method will be called during destruction.- Parameters:
os- TheScheduledFutureto add.
-
add
public void add(java.lang.AutoCloseable os)
Adds anAutoCloseableto the destruction queue. The item will be wrapped, and itsclose()method will be called during destruction.- Parameters:
os- TheAutoCloseableitem to add.
-
remove
public void remove(Destroyable destroyable)
Removes a specificDestroyableinstance from the queue.Note: This only works for items added via
add(Destroyable d). It will not remove items added viaadd(AutoCloseable os)oradd(ScheduledFuture<?> os)as they are wrapped.- Parameters:
destroyable- The exactDestroyableinstance to remove.
-
remove
public void remove(java.util.concurrent.ScheduledFuture<?> os)
Finds and removes the wrapper for the specifiedScheduledFuture. This method iterates the queue to find theDestroyer.ScheduledFutureDestroyablewrapper that contains the given future and removes it.- Parameters:
os- TheScheduledFutureto remove.
-
remove
public void remove(java.lang.AutoCloseable os)
Finds and removes the wrapper for the specifiedAutoCloseable. This method iterates the queue to find theDestroyer.AutoCloseableDestroyablewrapper that contains the given item and removes it.- Parameters:
os- TheAutoCloseableitem 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:
destroyin interfaceDestroyable- Parameters:
force- If true, forces the destruction (e.g.,future.cancel(true)); otherwise, attempts a graceful shutdown.- Returns:
- An
AFuturethat completes when all items are destroyed.
-
-