%

%   Copyright 2013 The MathWorks, Inc.

classdef (Hidden, Sealed) PoolSettings < parallel.internal.settings.CustomSettingsGetSet
    properties (Access = protected)
        NodeFacade
    end
    
    properties (Transient, Access = private)
        Listeners
    end

    %---------------------------------------------------
    % Properties stored in Settings
    %---------------------------------------------------
    properties (PCTGetSet, Transient, PCTConstraint = 'logicalscalar')
        % Whether or not we auto-create the pool.
        AutoCreate
        % Whether or not the pool is restarted if the default profile is
        % changed
        RestartOnClusterChange 
        % Whether or not the pool is restarted if PreferredNumWorkers is
        % changed
        RestartOnPreferredNumWorkersChange        
    end
    properties (PCTGetSet, Transient, PCTConstraint = 'positivenonzerointscalar')
        % The timeout in minutes after which an idle pool will be deleted.
        IdleTimeout
        % The preferred number of workers in the pool
        PreferredNumWorkers
    end
        
    events
        % Indicates that the value for IdleTimeout was changed
        IdleTimeoutChanged
        % Indicates that the value for PreferredNumWorkesr was changed
        PreferredNumWorkersChanged
        % Indicate that the value for AutoCreate was changed
        AutoCreateChanged
    end

    methods
        function obj = PoolSettings()
            import parallel.internal.settings.ParallelSettingsTree
            import parallel.internal.settings.SettingsNodeFacade
            obj.NodeFacade = SettingsNodeFacade(ParallelSettingsTree.PoolSettingsNode, ...
                ParallelSettingsTree.PoolNodeName, ParallelSettingsTree.ClientSettingsNode);
            idleTimeoutChangedListener = obj.NodeFacade.addChangeListenerToKeys(...
                {'IdleTimeout'}, @obj.handleIdleTimeoutChanged);
            preferredNumWorkersChangedListener = obj.NodeFacade.addChangeListenerToKeys(...
                {'PreferredNumWorkers'}, @obj.handlePreferredNumWorkersChanged);
            autoCreateChangedListener = obj.NodeFacade.addChangeListenerToKeys(...
                {'AutoCreate'}, @obj.handleAutoCreateChanged);
            obj.Listeners = [idleTimeoutChangedListener;...
                preferredNumWorkersChangedListener; ...
                autoCreateChangedListener];
        end
    end
    
    methods (Hidden)
        function handleIdleTimeoutChanged(obj, ~, ~)
            obj.notify('IdleTimeoutChanged');
        end
        
        function handlePreferredNumWorkersChanged(obj, ~, ~)
            obj.notify('PreferredNumWorkersChanged');
        end
        
        function handleAutoCreateChanged(obj, ~, ~)
            obj.notify('AutoCreateChanged');
        end
    end
    
end

%#ok<*ATUNK> - custom attributes