When a method uses progress monitors extensively, it is inelegant to keep checking whether the monitor is null
or not. Instead, the progress monitor can be replaced with a NullProgressMonitor
, which acts as a no-op for all monitor calls.
Update the
checkDozen
to use aNullProgressMonitor
ifnull
is passed:private void checkDozen(IProgressMonitor monitor) { if (monitor == null) monitor = new NullProgressMonitor();
This allows the remainder of the method to run without modification and saves any
NullPointerException
errors that may result.A similar result to both the
NullProgressMonitor
and aSubProgressMonitor
with a wrapper/factory class calledSubMonitor
. This provides factory methods to wrap the monitor and create child progress monitors:protected IStatus run(IProgressMonitor monitor) { try { SubMonitor subMonitor = SubMonitor.convert(monitor,"Preparing", 5000); for (int i = 0; i < 50 && !subMonitor...