Files
Common-VS2019/SacsStandardBase/Base/Core/CodeDesc.cs
T
2025-01-16 13:20:10 +00:00

124 lines
4.3 KiB
C#

using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
public class CodeDesc
{
//-------------------------------------------------------------------
// Data members.
//-------------------------------------------------------------------
public CodeDesc(String code, String desc, String comment = "")
{
Code = StringUtil.EmptyOnNull(code);
Desc = StringUtil.EmptyOnNull(desc);
Comment = StringUtil.EmptyOnNull(comment);
}
//-------------------------------------------------------------------
// Properties.
public String Code { get; }
public String Desc { get; }
public String Comment { get; }
//-------------------------------------------------------------------
}
//=======================================================================
public static class CodeDescList
{
//-------------------------------------------------------------------
// Population.
public static void Add (this List<CodeDesc> values, String code, string desc, String comment = "")
{
values.Add(new CodeDesc(code, desc, comment));
}
public static void Insert (this List<CodeDesc> values, int idx, String code, string desc, String comment = "")
{
values.Insert (idx, new CodeDesc(code, desc, comment));
}
//-------------------------------------------------------------------
// Sorting.
public static void SortByCode (this List<CodeDesc> values)
{
values.Sort(
delegate (CodeDesc codeDesc1, CodeDesc codeDesc2)
{
return codeDesc1.Code.CompareTo(codeDesc2.Code);
}
);
}
public static void SortByDesc (this List<CodeDesc> values)
{
values.Sort(
delegate (CodeDesc codeDesc1, CodeDesc codeDesc2)
{
return codeDesc1.Desc.CompareTo(codeDesc2.Desc);
}
);
}
//-------------------------------------------------------------------
// Conversion.
public static List<String> GetCodeList (this List<CodeDesc> values)
{
List<String> codes = new List<string>();
foreach (var value in values)
if (!codes.Contains(value.Code)) codes.Add(value.Code);
return codes;
}
public static List<String> GetDescList (this List<CodeDesc> values)
{
List<String> descs = new List<string>();
foreach (var value in values)
if (!descs.Contains(value.Desc)) descs.Add(value.Desc);
return descs;
}
//-------------------------------------------------------------------
// Lookup.
public static bool FindCode (this List<CodeDesc> values, String code)
{
return values.Exists(item => item.Code.EqualsCI(code));
}
public static CodeDesc GetByCode (this List<CodeDesc> values, String code)
{
return values.Find(item => item.Code.EqualsCI(code));
}
public static CodeDesc GetByNumericCode (this List<CodeDesc> values, String code)
{
if (!StringUtil.IsNumeric(code)) return null;
return values.Find(item => StringUtil.IsNumeric(item.Code) && StringUtil.ToInt(item.Code) == StringUtil.ToInt(code));
}
//-------------------------------------------------------------------
// Lookup code.
/// <summary>Return code matching supplied code (case-insenstive) - to ensure case matches list.</summary>
public static String GetCodeForCode (this List<CodeDesc> values, String code, String defaultValue = "")
{
foreach (var value in values)
if (value.Code.EqualsCI(code)) return value.Code;
return StringUtil.EmptyOnNull(defaultValue);
}
/// <summary>Return code matching supplied description (case-insenstive).</summary>
public static String GetCodeForDesc (this List<CodeDesc> values, String desc, String defaultValue = "")
{
foreach (var value in values)
if (value.Desc.EqualsCI(desc)) return value.Code;
return StringUtil.EmptyOnNull(defaultValue);
}
//-------------------------------------------------------------------
// Lookup desc.
public static String GetDescForCode (this List<CodeDesc> values, String code, String defaultValue = "")
{
foreach (var value in values)
if (value.Code.EqualsCI(code)) return value.Desc;
return StringUtil.EmptyOnNull(defaultValue);
}
//-------------------------------------------------------------------
}
//=======================================================================
}