OLD | NEW |
1 class AbstractNode { | 1 class AbstractNode { |
2 | 2 |
3 // Nodes always have a 'depth' greater than their ancestors'. | 3 // AbstractNode represents a node in a tree. |
4 // There's no guarantee regarding depth between siblings. The depth | 4 // The AbstractNode protocol is as follows: |
5 // of a node is used to ensure that nodes are processed in depth | 5 // - When a subclass is changing the parent of a child, it should |
6 // order. The 'depth' of a child can be more than one greater than | 6 // call either parent.adoptChild(child) or parent.dropChild(child) |
7 // the 'depth' of the parent, because the 'depth' values are never | 7 // as appropriate. Subclasses should expose an API for |
8 // decreased: all that matters is that it's greater than the parent. | 8 // manipulating the tree if you want to (e.g. a setter for a |
9 // Consider a tree with a root node A, a child B, and a grandchild | 9 // 'child' property, or an 'add()' method to manipulate a list). |
10 // C. Initially, A will have 'depth' 0, B 'depth' 1, and C 'depth' | 10 // - You can see the current parent by querying 'parent'. |
11 // 2. If C is moved to be a child of A, sibling of B, then the | 11 // - You can see the current attachment state by querying |
12 // numbers won't change. C's 'depth' will still be 2. | 12 // 'attached'. The root of any tree that is to be considered |
| 13 // attached should be manually attached by calling 'attach()'. |
| 14 // Other than that, don't call 'attach()' or 'detach()'. This is |
| 15 // all managed automatically assuming you call the 'adoptChild()' |
| 16 // and 'dropChild()' methods appropriately. |
| 17 // - Subclasses that have children must override 'attach()' and |
| 18 // 'detach()' as described below. |
| 19 // - Nodes always have a 'depth' greater than their ancestors'. |
| 20 // There's no guarantee regarding depth between siblings. The |
| 21 // depth of a node is used to ensure that nodes are processed in |
| 22 // depth order. The 'depth' of a child can be more than one |
| 23 // greater than the 'depth' of the parent, because the 'depth' |
| 24 // values are never decreased: all that matters is that it's |
| 25 // greater than the parent. Consider a tree with a root node A, a |
| 26 // child B, and a grandchild C. Initially, A will have 'depth' 0, |
| 27 // B 'depth' 1, and C 'depth' 2. If C is moved to be a child of A, |
| 28 // sibling of B, then the numbers won't change. C's 'depth' will |
| 29 // still be 2. This is all managed automatically assuming you call |
| 30 // 'adoptChild()' and 'dropChild()' appropriately. |
13 | 31 |
14 int _depth = 0; | 32 int _depth = 0; |
15 int get depth => _depth; | 33 int get depth => _depth; |
16 void redepthChild(AbstractNode child) { // internal, do not call | 34 void redepthChild(AbstractNode child) { // internal, do not call |
17 assert(child._attached == _attached); | 35 assert(child._attached == _attached); |
18 if (child._depth <= _depth) { | 36 if (child._depth <= _depth) { |
19 child._depth = _depth + 1; | 37 child._depth = _depth + 1; |
20 child.redepthChildren(); | 38 child.redepthChildren(); |
21 } | 39 } |
22 } | 40 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 void dropChild(AbstractNode child) { // only for use by subclasses | 73 void dropChild(AbstractNode child) { // only for use by subclasses |
56 assert(child != null); | 74 assert(child != null); |
57 assert(child._parent == this); | 75 assert(child._parent == this); |
58 assert(child.attached == attached); | 76 assert(child.attached == attached); |
59 child._parent = null; | 77 child._parent = null; |
60 if (attached) | 78 if (attached) |
61 child.detach(); | 79 child.detach(); |
62 } | 80 } |
63 | 81 |
64 } | 82 } |
OLD | NEW |