Created
February 13, 2018 21:18
-
-
Save ThuCommix/47c9d4dfbb84502e6ee3f706bde1241a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Nightingale.Entities; | |
namespace Nightingale.Sessions | |
{ | |
/// <summary> | |
/// Represents a graph node within the <see cref="SessionGraph"/>. | |
/// </summary> | |
public class SessionGraphNode | |
{ | |
/// <summary> | |
/// Gets the entity for this session graph node. | |
/// </summary> | |
public Entity Entity { get; } | |
/// <summary> | |
/// Gets the root session graph for this node. | |
/// </summary> | |
public SessionGraph Graph { get; } | |
/// <summary> | |
/// Gets the sub nodes. | |
/// </summary> | |
public IEnumerable<SessionGraphNode> Nodes => _nodes; | |
private readonly List<SessionGraphNode> _nodes; | |
/// <summary> | |
/// Initializes a new SessionGraphNode class. | |
/// </summary> | |
/// <param name="sessionGraph">The session graph.</param> | |
/// <param name="entity">The entity.</param> | |
public SessionGraphNode(SessionGraph sessionGraph, Entity entity) | |
{ | |
Graph = sessionGraph; | |
Entity = entity; | |
_nodes = new List<SessionGraphNode>(); | |
} | |
/// <summary> | |
/// Adds a new session graph node to this node. | |
/// </summary> | |
/// <param name="sessionGraphNode">The session graph node.</param> | |
public void AddNode(SessionGraphNode sessionGraphNode) | |
{ | |
if (!Graph.Contains(sessionGraphNode.Entity)) | |
{ | |
Graph.AddEntity(sessionGraphNode.Entity); | |
_nodes.Add(sessionGraphNode); | |
} | |
} | |
/// <summary> | |
/// Adds a new session graph node to this node. | |
/// </summary> | |
/// <param name="entity">The entity.</param> | |
public void AddNode(Entity entity) | |
{ | |
AddNode(new SessionGraphNode(Graph, entity)); | |
} | |
/// <summary> | |
/// Removes a session graph node from this node. | |
/// </summary> | |
/// <param name="sessionGraphNode">The session graph node.</param> | |
/// <returns>Returns true when the session graph node was removed.</returns> | |
public bool RemoveNode(SessionGraphNode sessionGraphNode) | |
{ | |
if (_nodes.Remove(sessionGraphNode)) | |
{ | |
Graph.RemoveEntity(sessionGraphNode.Entity); | |
return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Removes a session graph node from this node. | |
/// </summary> | |
/// <param name="entity">The entity.</param> | |
/// <returns>Returns true when the session graph node was removed.</returns> | |
public bool RemoveNode(Entity entity) | |
{ | |
var sessionGraphNode = _nodes.FirstOrDefault(x => x.Entity == entity); | |
if (sessionGraphNode == null) | |
return false; | |
return RemoveNode(sessionGraphNode); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment