- java.lang.Object
-
- com.github.robtimus.filesystems.CleanerSupport
-
public final class CleanerSupport extends Object
A utility class forCleaner
s.Action for
Cleaner
s cannot declare any checked exception to be thrown. When usingCleaner
s with file systems, it's common that the action can throwIOException
s. The methods in this class allow such exceptions to be wrapped and unwrapped easily. The typical use is to callregister(Cleaner, Object, CleanAction)
to createCleaner.Cleanable
instances, andclean(Cleanable)
to invoke these instances. This allows anyIOException
thrown from the action provided toregister(Cleaner, Object, CleanAction)
to be re-thrown whenclean(Cleanable)
is called.- Author:
- Rob Spoor
- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CleanerSupport.CleanAction
An action to invoke when objects become phantom reachable.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
clean(Cleaner.Cleanable cleanable)
Invokes aCleaner.Cleanable
, unwrapping anyUncheckedIOException
thrown by it.static Cleaner.Cleanable
register(Cleaner cleaner, Object obj, CleanerSupport.CleanAction action)
Registers an object and a cleaning action in aCleaner
.
-
-
-
Method Detail
-
register
public static Cleaner.Cleanable register(Cleaner cleaner, Object obj, CleanerSupport.CleanAction action)
Registers an object and a cleaning action in aCleaner
. This method is a wrapper aroundCleaner.register(Object, Runnable)
that allows the action to throwIOException
s. If the action throws anIOException
when the returnedCleaner.Cleanable
is called, anUncheckedIOException
will be thrown. Useclean(Cleanable)
instead of directly callingCleaner.Cleanable.clean()
to unwrap that exception.- Parameters:
cleaner
- TheCleaner
to register the given object and action in.obj
- The object to monitor.action
- The action to invoke when the object becomes phantom reachable.- Returns:
- A
Cleaner.Cleanable
instance. - Throws:
NullPointerException
- If the givenCleaner
, object or action isnull
.
-
clean
public static void clean(Cleaner.Cleanable cleanable) throws IOException
Invokes aCleaner.Cleanable
, unwrapping anyUncheckedIOException
thrown by it. Any other unchecked exception will be relayed to the caller.- Parameters:
cleanable
- TheCleaner.Cleanable
to invoke.- Throws:
NullPointerException
- If the givenCleaner.Cleanable
isnull
.IOException
- If the givenCleaner.Cleanable
throws anUncheckedIOException
.
-
-