Top Description Fields Constructors Methods
org.newsclub.net.unix

pack-priv abstract Class CleanableState

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.newsclub.net.unix.AFCore
Annotations
@IgnoreJRERequirement
Imports
java.io.Closeable, .IOException, java.lang.ref.Cleaner, org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

This wrapper (along with the Java 8-specific counterpart in src/main/java8) allows us to implement cleanup logic for objects that are garbage-collectable/no longer reachable.

Usage:

  1. Create a subclass of CleanableState and attach it as a private field to the object you want to be cleaned up. You may call that field cleanableState.
  2. Define all resources that need to be cleaned up in this subclass (instead of the observed object itself).
  3. Make sure to not refer the observed object instance itself, as that will create a reference cycle and prevent proper cleanup.
  4. Implement the doClean() method to perform all the necessary cleanup steps.
  5. If the observed class implements close(), it's a good practice to have it just call cleanableState.runCleaner().

Implementation details:

Author
Christian Kohlschütter

Field Summary

Modifier and TypeField and Description
private final Cleaner.Cleanable
private static final Cleaner

Constructor Summary

AccessConstructor and Description
protected
CleanableState(Object
The observed instance (the outer class referencing this CleanableState).
observed
)

Creates a state object to be used as an implementation detail of the specified observed instance.

Method Summary

Modifier and TypeMethod and Description
public final void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

protected abstract void
doClean()

Performs the actual cleanup.

public final void
runCleaner()

Explicitly the cleanup code defined in doClean().

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cleanableback to summary
private final Cleaner.Cleanable cleanable
CLEANERback to summary
private static final Cleaner CLEANER

Constructor Detail

CleanableStateback to summary
protected CleanableState(Object observed)

Creates a state object to be used as an implementation detail of the specified observed instance.

Parameters
observed:Object

The observed instance (the outer class referencing this CleanableState).

Method Detail

closeback to summary
public final void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
doCleanback to summary
protected abstract void doClean()

Performs the actual cleanup.

runCleanerback to summary
public final void runCleaner()

Explicitly the cleanup code defined in doClean(). This is best be called from a close() method in the observed class.