function targetFeatureObject = get(targetFeatureClass, varargin)
%TARGET.GET    Retrieve target feature object from memory.
%   targetFeatureObject = TARGET.GET(targetFeatureClass) retrieves all
%   target feature objects from memory. If no target feature objects exist
%   in memory, an empty array is returned.
%
%   targetFeatureObject = TARGET.GET(targetFeatureClass,
%   targetFeatureObjectIdentifier) uses the specified class and identifier
%   to retrieve a target feature object from memory. If the target feature
%   object does not exist in memory, an empty array is returned.
%
%   targetFeatureObject = TARGET.GET(targetFeatureClass, Name, Value)
%   retrieves from memory the target feature objects that match the
%   specified name-value pair arguments. If no such target feature objects
%   exist in memory, an empty array is returned.
%   Supported argument names include:
%
%   CaseSensitive       If value is true (default), string matching is case
%                       sensitive. If value is false, string matching is
%                       not case sensitive.
%   'propertyName'      Retrieve a target feature object for which the
%                       specified property is equal to the specified value.
%
%   See also TARGET.CREATE, TARGET.ADD, TARGET.REMOVE

%   Copyright 2018-2020 The MathWorks, Inc.
    
    narginchk(1, Inf)
    
    % Create target repository instance.
    tr = targetrepository.create(true);
    
    % Process inputs
    arguments = varargin;
    validateattributes(targetFeatureClass, {'string', 'char'}, {'scalartext'}, 1)
    switch nargin
        case 1
            % do nothing
        case 2
            validateattributes(arguments{1}, {'string', 'char', 'function_handle'}, {}, 2)
        otherwise
            % Create name-value manager.
            % By default, name-value manager does not allow partial
            % matching or case sensitiveness as users can add their own
            % properties (with custom names) to the target objects.
            parser = target.internal.NameValueManager('FunctionName', 'target.get');
            
            % Create dummy instance of class to extract default values.
            dummyInstance = tr.create(targetFeatureClass);
            
            % Validation of any further name-value pairs is performed by a
            % dynamically created input parser as we have different
            % acceptable parameters depending which object is added.
            parser.addSettableProperties(dummyInstance, ...
                'ValidationFunction', @(x) validateattributes(x, {'numeric', 'string', 'char', 'target.Data', 'cell'}, {}));
            
            % Add optional "CaseSensitive" flag for case sensitivity of
            % strings. We also have to support numeric binary entries as
            % the function signature cannot suggest logicals.
            parser.addParameter('CaseSensitive', true, @(x) validateattributes(x, {'logical', 'numeric'}, {'scalar', 'binary'}));
            
            parser.parse(arguments{:})
            arguments = parser.struct2matcell(parser.ProcessedResults);
    end
    targetFeatureObject = tr.get(targetFeatureClass, arguments{:});
end