classdef Interval
    properties 
        name
    end
    
    properties (SetAccess = private) % read-only
        low = [];
        high = [];
        lowIncluded = true;
        highIncluded = true;
    end
    
    
    methods
        function l = getFlatLow(obj)
            l = obj.low;
            
            if isa(l, 'embedded.fi')
                l= l.double;
            end
         end
         function h = getFlatHigh(obj)
             h = obj.high;
             
             if isa(h, 'embedded.fi')
                 h = h.double;
             end
         end
         function out = getBoundary(obj)
             if obj.lowIncluded
                 if obj.highIncluded
                     out = '[]';
                 else
                     out = '[)';
                 end
             else
                 if obj.highIncluded
                     out = '(]';
                 else
                     out = '()';
                 end
             end
         end
    end
    
    methods
        function obj = Interval(varargin)
            checkArg(nargin);
            
            point1 = varargin{1};
            point2 = varargin{2};
            if nargin==3
                boundaryInfo = varargin{3};
            else
                boundaryInfo = '';
            end

            checkPoints(point1, point2, boundaryInfo);
            
            obj.low = point1;
            obj.high = point2;
            if ~isempty(boundaryInfo),
                switch(boundaryInfo),
                    case '()'
                        obj.lowIncluded = false;
                        obj.highIncluded = false;
                    case '(]'
                        obj.lowIncluded = false;
                    case '[)'
                        obj.highIncluded = false;
                end
            end
            
        end
    end
end

function checkArg(n)
    if ~(n==2 || n==3)
        error(message('Sldv:Interval:ArgCheck'));        
    end       
end

function checkPoints(p1, p2, boundaryInfo)
    if ~(isnumeric(p1)||islogical(p1)) || ...
            ~(isnumeric(p2) || islogical(p2))
        error(message('Sldv:Interval:NonNumeric'));
    end
    size1 = size(p1);
    size2 = size(p2);
    
    if length(size1)~=length(size2) || any(size1 ~= size2)
        error(message('Sldv:Interval:DimMisMatch'));
    end    
    
    if any(p1 > p2)
            error(message('Sldv:Interval:Invalid'));
    end
    
    if ~isempty(boundaryInfo)
        if ~ischar(boundaryInfo) || ...
                ~any(strcmp(boundaryInfo,{'()','(]','[)','[]'}))
            error(message('Sldv:Interval:BadBoundaries'));
        end
    end 
end
