Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace TelegramBot.Navigation
- {
- public abstract class NavigationNode<T> where T : NavigationNode<T>
- {
- public string HexID { get; private set; }
- public string FullAddress { get; private set; }
- public int Depth { get; private set; }
- public T Parent { get; private set; }
- protected List<T> _children = new List<T>();
- public IReadOnlyList<T> Children => _children.AsReadOnly();
- protected NavigationNode(T parent = null)
- {
- SetParent(parent);
- }
- private void _SetProperties()
- {
- _SetHexID();
- _SetDepth();
- _SetFullAddress();
- }
- private void _SetHexID()
- {
- if (Parent == null)
- {
- HexID = "r";
- }
- else
- {
- HexID = Convert.ToString(Parent._children.IndexOf(this as T), 16);
- }
- }
- private void _SetDepth()
- {
- if (Parent == null)
- {
- Depth = 0;
- }
- else
- {
- Depth = Parent.Depth + 1;
- }
- }
- private void _SetFullAddress()
- {
- FullAddress = HexID;
- T currentNode = this as T;
- for (int i = 0; i < Depth; i++)
- {
- FullAddress = FullAddress.Insert(0, currentNode.Parent.HexID);
- currentNode = currentNode.Parent;
- }
- }
- public t GetChild<t>() where t : NavigationNode<T>
- {
- return _children.Find(x => x is t) as t;
- }
- public async Task<t> GetChildAsync<t>() where t : NavigationNode<T>
- {
- return await Task.Run(() => _children.Find(x => x is t) as t);
- }
- public void SetParent(T newParent)
- {
- if (Parent != null)
- {
- Parent._children.Remove(this as T);
- }
- if (newParent != null)
- {
- if (newParent._children.Count >= 16)
- {
- throw new ArgumentOutOfRangeException("Maximum capacity of the node has been reached", "newParent");
- }
- newParent._children.Add(this as T);
- Parent = newParent;
- }
- _SetProperties();
- }
- public T GetChild(int index)
- {
- return _children[index];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement