| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NumberNeighborIterator |
|
| 1.4;1,4 |
| 1 | /* | |
| 2 | This file is part of Combean. | |
| 3 | ||
| 4 | Combean is free software; you can redistribute it and/or modify | |
| 5 | it under the terms of the GNU General Public License as published by | |
| 6 | the Free Software Foundation; either version 2 of the License, or | |
| 7 | (at your option) any later version. | |
| 8 | ||
| 9 | Combean is distributed in the hope that it will be useful, | |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | GNU General Public License for more details. | |
| 13 | ||
| 14 | You should have received a copy of the GNU General Public License | |
| 15 | along with Combean; if not, write to the Free Software | |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 17 | */ | |
| 18 | /* | |
| 19 | * Created on 06.01.2005 | |
| 20 | * | |
| 21 | */ | |
| 22 | package net.sourceforge.combean.samples.simplegraphs; | |
| 23 | ||
| 24 | import net.sourceforge.combean.graph.NodePairEdge; | |
| 25 | import net.sourceforge.combean.graph.NumberNode; | |
| 26 | import net.sourceforge.combean.interfaces.graph.Edge; | |
| 27 | import net.sourceforge.combean.interfaces.graph.EdgeIterator; | |
| 28 | ||
| 29 | /** | |
| 30 | * An iterator through the neighbors of a NumberNode. | |
| 31 | * | |
| 32 | * @author schickin | |
| 33 | * | |
| 34 | */ | |
| 35 | public class NumberNeighborIterator implements EdgeIterator { | |
| 36 | private final NumberGraph graph; | |
| 37 | ||
| 38 | private final NumberNode startNode; | |
| 39 | ||
| 40 | private NumberNode currNode; | |
| 41 | ||
| 42 | /** | |
| 43 | * The current node which corresponds to the next neighbor in the iteration. | |
| 44 | * | |
| 45 | * @return Returns the currNode. | |
| 46 | */ | |
| 47 | protected final NumberNode getCurrNode() { | |
| 48 | 1206 | return this.currNode; |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * Set the next neighbor node to be returned in the iteration. | |
| 53 | * | |
| 54 | * @param currNode The currNode to set. | |
| 55 | */ | |
| 56 | protected final void setCurrNode(NumberNode currNode) { | |
| 57 | 1128 | this.currNode = currNode; |
| 58 | 1128 | } |
| 59 | /** | |
| 60 | * Construct an iterator for the neighborhood of startNode in the given graph. | |
| 61 | * | |
| 62 | * @param graph the graph where the nodes reside | |
| 63 | * @param startNode the node where the iteration starts | |
| 64 | */ | |
| 65 | 522 | public NumberNeighborIterator(NumberGraph graph, NumberNode startNode) { |
| 66 | 522 | this.graph = graph; |
| 67 | 522 | this.startNode = startNode; |
| 68 | 522 | setCurrNode(null); |
| 69 | 522 | } |
| 70 | ||
| 71 | /** | |
| 72 | * @see net.sourceforge.combean.interfaces.graph.EdgeIterator#hasNext() | |
| 73 | */ | |
| 74 | public boolean hasNext() { | |
| 75 | 1155 | return this.graph.nextNode(this.startNode, this.currNode) >= 0; |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * @see net.sourceforge.combean.interfaces.graph.EdgeIterator#next() | |
| 80 | */ | |
| 81 | public Edge next() { | |
| 82 | 606 | int nextNodeNum = this.graph.nextNode(this.startNode, this.currNode); |
| 83 | 606 | setCurrNode(this.graph.convertNumToNode(nextNodeNum)); |
| 84 | 606 | if (getCurrNode() == null) { |
| 85 | 6 | return null; |
| 86 | } | |
| 87 | 600 | return new NodePairEdge(this.startNode, getCurrNode()); |
| 88 | } | |
| 89 | } |