123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- using System;
- using System.Text;
- using System.Collections.Generic;
- namespace Wayne.Lib
- {
-
-
-
- public interface IIdentifiableEntity
- {
- #region Properties
-
-
-
- int Id { get; }
-
-
-
- string EntityType { get; }
-
-
-
- string FullEntityName { get; set; }
-
-
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubType")]
- string EntitySubType { get; }
-
-
-
- IIdentifiableEntity ParentEntity { get; }
- #endregion
- }
-
-
-
-
- public class IdentifiableEntity : IIdentifiableEntity
- {
- #region Fields
- private readonly int id;
- private readonly string entityType;
- private readonly string entitySubType;
- private readonly IIdentifiableEntity parentEntity;
- #endregion
- #region Construction
- static IdentifiableEntity()
- {
- Empty = new IdentifiableEntity(NoId, string.Empty, string.Empty, null);
- }
-
-
-
-
-
-
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubType")]
- public IdentifiableEntity(int id, string entityType, string entitySubType, IIdentifiableEntity parentEntity)
- {
- this.id = id;
- this.entityType = entityType;
- this.entitySubType = entitySubType;
- this.parentEntity = parentEntity;
- }
- #endregion
- #region IIdentifiableEntity Members
-
-
-
- public int Id
- {
- get { return id; }
- }
-
-
-
- public string EntitySubType
- {
- get { return entitySubType; }
- }
-
-
-
- public string EntityType
- {
- get { return entityType; }
- }
-
-
-
- public string FullEntityName { get; set; }
-
-
-
- public IIdentifiableEntity ParentEntity
- {
- get { return parentEntity; }
- }
- #endregion
- #region Misc Static Members
-
-
-
- public const int NoId = int.MinValue;
-
-
-
- public static IdentifiableEntity Empty { get; private set; }
-
-
-
-
-
-
- public static IIdentifiableEntity[] GetAncestorArray(IIdentifiableEntity entity)
- {
- List<IIdentifiableEntity> ancestors = new List<IIdentifiableEntity>();
- while (entity != null)
- {
- ancestors.Add(entity);
- entity = entity.ParentEntity;
- }
- return ancestors.ToArray();
- }
-
-
-
-
-
-
-
- public static bool Equals(IIdentifiableEntity entity1, IIdentifiableEntity entity2)
- {
- if (entity1 == null)
- throw new ArgumentNullException("entity1");
- if (entity2 == null)
- throw new ArgumentNullException("entity2");
- if (entity1 == entity2)
- return true;
- EnsureFullEntityName(entity1);
- EnsureFullEntityName(entity2);
- return entity1.FullEntityName == entity2.FullEntityName;
- }
- private static void EnsureFullEntityName(IIdentifiableEntity entity1)
- {
- if (string.IsNullOrEmpty(entity1.FullEntityName))
- {
- entity1.FullEntityName = ToString(entity1, true);
- }
- }
- #endregion
- #region ToString
-
-
-
-
- public override string ToString()
- {
- if (string.IsNullOrEmpty(FullEntityName))
- {
- FullEntityName = ToString(this, true);
- }
- return FullEntityName;
- }
-
-
-
-
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
- public static string ToString(IIdentifiableEntity entity)
- {
- return ToString(entity, false);
- }
-
-
-
-
-
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
- public static string ToString(IIdentifiableEntity entity, bool ancestors)
- {
- if ((entity == null) || (entity == Empty))
- return string.Empty;
- StringBuilder builder = new StringBuilder();
- if (ancestors)
- {
- List<string> entities = new List<string>();
- do
- {
-
- builder.Append(entity.EntityType);
-
- if (!string.IsNullOrEmpty(entity.EntitySubType))
- {
- builder.Append("{");
- builder.Append(entity.EntitySubType);
- builder.Append("}");
- }
-
- if (entity.Id != NoId)
- {
- builder.Append('#');
- builder.Append(entity.Id.ToString(System.Globalization.CultureInfo.InvariantCulture));
- }
- entity = entity.ParentEntity;
- entities.Add(builder.ToString());
- builder.Length = 0;
- }
- while (entity != null);
- for (int i = entities.Count - 1; i >= 0; i--)
- {
- builder.Append("\\");
- builder.Append(entities[i]);
- }
- }
- else
- {
-
- builder.Append(entity.EntityType);
-
- if (!string.IsNullOrEmpty(entity.EntitySubType))
- {
- builder.Append("{");
- builder.Append(entity.EntitySubType);
- builder.Append("}");
- }
-
- if (entity.Id != NoId)
- builder.Append(entity.Id.ToString(System.Globalization.CultureInfo.InvariantCulture));
- }
- return builder.ToString();
- }
- #endregion
- #region Parse
-
-
-
-
-
- public static IIdentifiableEntity[] ParseAncestorString(string ancestryString)
- {
- string[] entityStrings = ancestryString.Split('\\');
- List<IIdentifiableEntity> identifiableEntityList = new List<IIdentifiableEntity>();
- IIdentifiableEntity currentParent = null;
- System.Text.RegularExpressions.Regex allowedEntityStringFormat = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9_\.]+({[a-zA-Z0-9_\.]+})?(#[0-9]+)?$");
- foreach (string entityString in entityStrings)
- {
- if (string.IsNullOrEmpty(entityString))
- continue;
- if (!allowedEntityStringFormat.IsMatch(entityString))
- return new IIdentifiableEntity[0];
- string entityType;
- string entitySubType = string.Empty;
- int id = NoId;
-
-
-
-
-
- string[] firstSplit = entityString.Split('#');
- if (firstSplit.Length >= 1)
- {
-
- List<string> secondSplit = new List<string>(firstSplit[0].Split('{', '}'));
- while (secondSplit.Contains(string.Empty))
- secondSplit.Remove(string.Empty);
- if (secondSplit.Count == 1)
- {
-
- entityType = secondSplit[0];
- }
- else if (secondSplit.Count == 2)
- {
-
- entityType = secondSplit[0];
- entitySubType = secondSplit[1];
- }
- else
- return new IIdentifiableEntity[0];
- if (firstSplit.Length == 2)
- id = int.Parse(firstSplit[1], System.Globalization.CultureInfo.InvariantCulture);
- else if (firstSplit.Length > 2)
- return new IIdentifiableEntity[0];
- }
- else
- return new IIdentifiableEntity[0];
- IIdentifiableEntity entity = new IdentifiableEntity(id, entityType, entitySubType, currentParent);
- identifiableEntityList.Add(entity);
- currentParent = entity;
- }
- return identifiableEntityList.ToArray();
- }
-
-
-
-
-
- public static IIdentifiableEntity Parse(string path)
- {
- System.Text.RegularExpressions.Regex allowedEntityStringFormat = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9_\.]+({[a-zA-Z0-9_\.]+})?(#[0-9]+)?$");
- string[] entityStrings = path.TrimStart('\\').Split('\\');
- IdentifiableEntity currentParent = null;
- foreach (string entityString in entityStrings)
- {
- if (!allowedEntityStringFormat.IsMatch(entityString))
- return null;
- string entityType;
- string entitySubType = string.Empty;
- int id = NoId;
-
-
-
-
-
- string[] firstSplit = entityString.Split('#');
- if (firstSplit.Length >= 1)
- {
-
- List<string> secondSplit = new List<string>(firstSplit[0].Split('{', '}'));
- while (secondSplit.Contains(string.Empty))
- secondSplit.Remove(string.Empty);
- if (secondSplit.Count == 1)
- {
-
- entityType = secondSplit[0];
- }
- else if (secondSplit.Count == 2)
- {
-
- entityType = secondSplit[0];
- entitySubType = secondSplit[1];
- }
- else
- return null;
- if (firstSplit.Length == 2)
- id = int.Parse(firstSplit[1], System.Globalization.CultureInfo.InvariantCulture);
- else if (firstSplit.Length > 2)
- return null;
- }
- else
- return null;
- IdentifiableEntity entity = new IdentifiableEntity(id, entityType, entitySubType, currentParent);
- currentParent = entity;
- }
- return currentParent;
- }
- public class EqualityComparer : IEqualityComparer<IIdentifiableEntity>
- {
- public bool Equals(IIdentifiableEntity x, IIdentifiableEntity y)
- {
- return IdentifiableEntity.Equals(x, y);
- }
- public int GetHashCode(IIdentifiableEntity obj)
- {
- EnsureFullEntityName(obj);
- return obj.FullEntityName.GetHashCode();
- }
- }
- #endregion
- }
- }
|