Files
Common-VS2019/SacsStandardBase/Base/Bus/ItemId.cs
T
2022-07-16 13:33:22 +01:00

116 lines
3.8 KiB
C#

using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace Sarand.Net.Standard.Base.Bus
{
//=======================================================================
[Serializable()]
public class ItemId
{
//-------------------------------------------------------------------
// Data members.
public enum ItemIdType { Unknown, SysId, String }
private ItemIdType idType = ItemIdType.Unknown;
//-------------------------------------------------------------------
// Construction.
public ItemId() { }
public ItemId(short itemType, SysId id, String caption = "")
{
Type = itemType;
Id = SysId.InvalidOnNull(id);
idType = ItemIdType.SysId;
Caption = StringUtil.EmptyOnNull(caption);
}
public ItemId(short itemType, String stringId, String caption = "")
{
Type = itemType;
StringId = StringUtil.EmptyOnNull(stringId);
idType = ItemIdType.String;
Caption = StringUtil.EmptyOnNull(caption);
}
//-------------------------------------------------------------------
// Properties.
[XmlAttribute]
public short Type { get; private set; } = 0;
[XmlAttribute]
public SysId Id { get; private set; } = new SysId();
public String StringId { get; private set; } = "";
public ItemIdType IdType { get { return idType; } }
// Caption - display text to identify this item in a list.
public String Caption { get; set; } = "";
// Data value - additional information for this item.
public String Data { get; set; } = "";
//-------------------------------------------------------------------
// Utilities.
/// <summary>Verify that the Item Id is non-null and that the Item Type is specified (>0) and the Id is valid (>0).</summary>
public static bool IsValid(ItemId itemId)
{
if (itemId == null) return false;
if (itemId.Type < 1) return false;
if (itemId.idType == ItemIdType.SysId && !SysId.IsValid(itemId.Id)) return false;
if (itemId.idType == ItemIdType.String && StringUtil.IsEmpty(itemId.StringId)) return false;
return true;
}
/// <summary>Return the Item Id if non-=null, otherwise empty Item Id.</summary>
public static ItemId InvalidOnNull (ItemId itemId)
{
return itemId ?? new ItemId();
}
//-------------------------------------------------------------------
// Comparison.
public override bool Equals(object obj)
{
if (obj == null) return false;
return Equals(obj as ItemId);
}
public bool Equals(ItemId itemId)
{
if (itemId == null) return false;
if (this.Type != itemId.Type) return false;
if (this.idType != itemId.idType) return false;
if (idType == ItemIdType.SysId && SysId.ToInt(Id) != SysId.ToInt(itemId.Id)) return false;
if (idType == ItemIdType.String && !StringId.EqualsCI(itemId.StringId)) return false;
return true;
}
public override int GetHashCode()
{
return SysId.ToInt(Id);
}
//-------------------------------------------------------------------
// Conversion.
public override String ToString()
{
return ToString(this);
}
// Null-safe static versions.
public static String ToString(ItemId itemId)
{
return ToString(itemId, String.Empty);
}
public static String ToString(ItemId itemId, String invalidValue)
{
if (itemId == null || !IsValid(itemId)) return invalidValue;
return itemId.Type.ToString() + (SysId.IsValid(itemId.Id) ? ":" + SysId.ToString(itemId.Id) : "");
}
//-------------------------------------------------------------------
}
//=======================================================================
public static class ItemIdList
{
public static void Add(this List<ItemId> values, short type, SysId id)
{
values.Add(new ItemId(type, id));
}
}
//=======================================================================
}