classdef CoderCollection < handle
    
    properties
        arrayOfCoderItems
    end
    
    properties (Abstract)
        Coders
    end
    
  
    methods
        
        % public

        
        function add(obj,codername,params)
            nArray=obj.getarraysize;
            if obj.isvalidcodername(codername)
                obj.arrayOfCoderItems{nArray+1}=obj.createcoder(codername,params);
            end
        end
        
        function cicoll=proc(obj,item)
            if isa(item,'ReferenceItem')
                cicoll=obj.procItem(item);
            elseif isa(item,'ReferenceCollection')
                cicoll=obj.procColl(item);
            else
                error('Invalid object type');
            end
        end
        
        % private
        
        function cicoll=procItem(obj,item)
%            cicoll=CodedItemCollection;
            nCoder=obj.getarraysize;
            for iCoder=1:nCoder,
                if iCoder==1
                cicoll=obj.arrayOfCoderItems{iCoder}.procItem(item);
                else
                    cicoll=cicoll+obj.arrayOfCoderItems{iCoder}.procItem(item);
                end
            end
        end
        
        function cicoll=procColl(obj,coll)
           %cicoll=CodedItemCollection;
            nCoder=obj.getarraysize;
            for iCoder=1:nCoder,
                if iCoder==1
                    cicoll=obj.arrayOfCoderItems{iCoder}.procColl(coll);
                else
                cicoll=cicoll+obj.arrayOfCoderItems{iCoder}.procColl(coll);
                end
            end
        end
        
        function nArray=getarraysize(obj)
            nArray=length(obj.arrayOfCoderItems);
        end
        
        
        function b=isvalidcodername(obj,codername)
            b=sum(sum(strcmp(obj.Coders,codername)));
        end
        
        function o=createcoder(obj,codername,params) %#ok<INUSD>
            iClass=strcmp(obj.Coders,codername);
            iClass=find(iClass(:,1));
            if isempty(iClass)
                error('Coder name %s not found',codername);
            end
            if length(iClass)>1
                error('Coder name %s is not unique',codername);
            end
            eval(sprintf('o=%s(params);',obj.Coders{iClass,2}));
        end

        
    end
    
    
end