classdef MetricsCollection < handle & matlab.mixin.Copyable
    properties
        arrayOfMetricItems
    end
    
    properties (Abstract)
        Metrics
    end
    
    
    methods
        
        
        function obj=add(obj,metricname)
            nArray=obj.getarraysize;
            if obj.isvalidmetricname(metricname)
                obj.arrayOfMetricItems{nArray+1}=obj.createmetric(metricname);
            else
                error('Invalid metric name %s',metricname);
            end
        end
        
        function metrics=calc(obj,refitem,codeditem)
            if ~isa(refitem,'ReferenceItem')
                error('Only single reference item is supported here');
            end
            if ~isa(codeditem,'CodedItem')
                error('Only single coded item is supported here');
            end
            
            nMetrics=obj.getarraysize;
            
            for iMetric=1:nMetrics,
                metrics(iMetric).name=obj.arrayOfMetricItems{iMetric}.getname;
                metrics(iMetric).value=obj.arrayOfMetricItems{iMetric}.calc(refitem,codeditem);
            end
            
        end
        
        function n=getarraysize(obj)
            n=length(obj.arrayOfMetricItems);
        end
        
        function o=getelem(obj,index)
            o=obj.arrayOfMetricItems{index};
        end
        
        function b=isvalidmetricname(obj,metricname)
            b=sum(sum(strcmp(obj.Metrics,metricname)));
        end
        
        function o=createmetric(obj,metricname)
            iClass=strcmp(obj.Metrics,metricname);
            iClass=find(iClass(:,1));
            if isempty(iClass)
                error('Metric name %s not found',metricname);
            end
            if length(iClass)>1
                error('Metric name %s is not unique',metricname);
            end
            eval(sprintf('o=%s;',obj.Metrics{iClass,2}));
        end
        
        
        
    end
    
end