classdef MetricItem <  matlab.mixin.Heterogeneous & handle
    
    properties
        name
    end
    
    methods (Abstract)
        val=calc(obj,item1,item2)
    end
    
    
    methods
        % public
        function obj=MetricItem(name)
            obj.name=name;
        end
        
        function name=getname(obj)
            name=obj.name;
        end
        
        function [val,isCached]=CheckCache(obj,item1,item2)
            fprintf('Searching for coded item in metric cache...\n')
            [~,NAME1,EXT1] = fileparts(item1.getfilename);
            [~,NAME2,EXT2] = fileparts(item2.getfilename);
            
            fname1=sprintf('%s%s',NAME1,EXT1);
            fname2=sprintf('%s%s',NAME2,EXT2);
            
            if exist('MetricsCache.mat','file')
                load('MetricsCache.mat','MetricsCache');
                isCached=0; val=0;
                for iData=1:size(MetricsCache,1), %#ok<USENS>
                    if strcmp(class(obj),MetricsCache{iData,1}) &&...
                            strcmp(MetricsCache{iData,2},fname1) && ...
                            strcmp(MetricsCache{iData,3},fname2)
                        isCached=1;
                        val=MetricsCache{iData,4};
                    end
                end
            else
                isCached=0; val=0;
            end
            if isCached
                fprintf('Coded item found in metric cache...\n')
            else
                fprintf('Coded item not found in metric cache...\n')
            end
        end
        
        function StoreToCache(obj,item1,item2,val)
            
            [~,NAME1,EXT1] = fileparts(item1.getfilename);
            [~,NAME2,EXT2] = fileparts(item2.getfilename);
            
            fname1=sprintf('%s%s',NAME1,EXT1);
            fname2=sprintf('%s%s',NAME2,EXT2);
            
            
            if exist('MetricsCache.mat','file')
                load('MetricsCache.mat','MetricsCache');
            else
                MetricsCache=cell(0,4);
            end
            nlast=size(MetricsCache,1);
            MetricsCache{nlast+1,1}=class(obj);
            MetricsCache{nlast+1,2}=fname1;
            MetricsCache{nlast+1,3}=fname2;
            MetricsCache{nlast+1,4}=val;
                    save('MetricsCache.mat','MetricsCache');
        end
    end
end