classdef ReferenceCollection < handle
    properties
        arrayOfReferenceItems
        type
    end
    
    methods (Abstract)
        CreateReferenceItem(filename)
        InitArray(obj)
    end
    
    methods
        % public
        function obj=ReferenceCollection(filenamemask)
            %obj.arrayOfReferenceItems=ReferenceItem.empty(0,1);
            obj.InitArray;
            obj.buildfromfilelist(filenamemask);
         end
        
        %function ref = subsref(obj,index)
        %    ref=obj.arrayOfReferenceItems{index};
        %end
        
		function o=getelem(obj,index)
			o=obj.arrayOfReferenceItems{index};
		end
		
        function nArray=getnumref(obj)
            nArray=length(obj.arrayOfReferenceItems);
        end
        
        function add(obj,filename)
            nl=obj.numfiles(filename);
            if ~nl
                error('File name or mask %s resulted in empty file list',filename);
            end
            if nl>1
                obj.buildfromfilelist(obj,filename);
            else
                nArray=length(obj.arrayOfReferenceItems);
                obj.arrayOfReferenceItems{nArray+1}=obj.CreateReferenceItem(filename);
            end
        end
        
        function nl=numfiles(obj,filenamemask)
            fileList=dir(filenamemask);
            nl=length(fileList);
        end
        
        % private
        function buildfromfilelist(obj,filenamemask)
            fileList=dir(filenamemask);
            nl=obj.numfiles(filenamemask);
            if ~nl
                error('File name mask %s resulted in empty file list',filenamemask);
            end
            for iFile=1:length(fileList)
                obj.add(fileList(iFile).name);
            end
        end
        
        
    end
end