Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.IO;
00005 using N2.Engine;
00006
00007 namespace N2.Collections
00008 {
00014 public class HierarchyNode<T>
00015 {
00016 private T current;
00017 IList<HierarchyNode<T>> children = new List<HierarchyNode<T>>();
00018 HierarchyNode<T> parent;
00019
00022 public HierarchyNode(T current)
00023 {
00024 this.current = current;
00025 }
00026
00028 public T Current
00029 {
00030 get { return current; }
00031 set { current = value; }
00032 }
00033
00035 public HierarchyNode<T> Parent
00036 {
00037 get { return parent; }
00038 set { parent = value; }
00039 }
00040
00042 public IList<HierarchyNode<T>> Children
00043 {
00044 get { return children; }
00045 }
00046
00047 public string ToString(Func<T, string> begin, Func<T, string> indent, Func<T, string> outdent, Func<T, string> end)
00048 {
00049 using (var sw = new StringWriter())
00050 {
00051 Write(sw, begin, indent, outdent, end);
00052 return sw.ToString();
00053 }
00054 }
00055
00056 public void Write(TextWriter writer, Func<T, string> begin, Func<T, string> indent, Func<T, string> outdent, Func<T, string> end)
00057 {
00058 writer.Write(begin(Current));
00059 if (Children.Count > 0)
00060 {
00061 writer.Write(indent(Current));
00062 foreach (var child in Children)
00063 {
00064 child.Write(writer, begin, indent, outdent, end);
00065 }
00066 writer.Write(outdent(Current));
00067 }
00068 writer.Write(end(Current));
00069 }
00070
00071 #region Equals & GetHashCode
00072 public override bool Equals(object obj)
00073 {
00074 if (Current == null)
00075 return false;
00076 var other = obj as HierarchyNode<T>;
00077 if (other == null)
00078 return false;
00079
00080 return Current.Equals(other.Current);
00081 }
00082 public override int GetHashCode()
00083 {
00084 if (Current == null)
00085 return 0.GetHashCode();
00086 return Current.GetHashCode();
00087 }
00088 #endregion
00089 }
00090 }