| OLD | NEW |
| 1 | 1 |
| 2 /** | 2 /** |
| 3 * Instances of the class {@code NodeList} represent a list of AST nodes that ha
ve a common parent. | 3 * Instances of the class {@code NodeList} represent a list of AST nodes that ha
ve a common parent. |
| 4 */ | 4 */ |
| 5 class NodeList<E extends ASTNode> extends ListWrapper<E> { | 5 class NodeList<E extends ASTNode> extends Object with ListMixin<E> { |
| 6 /** |
| 7 * Create an empty list with the given owner. This is a convenience method tha
t allows the |
| 8 * compiler to determine the correct value of the type argument [E] without ne
eding to |
| 9 * explicitly specify it. |
| 10 * |
| 11 * @param owner the node that is the parent of each of the elements in the lis
t |
| 12 * @return the list that was created |
| 13 */ |
| 14 static NodeList create(ASTNode owner) => new NodeList(owner); |
| 15 |
| 6 /** | 16 /** |
| 7 * The node that is the parent of each of the elements in the list. | 17 * The node that is the parent of each of the elements in the list. |
| 8 */ | 18 */ |
| 9 ASTNode owner; | 19 ASTNode owner; |
| 20 |
| 10 /** | 21 /** |
| 11 * The elements of the list. | 22 * The elements contained in the list. |
| 12 */ | 23 */ |
| 13 List<E> elements = new List<E>(); | 24 List<ASTNode> _elements = ASTNode.EMPTY_ARRAY; |
| 25 |
| 14 /** | 26 /** |
| 15 * Initialize a newly created list of nodes to be empty. | 27 * Initialize a newly created list of nodes to be empty. |
| 28 * |
| 16 * @param owner the node that is the parent of each of the elements in the lis
t | 29 * @param owner the node that is the parent of each of the elements in the lis
t |
| 17 */ | 30 */ |
| 18 NodeList(ASTNode this.owner); | 31 NodeList(this.owner); |
| 32 |
| 19 /** | 33 /** |
| 20 * Use the given visitor to visit each of the nodes in this list. | 34 * Use the given visitor to visit each of the nodes in this list. |
| 35 * |
| 21 * @param visitor the visitor to be used to visit the elements of this list | 36 * @param visitor the visitor to be used to visit the elements of this list |
| 22 */ | 37 */ |
| 23 accept(ASTVisitor visitor) { | 38 accept(ASTVisitor visitor) { |
| 24 for (E element in elements) { | 39 for (ASTNode element in _elements) { |
| 25 element.accept(visitor); | 40 element.accept(visitor); |
| 26 } | 41 } |
| 27 } | 42 } |
| 28 void add(E node) { | 43 void add(E node) { |
| 44 insert(length, node); |
| 45 } |
| 46 void insert(int index, E node) { |
| 47 int length = _elements.length; |
| 48 if (index < 0 || index > length) { |
| 49 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
| 50 } |
| 29 owner.becomeParentOf(node); | 51 owner.becomeParentOf(node); |
| 30 elements.add(node); | 52 if (length == 0) { |
| 53 _elements = <ASTNode> [node]; |
| 54 } else { |
| 55 List<ASTNode> newElements = new List<ASTNode>(length + 1); |
| 56 JavaSystem.arraycopy(_elements, 0, newElements, 0, index); |
| 57 newElements[index] = node; |
| 58 JavaSystem.arraycopy(_elements, index, newElements, index + 1, length - in
dex); |
| 59 _elements = newElements; |
| 60 } |
| 31 } | 61 } |
| 32 bool addAll(Iterable<E> nodes) { | 62 bool addAll(Iterable<E> nodes) { |
| 33 if (nodes != null) { | 63 if (nodes != null && !nodes.isEmpty) { |
| 64 int oldCount = _elements.length; |
| 65 int newCount = nodes.length; |
| 66 List<ASTNode> newElements = new List<ASTNode>(oldCount + newCount); |
| 67 JavaSystem.arraycopy(_elements, 0, newElements, 0, oldCount); |
| 68 int index = oldCount; |
| 34 for (E node in nodes) { | 69 for (E node in nodes) { |
| 35 add(node); | 70 owner.becomeParentOf(node); |
| 71 newElements[index++] = node; |
| 36 } | 72 } |
| 73 _elements = newElements; |
| 37 return true; | 74 return true; |
| 38 } | 75 } |
| 39 return false; | 76 return false; |
| 40 } | 77 } |
| 78 E operator[](int index) { |
| 79 if (index < 0 || index >= _elements.length) { |
| 80 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
| 81 } |
| 82 return _elements[index] as E; |
| 83 } |
| 84 |
| 41 /** | 85 /** |
| 42 * Return the first token included in this node's source range. | 86 * Return the first token included in this node's source range. |
| 87 * |
| 43 * @return the first token included in this node's source range | 88 * @return the first token included in this node's source range |
| 44 */ | 89 */ |
| 45 Token get beginToken { | 90 Token get beginToken { |
| 46 if (elements.isEmpty) { | 91 if (_elements.length == 0) { |
| 47 return null; | 92 return null; |
| 48 } | 93 } |
| 49 return elements[0].beginToken; | 94 return _elements[0].beginToken; |
| 50 } | 95 } |
| 96 |
| 51 /** | 97 /** |
| 52 * Return the last token included in this node list's source range. | 98 * Return the last token included in this node list's source range. |
| 99 * |
| 53 * @return the last token included in this node list's source range | 100 * @return the last token included in this node list's source range |
| 54 */ | 101 */ |
| 55 Token get endToken { | 102 Token get endToken { |
| 56 if (elements.isEmpty) { | 103 if (_elements.length == 0) { |
| 57 return null; | 104 return null; |
| 58 } | 105 } |
| 59 return elements[elements.length - 1].endToken; | 106 return _elements[_elements.length - 1].endToken; |
| 60 } | 107 } |
| 61 /** | 108 E removeAt(int index) { |
| 62 * Return the node that is the parent of each of the elements in the list. | 109 if (index < 0 || index >= _elements.length) { |
| 63 * @return the node that is the parent of each of the elements in the list | 110 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
| 64 */ | 111 } |
| 65 ASTNode getOwner() { | 112 E removedNode = _elements[index] as E; |
| 66 return owner; | 113 int length = _elements.length; |
| 114 if (length == 1) { |
| 115 _elements = ASTNode.EMPTY_ARRAY; |
| 116 return removedNode; |
| 117 } |
| 118 List<ASTNode> newElements = new List<ASTNode>(length - 1); |
| 119 JavaSystem.arraycopy(_elements, 0, newElements, 0, index); |
| 120 JavaSystem.arraycopy(_elements, index + 1, newElements, index, length - inde
x - 1); |
| 121 _elements = newElements; |
| 122 return removedNode; |
| 67 } | 123 } |
| 124 void operator[]=(int index, E node) { |
| 125 if (index < 0 || index >= _elements.length) { |
| 126 throw new RangeError("Index: ${index}, Size: ${_elements.length}"); |
| 127 } |
| 128 _elements[index] as E; |
| 129 owner.becomeParentOf(node); |
| 130 _elements[index] = node; |
| 131 } |
| 132 int get length => _elements.length; |
| 68 } | 133 } |
| OLD | NEW |