Files
Common-VS2019/SacsStandardBase/Base/Core/CodeParam.cs
T
2021-05-05 15:36:28 +01:00

67 lines
2.2 KiB
C#

using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
/// <summary>
/// Code parameter value with flag to indicate whether the value
/// is included (valid as a filter) or excluded (explicitly not matched).
/// </summary>
public class CodeParam
{
//-------------------------------------------------------------------
// Construction.
public CodeParam (String code, bool including)
{
Code = StringUtil.EmptyOnNull(code);
Including = including;
}
//-------------------------------------------------------------------
// Properties.
/// <summary>Code parameter.</summary>
public String Code { get; }
/// <summary>Whether the value is included (valid as a filter) or excluded (explicitly not matched).</summary>
public bool Including { get; }
//-------------------------------------------------------------------
/// <summary>Determine if the two code params 'match' - taking code and 'sense' flag into account.</summary>
public bool Matches (CodeParam cp)
{
// Values match if either code is blank:
if (StringUtil.IsEmpty(Code) || StringUtil.IsEmpty(cp.Code)) return true;
// Values match if both 'senses' are negative:
if (!Including && !cp.Including) return true;
// Values match if both 'senses' are positive and the codes are the same:
if (Including && cp.Including) return Code.EqualsCI(cp.Code);
// Values match if 'senses' and codes are different:
if (Including != cp.Including) return !Code.EqualsCI(cp.Code);
return false;
}
//-------------------------------------------------------------------
/// <summary>Compare code params - first on code then on flag.</summary>
public int Compare (CodeParam cp)
{
if (Code.EqualsCI(cp.Code))
{
// Compare 'Including' flags:
if (!Including && cp.Including) return 1;
if (Including && !cp.Including) return -1;
return 0;
}
return Code.CompareTo(cp.Code);
}
//-------------------------------------------------------------------
}
//=======================================================================
}