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()
method 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
NullPointerExceptions
that may result.A similar result is obtained for both the
NullProgressMonitor
andSubProgressMonitor
with a wrapper/factory class calledSubMonitor
. This provides factory methods to wrap the monitor and creates child progress monitors.protected IStatus run(IProgressMonitor monitor) { try { SubMonitor subMonitor = SubMonitor.convert(monitor,"Preparing", 5000); for (int i = 0; i < 50...