Hi Rienars,
Sorry, another stumble.
While looking at SwtUtilities I wondered why there is no
dispose()/isDisposed() helper for IDisposables.
Maybe because SwtUtilities (ui.swt) is "too far away"
from IDisposable (ui.core). Ok!
But I would like to have something similar. Introducing a
new helper just for dispose in ui.core? Maybe!?
But, how about this:
public interface IDisposable {
/**
* Disposes this object, i.e.
free any resources.
*/
void dispose();
/**
* Check whether this object has
already disposed or not.
*
* @return disposed or
not
*/
boolean isDisposed();
/**
* Provides a helper on {@code
IDisposable}s.
*/
public static final class Util {
private Util() {
// utility
}
/**
* Disposes the given
{@code IDisposable}, if the {@code IDisposable} is
* not {@code null} and is
not already disposed.
*
* @param disposable
* {@code
IDisposable} to dispose
*/
public static void dispose(final IDisposable
disposable) {
if (disposable != null &&
!disposable.isDisposed()) {
disposable.dispose();
}
}
}
}
Can be used like this:
IDisposable.Util.dispose(getNavigationNode());
Yep, requires getting used to it!
Thoughts and feedback?
Tschüß,
Stefan