%CombinedTask
% Class for holding all the details necessary for performing several tasks
% in a single parfevalOnAll.

%   Copyright 2015-2016 The MathWorks, Inc.

classdef (Sealed, Hidden) CombinedTask < parallel.internal.mapreduce.AbstractTask
    properties (GetAccess = private, SetAccess = immutable)
        Tasks = {}; % A cell array of tasks.
    end
    
    methods
        % Create a finalize shuffle-sort task
        % Syntax:
        % CombinedTask(task1, task2, task3, ..., shuffleSortStrategy)
        % CombinedTask(task1, task2, task3, ..., remoteMapReduceDetails)
        function obj = CombinedTask(varargin)
            obj = obj@parallel.internal.mapreduce.AbstractTask(varargin{end});
            obj.Tasks = varargin(1:end-1);
        end
        
        % Runs the given map task.
        function results = run(obj)
            tasks = obj.Tasks;
            results = cell(size(tasks));
            for ii = 1:numel(tasks)
                task = tasks{ii};
                results{ii} = task.run();
            end
            results = vertcat(results{:});
        end
    end
    
    methods (Hidden)
        % Launch a task on the given pool.
        % Visitor will be an object/struct with both a runOnSingle and a 
        % runOnAll method. This should call one of those and return the
        % result.
        function future = hLaunchTask(obj, visitor)
            future = visitor.runOnAll(obj);
        end
    end
end
