public virtual NodeHistoryState Append(HistoryNode current) { if (current == null) { throw new ArgumentNullException(nameof(current)); } var historyNodes = BackNodes.Append(Current).ToArray(); var backNodes = historyNodes.TakeLast(_maxHistoryNodes); return(new NodeHistoryState(current, _maxHistoryNodes, backNodes, forwardNodes: null)); }
public NodeHistoryState(HistoryNode current, int maxHistoryNodes, IEnumerable <HistoryNode> backNodes = null, IEnumerable <HistoryNode> forwardNodes = null) { if (maxHistoryNodes < 1) { throw new ArgumentException($"{nameof(maxHistoryNodes)} must be positive", nameof(maxHistoryNodes)); } _maxHistoryNodes = maxHistoryNodes; Current = current ?? throw new ArgumentNullException(nameof(current)); BackNodes = backNodes?.ToArray() ?? Enumerable.Empty <HistoryNode>(); ForwardNodes = forwardNodes?.ToArray() ?? Enumerable.Empty <HistoryNode>(); }
public virtual NodeHistoryState GoBackTo(HistoryNode historyNode) { const int maxBackAttempts = 1000; NodeHistoryState result = this; for (int i = 0; i < maxBackAttempts; i++) { result = result.GoBack(); if (historyNode.Equals(result.Current)) { return(result); } } throw new Exception("the history node was not found"); }