Advertisement
Guest User

NavigationNode.cs

a guest
Jul 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4.  
  5. namespace TelegramBot.Navigation
  6. {
  7.     public abstract class NavigationNode<T> where T : NavigationNode<T>
  8.     {
  9.         public string HexID { get; private set; }
  10.         public string FullAddress { get; private set; }
  11.         public int Depth { get; private set; }        
  12.         public T Parent { get; private set; }
  13.         protected List<T> _children = new List<T>();
  14.         public IReadOnlyList<T> Children => _children.AsReadOnly();
  15.  
  16.  
  17.         protected NavigationNode(T parent = null)
  18.         {
  19.             SetParent(parent);
  20.         }
  21.         private void _SetProperties()
  22.         {
  23.             _SetHexID();
  24.             _SetDepth();
  25.             _SetFullAddress();
  26.         }
  27.         private void _SetHexID()
  28.         {
  29.             if (Parent == null)
  30.             {
  31.                 HexID = "r";
  32.             }
  33.             else
  34.             {
  35.                 HexID = Convert.ToString(Parent._children.IndexOf(this as T), 16);
  36.             }
  37.         }
  38.         private void _SetDepth()
  39.         {
  40.             if (Parent == null)
  41.             {
  42.                 Depth = 0;
  43.             }
  44.             else
  45.             {
  46.                 Depth = Parent.Depth + 1;
  47.             }
  48.         }
  49.         private void _SetFullAddress()
  50.         {
  51.             FullAddress = HexID;
  52.             T currentNode = this as T;
  53.             for (int i = 0; i < Depth; i++)
  54.             {
  55.                 FullAddress = FullAddress.Insert(0, currentNode.Parent.HexID);
  56.                 currentNode = currentNode.Parent;
  57.             }
  58.         }
  59.         public t GetChild<t>() where t : NavigationNode<T>
  60.         {
  61.             return _children.Find(x => x is t) as t;
  62.         }
  63.         public async Task<t> GetChildAsync<t>() where t : NavigationNode<T>
  64.         {
  65.             return await Task.Run(() => _children.Find(x => x is t) as t);
  66.         }
  67.         public void SetParent(T newParent)
  68.         {
  69.             if (Parent != null)
  70.             {
  71.                 Parent._children.Remove(this as T);
  72.             }
  73.             if (newParent != null)
  74.             {
  75.                 if (newParent._children.Count >= 16)
  76.                 {
  77.                     throw new ArgumentOutOfRangeException("Maximum capacity of the node has been reached", "newParent");
  78.                 }
  79.                 newParent._children.Add(this as T);
  80.                 Parent = newParent;
  81.             }
  82.             _SetProperties();
  83.         }
  84.         public T GetChild(int index)
  85.         {
  86.             return _children[index];
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement