class CreateMultipleFunctionsCmd extends BackgroundCommand {

	@Override
	public boolean applyTo(DomainObject obj, TaskMonitor monitor) {
		program = (Program) obj;

		Listing listing = program.getListing();
		AddressSet addressSet = new AddressSet(selection);
		Address address = addressSet.getMinAddress();
		int functionsCreated = 0;
		long numInitialAddresses = addressSet.getNumAddresses();
		monitor.initialize(numInitialAddresses);
		monitor.setMessage("Creating functions from selection.");

		while (address != null && !monitor.isCancelled()) {
			// Try to get a function containing the current address.
			Function function = listing.getFunctionContaining(address);
			if (function == null) {
				// Create the next function.
				Function createdFunction = createFunction(address, program, monitor);
				if (createdFunction != null) {
					functionsCreated++;
				}
			}

			if (function != null) {
				AddressSetView body = function.getBody();
				addressSet.delete(body);
			}
			addressSet.deleteRange(address, address);
			address = addressSet.getMinAddress();
			long numRemainingAddresses = addressSet.getNumAddresses();
			monitor.setProgress(numInitialAddresses - numRemainingAddresses);
		}
		if (monitor.isCancelled()) {
			Msg.info(this, "User canceled " + getName() + ".");
			return false;
		}
		Msg.info(this, functionsCreated + " functions created by " + getName() + ".");
		return true;
	}

	public  CreateMultipleFunctionsCmd(AddressSetView selection, SourceType source);

}