Notification changes
[pithos-ms-client] / trunk / Pithos.Client.WPF / Utils / Node.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using Caliburn.Micro;
4
5 namespace Pithos.Client.WPF.Utils
6 {
7     public class Node<T> : IEnumerable<Node<T>>
8     {
9         public string Path { get; set; }
10
11         public T Data { get; set; }
12
13         private List<Node<T>> _children = new List<Node<T>>();
14         public List<Node<T>> Children
15         {
16             get { return _children; }
17             set { _children = value; }
18         }
19
20         public IEnumerator<Node<T>> GetEnumerator()
21         {
22             yield return this;
23             foreach (var children in _children)
24                 foreach (var info in children)
25                 {
26                     yield return info;
27                 }
28         }
29
30         IEnumerator IEnumerable.GetEnumerator()
31         {
32             return GetEnumerator();
33         }
34
35         public override int GetHashCode()
36         {
37             return Path.GetHashCode();
38         }
39
40         public override bool Equals(object obj)
41         {
42             if (!(obj is Node<T>))
43                 return false;
44             var other = (Node<T>) obj;
45             if (Path != other.Path)
46                 return false;
47             if (Data==null ^ other.Data==null)
48                 return false;
49             
50             if (Data!=null && !Data.Equals(other.Data))
51                 return false;
52             var thisEnum=GetEnumerator();
53             var otherEnum = other.GetEnumerator();
54             //Skipt the first item, it is the current node itself
55             thisEnum.MoveNext();
56             otherEnum.MoveNext();
57             while(true)
58             {
59                 var thisMove=thisEnum.MoveNext();
60                 var otherMove=otherEnum.MoveNext();
61
62                 if (thisMove ^ otherMove)
63                     return false;
64                 if (!thisMove)
65                     return true;
66                 
67                 if (!thisEnum.Current.Equals(otherEnum.Current))
68                     return false;
69             }
70         }
71     }
72 }