Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
KeyIterator |
|
| 1.9;1.9 | ||||
KeyIterator$Locator |
|
| 1.9;1.9 |
1 | /** | |
2 | * Distribution License: | |
3 | * JSword is free software; you can redistribute it and/or modify it under | |
4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
8 | * See the GNU Lesser General Public License for more details. | |
9 | * | |
10 | * The License is available on the internet at: | |
11 | * http://www.gnu.org/copyleft/lgpl.html | |
12 | * or by writing to: | |
13 | * Free Software Foundation, Inc. | |
14 | * 59 Temple Place - Suite 330 | |
15 | * Boston, MA 02111-1307, USA | |
16 | * | |
17 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.jsword.passage; | |
21 | ||
22 | import java.util.Iterator; | |
23 | import java.util.NoSuchElementException; | |
24 | import java.util.Stack; | |
25 | ||
26 | /** | |
27 | * This KeyIterator performs a depth first iteration over the subkeys in the | |
28 | * key. | |
29 | * | |
30 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
31 | * @author DM Smith | |
32 | */ | |
33 | 0 | public class KeyIterator implements Iterator<Key> { |
34 | 0 | public KeyIterator(Key key) { |
35 | 0 | stack = new Stack<Locator>(); |
36 | 0 | stack.push(new Locator(key)); |
37 | 0 | } |
38 | ||
39 | protected void prepare() { | |
40 | // If there is nothing on the stack we have nothing to do. | |
41 | 0 | if (stack.size() == 0) { |
42 | 0 | return; |
43 | } | |
44 | ||
45 | // Check to see if there are more children to process | |
46 | 0 | Locator peek = stack.peek(); |
47 | ||
48 | 0 | if (peek.getParent().getChildCount() > peek.getPosition()) { |
49 | 0 | return; |
50 | } | |
51 | ||
52 | // There are no more so we are done with this Locator. | |
53 | 0 | stack.pop(); |
54 | ||
55 | // Try the next | |
56 | 0 | prepare(); |
57 | 0 | } |
58 | ||
59 | public boolean hasNext() { | |
60 | 0 | prepare(); |
61 | 0 | return stack.size() != 0; |
62 | } | |
63 | ||
64 | public Key next() { | |
65 | 0 | if (!hasNext()) { |
66 | 0 | throw new NoSuchElementException(); |
67 | } | |
68 | ||
69 | 0 | Locator peek = stack.peek(); |
70 | ||
71 | // Determine which child in the list of children to consider | |
72 | 0 | int childNum = peek.getPosition(); |
73 | ||
74 | // Advance to the next potential child | |
75 | 0 | peek.setPosition(childNum + 1); |
76 | ||
77 | // If we have exhausted all the children, | |
78 | // then return the parent key | |
79 | 0 | if (childNum == -1) { |
80 | 0 | return peek.getParent(); |
81 | } | |
82 | ||
83 | 0 | stack.push(new Locator(peek.getParent().get(childNum))); |
84 | ||
85 | 0 | return next(); |
86 | } | |
87 | ||
88 | public void remove() { | |
89 | 0 | throw new UnsupportedOperationException(); |
90 | } | |
91 | ||
92 | /** | |
93 | * A helper class that remembers where we've been and where we are. | |
94 | */ | |
95 | public static class Locator { | |
96 | private Key parent; | |
97 | private int position; | |
98 | ||
99 | 0 | public Locator(Key parent) { |
100 | 0 | this.parent = parent; |
101 | 0 | this.position = -1; |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * @return the parent | |
106 | */ | |
107 | public Key getParent() { | |
108 | 0 | return parent; |
109 | } | |
110 | ||
111 | /** | |
112 | * @param parent | |
113 | * the parent to set | |
114 | */ | |
115 | public void setParent(Key parent) { | |
116 | 0 | this.parent = parent; |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * @return the position | |
121 | */ | |
122 | public int getPosition() { | |
123 | 0 | return position; |
124 | } | |
125 | ||
126 | /** | |
127 | * @param position | |
128 | * the position to set | |
129 | */ | |
130 | public void setPosition(int position) { | |
131 | 0 | this.position = position; |
132 | 0 | } |
133 | ||
134 | } | |
135 | ||
136 | private Stack<Locator> stack; | |
137 | } |