Package com.exonum.binding.core.proxy
Class Cleaner
- java.lang.Object
-
- com.exonum.binding.core.proxy.Cleaner
-
- All Implemented Interfaces:
AutoCloseable
public final class Cleaner extends Object implements AutoCloseable
A context controlling lifecycle of native proxies. When a proxy of a native object is created, it must register a cleaner of the native object in a context. The context performs the cleaning actions in a reversed order of their registration when it is closed. Once closed, the context must no longer be used to register new clean actions.The context might have a description of its origin so that it can be identified for a particular context.
All method arguments are non-null by default.
This class is not thread-safe.
-
-
Method Summary
Modifier and Type Method Description void
add(CleanAction<?> cleanAction)
Registers a new clean action with this context.void
close()
Performs all the clean operations that has been registered in this context in a reversed order of the registration order.String
getDescription()
Returns a description of this cleaner.int
getNumRegisteredActions()
Returns the number of the registered clean actions.boolean
isClosed()
Returns true if this cleaner is closed.String
toString()
Returns a string representation of this object, including its hash code so that this instance can be easily identified in the logs.
-
-
-
Constructor Detail
-
Cleaner
public Cleaner()
Creates a new cleaner with no (an empty) description.
-
Cleaner
public Cleaner(String description)
Creates a new cleaner.- Parameters:
description
- a description of this context, which describes its origin and is included intoString()
-
-
Method Detail
-
isClosed
public boolean isClosed()
Returns true if this cleaner is closed.
-
add
public void add(CleanAction<?> cleanAction)
Registers a new clean action with this context. If the context is already closed, the clean action will be executed immediately.- Parameters:
cleanAction
- a clean action to register; must not be null- Throws:
IllegalStateException
- if it’s attempted to add a clean action to a closed context
-
close
public void close() throws CloseFailuresException
Performs all the clean operations that has been registered in this context in a reversed order of the registration order.If any clean operation throws an exception in its
CleanAction.clean()
, the context logs the exception and attempts to perform the remaining operations.The implementation is idempotent — subsequent invocations have no effect.
- Specified by:
close
in interfaceAutoCloseable
- Throws:
CloseFailuresException
- if any clean action failed. The exception includes all thrown exceptions as suppressed
-
getDescription
public String getDescription()
Returns a description of this cleaner. May be empty.
-
getNumRegisteredActions
public int getNumRegisteredActions()
Returns the number of the registered clean actions.
-
-