%

% Copyright 2014 The MathWorks, Inc.

classdef Waiter < handle
    properties
        WaitDelay
    end
    
    methods
        function obj = Waiter(waitDelay)
            obj.WaitDelay = waitDelay;
        end
        
        function ok = wait(obj, timeout)
            if timeout < obj.WaitDelay
                obj.WaitDelay = timeout;
            end

            ok = false;
            t1 = tic;
            while ~ok
                ok = obj.doCheck();
                if ~ok
                    elapsedTime = toc(t1);
                    if elapsedTime >= timeout
                        return
                    end
                    pause(obj.WaitDelay);
                else
                    return
                end
            end
        end
    end

    methods (Abstract, Access = protected)
        ok = doCheck(obj);
    end
end
