gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/+matlab/+bigdata/+internal/+lazyeval/ClosurePromise.m

    %ClosurePromise
% A class that represents the promise end of a ClosureFuture.

% Copyright 2015 The MathWorks, Inc.

classdef (Sealed) ClosurePromise < handle
    
    properties (SetAccess = immutable)
        % A unique ID.
        Id
        
        % A unique ID char vector.
        IdStr
    end
    
    properties (SetAccess = private, Dependent)
        % A flag that is true if and only if the value this promise
        % represents has been calculated.
        IsDone
        
        % Predecessor nodes.
        Predecessors
    end
    
    properties (SetAccess = private)
        % The underlying closure that will fulfill this promise.
        Closure = matlab.bigdata.internal.lazyeval.Closure.empty();
        
        % The index into the output of the underlying closure that this
        % promise represents.
        ArgoutIndex = 1;
        
        % A reference to the actual value if it has been calculated.
        CachedValue = [];
        
        % A reference to the Future object corresponding to this promise.
        Future;
    end
    
    properties (Access = private, Constant)
        % The means by which this class receives unique IDs.
        IdFactory = matlab.bigdata.internal.util.UniqueIdFactory('ClosurePromise');
    end
    
    methods
        % The main constructor.
        %
        %  obj = ClosurePromise(closure, argoutIndex) creates a promise
        %   around a closure/argoutIndex combination.
        %
        %  obj = ClosurePromise(value) creates a promise that has already
        %   been fulfilled with the given value.
        function obj = ClosurePromise(closureOrValue, argoutIndex)
            import matlab.bigdata.internal.lazyeval.ClosureFuture;
            
            obj.Id = obj.IdFactory.nextId();
            obj.IdStr = sprintf('promise_%s', obj.Id);
            obj.Future = ClosureFuture(obj);
            
            if nargin >= 2
                obj.Closure = closureOrValue;
                obj.ArgoutIndex = argoutIndex;
            else
                obj.CachedValue = closureOrValue;
            end
        end

        function pred = get.Predecessors(obj)
            pred = obj.Closure;
        end
        
        % Set the cached value of this promise.
        %
        % This completes the promise and replaces the Closure reference
        % with the actual value.
        function setValue(obj, value)
            import matlab.bigdata.internal.lazyeval.Closure;
            assert(~obj.IsDone);
            obj.CachedValue = value;
            obj.Closure     = Closure.empty(); %#ok<PROPLC>
        end
        
        % Swap two ClosurePromise instances.
        %
        % The caller must guarantee these two promises are equivalent, that
        % the two promises will be given the same output when their respective
        % closures are evaluated.
        %
        % This exists to allow optimizers to move output promises from one
        % Closure instance to another. It is a swap instead a pure move
        % because both Closure and ClosurePromise instances are not allowed
        % to be in an invalid state.
        function swap(promise1, promise2)
            [promise1.Future, promise2.Future] = deal(promise2.Future, promise1.Future);
            [promise1.Future.Promise, promise2.Future.Promise] = deal(promise2.Future.Promise, promise1.Future.Promise);
        end
        
        function isDone = get.IsDone(obj)
            isDone = isempty(obj.Closure);
        end
    end
end