You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TreeIterator.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.IOException;
  46. import java.util.Iterator;
  47. /**
  48. * A tree iterator iterates over a tree and all its members recursing into
  49. * subtrees according to order.
  50. *
  51. * Default is to only visit leafs. An {@link Order} value can be supplied to
  52. * make the iteration include Tree nodes as well either before or after the
  53. * child nodes have been visited.
  54. */
  55. public class TreeIterator implements Iterator<TreeEntry> {
  56. private Tree tree;
  57. private int index;
  58. private TreeIterator sub;
  59. private Order order;
  60. private boolean visitTreeNodes;
  61. private boolean hasVisitedTree;
  62. /**
  63. * Traversal order
  64. */
  65. public enum Order {
  66. /**
  67. * Visit node first, then leaves
  68. */
  69. PREORDER,
  70. /**
  71. * Visit leaves first, then node
  72. */
  73. POSTORDER
  74. }
  75. /**
  76. * Construct a {@link TreeIterator} for visiting all non-tree nodes.
  77. *
  78. * @param start
  79. */
  80. public TreeIterator(Tree start) {
  81. this(start, Order.PREORDER, false);
  82. }
  83. /**
  84. * Construct a {@link TreeIterator} visiting all nodes in a tree in a given
  85. * order.
  86. *
  87. * @param start Root node
  88. * @param order {@link Order}
  89. */
  90. public TreeIterator(Tree start, Order order) {
  91. this(start, order, true);
  92. }
  93. /**
  94. * Construct a {@link TreeIterator}
  95. *
  96. * @param start First node to visit
  97. * @param order Visitation {@link Order}
  98. * @param visitTreeNode True to include tree node
  99. */
  100. private TreeIterator(Tree start, Order order, boolean visitTreeNode) {
  101. this.tree = start;
  102. this.visitTreeNodes = visitTreeNode;
  103. this.index = -1;
  104. this.order = order;
  105. if (!visitTreeNodes)
  106. this.hasVisitedTree = true;
  107. try {
  108. step();
  109. } catch (IOException e) {
  110. throw new Error(e);
  111. }
  112. }
  113. public TreeEntry next() {
  114. try {
  115. TreeEntry ret = nextTreeEntry();
  116. step();
  117. return ret;
  118. } catch (IOException e) {
  119. throw new Error(e);
  120. }
  121. }
  122. private TreeEntry nextTreeEntry() throws IOException {
  123. TreeEntry ret;
  124. if (sub != null)
  125. ret = sub.nextTreeEntry();
  126. else {
  127. if (index < 0 && order == Order.PREORDER) {
  128. return tree;
  129. }
  130. if (order == Order.POSTORDER && index == tree.memberCount()) {
  131. return tree;
  132. }
  133. ret = tree.members()[index];
  134. }
  135. return ret;
  136. }
  137. public boolean hasNext() {
  138. try {
  139. return hasNextTreeEntry();
  140. } catch (IOException e) {
  141. throw new Error(e);
  142. }
  143. }
  144. private boolean hasNextTreeEntry() throws IOException {
  145. if (tree == null)
  146. return false;
  147. return sub != null
  148. || index < tree.memberCount()
  149. || order == Order.POSTORDER && index == tree.memberCount();
  150. }
  151. private boolean step() throws IOException {
  152. if (tree == null)
  153. return false;
  154. if (sub != null) {
  155. if (sub.step())
  156. return true;
  157. sub = null;
  158. }
  159. if (index < 0 && !hasVisitedTree && order == Order.PREORDER) {
  160. hasVisitedTree = true;
  161. return true;
  162. }
  163. while (++index < tree.memberCount()) {
  164. TreeEntry e = tree.members()[index];
  165. if (e instanceof Tree) {
  166. sub = new TreeIterator((Tree) e, order, visitTreeNodes);
  167. if (sub.hasNextTreeEntry())
  168. return true;
  169. sub = null;
  170. continue;
  171. }
  172. return true;
  173. }
  174. if (index == tree.memberCount() && !hasVisitedTree
  175. && order == Order.POSTORDER) {
  176. hasVisitedTree = true;
  177. return true;
  178. }
  179. return false;
  180. }
  181. public void remove() {
  182. throw new IllegalStateException(
  183. "TreeIterator does not support remove()");
  184. }
  185. }