| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NumberGraph |
|
| 1.5;1,5 |
| 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 | import net.sourceforge.combean.interfaces.graph.Graph; | |
| 29 | import net.sourceforge.combean.interfaces.graph.Node; | |
| 30 | import net.sourceforge.combean.interfaces.graph.NodeIterator; | |
| 31 | import net.sourceforge.combean.interfaces.graph.prop.GlobalNumberedNodesGraphProp; | |
| 32 | import net.sourceforge.combean.interfaces.graph.prop.OutgoingEdgeNeighborhoodGraphProp; | |
| 33 | import net.sourceforge.combean.util.except.IllegalParameterException; | |
| 34 | ||
| 35 | /** | |
| 36 | * This class represents a graph where every node is identified by a long-value. | |
| 37 | * | |
| 38 | * @author schickin | |
| 39 | * | |
| 40 | */ | |
| 41 | public abstract class NumberGraph | |
| 42 | implements Graph, | |
| 43 | OutgoingEdgeNeighborhoodGraphProp, | |
| 44 | GlobalNumberedNodesGraphProp { | |
| 45 | /** | |
| 46 | * The number of the first node in the graph. | |
| 47 | */ | |
| 48 | public static final int FIRSTNODE = 0; | |
| 49 | ||
| 50 | /** | |
| 51 | * The virtual node number if no such node exists. | |
| 52 | */ | |
| 53 | public static final int NOSUCHNODE = -1; | |
| 54 | ||
| 55 | /** | |
| 56 | * | |
| 57 | */ | |
| 58 | public NumberGraph() { | |
| 59 | 378 | super(); |
| 60 | 378 | } |
| 61 | ||
| 62 | /* (non-Javadoc) | |
| 63 | * @see net.sourceforge.combean.interfaces.graph.Graph#getEdgeClass() | |
| 64 | */ | |
| 65 | public Class getEdgeClass() { | |
| 66 | 0 | return NodePairEdge.class; |
| 67 | } | |
| 68 | ||
| 69 | /* (non-Javadoc) | |
| 70 | * @see net.sourceforge.combean.interfaces.graph.Graph#getNodeClass() | |
| 71 | */ | |
| 72 | public Class getNodeClass() { | |
| 73 | 0 | return NumberNode.class; |
| 74 | } | |
| 75 | /** | |
| 76 | * @see net.sourceforge.combean.interfaces.graph.prop.GlobalNodesGraphProp#getAllNodesIterator() | |
| 77 | */ | |
| 78 | public NodeIterator getAllNodesIterator() { | |
| 79 | 57 | return new NumberNodeIterator(FIRSTNODE, getNumNodes()-1); |
| 80 | } | |
| 81 | ||
| 82 | /* (non-Javadoc) | |
| 83 | * @see net.sourceforge.combean.interfaces.graph.GlobalIndexedNodesGraphProp#getNode(int) | |
| 84 | */ | |
| 85 | public Node getNode(int index) { | |
| 86 | 27 | if (index < 0 || index >= getNumNodes()) { |
| 87 | 6 | throw new IllegalParameterException( |
| 88 | "node index out of bounds. " + | |
| 89 | "must be in interval [0..number of nodes-1]"); | |
| 90 | } | |
| 91 | 21 | return new NumberNode(index); |
| 92 | } | |
| 93 | ||
| 94 | /* (non-Javadoc) | |
| 95 | * @see net.sourceforge.combean.interfaces.graph.prop.GlobalNumberedNodesGraphProp#getNodeNumber(net.sourceforge.combean.interfaces.graph.Node) | |
| 96 | */ | |
| 97 | public int getNodeNumber(Node v) { | |
| 98 | 2052 | return ((NumberNode) v).getNodeNum(); |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Create a node in the graph.given its number | |
| 103 | * | |
| 104 | * @param num the number of the node to be created | |
| 105 | * @return the node with the given number | |
| 106 | * or null if the node does not exist in the graph or num is NOSUCHNODE. | |
| 107 | */ | |
| 108 | public final NumberNode convertNumToNode(int num) { | |
| 109 | 618 | NumberNode result = new NumberNode(num); |
| 110 | 618 | if (!contains(result)) { |
| 111 | 15 | result = null; |
| 112 | } | |
| 113 | 618 | return result; |
| 114 | } | |
| 115 | ||
| 116 | public final EdgeIterator getIncidentEdges(Node v) { | |
| 117 | 522 | return new NumberNeighborIterator(this, (NumberNode)v); |
| 118 | } | |
| 119 | ||
| 120 | /* (non-Javadoc) | |
| 121 | * @see net.sourceforge.combean.interfaces.graph.OutgoingEdgeNeighborhoodGraphProp#getOutgoingEdges() | |
| 122 | */ | |
| 123 | public EdgeIterator getOutgoingEdges(Node v) { | |
| 124 | 87 | return getIncidentEdges(v); |
| 125 | } | |
| 126 | ||
| 127 | public final Node getFirstNode(Edge e) { | |
| 128 | 36 | NodePairEdge ep = (NodePairEdge) e; |
| 129 | 36 | return ep.getFirstNode(); |
| 130 | } | |
| 131 | ||
| 132 | public final Node getSecondNode(Edge e) { | |
| 133 | 12 | NodePairEdge ep = (NodePairEdge) e; |
| 134 | 12 | return ep.getSecondNode(); |
| 135 | } | |
| 136 | ||
| 137 | public final Node getOtherNode(Edge e, Node v) { | |
| 138 | 621 | NodePairEdge ep = (NodePairEdge) e; |
| 139 | 621 | if (ep.getFirstNode().equals(v)) { |
| 140 | 609 | return ep.getSecondNode(); |
| 141 | } | |
| 142 | 12 | return ep.getFirstNode(); |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Helps NumberNodeIterators to calculate the next node when iterating | |
| 147 | * through neighbors. | |
| 148 | * | |
| 149 | * @param startNode the start node of the NumberNeighborIterator | |
| 150 | * @param currNode the current node of the NumberNeighborIterator or null is this | |
| 151 | * is the first iteration | |
| 152 | * @return the next node of the NumberNeighborIterator or | |
| 153 | * NOSUCHNODE if no next node exists | |
| 154 | */ | |
| 155 | protected int nextNode(NumberNode startNode, NumberNode currNode) { | |
| 156 | 1761 | if (empty() || !contains(startNode)) { |
| 157 | 6 | return NOSUCHNODE; |
| 158 | } | |
| 159 | 1755 | int currNodeNum = 0; |
| 160 | 1755 | if (currNode != null) { |
| 161 | 789 | currNodeNum = currNode.getNodeNum()+1; |
| 162 | } | |
| 163 | 1755 | return calcNextNode(startNode.getNodeNum(), currNodeNum); |
| 164 | } | |
| 165 | ||
| 166 | /** | |
| 167 | * Template method for calculating neighbors. | |
| 168 | * | |
| 169 | * Calculates the smallest number >= minNodeNum of a node in the neighborhood of | |
| 170 | * the node with the number sourceNumNode or NOSUCHNODE if no next node exists. | |
| 171 | * It is guaranteed that the graph in non-empty and that the node number startNodeNum | |
| 172 | * is contained in the graph. | |
| 173 | * Note that minNodeNum may also be negative. | |
| 174 | * | |
| 175 | * @param sourceNodeNum | |
| 176 | * @param minNodeNum | |
| 177 | * @return the next node in the neighborhood of the given node | |
| 178 | * @see net.sourceforge.combean.samples.simplegraphs.NumberGraph#nextNode(NumberNode, NumberNode) | |
| 179 | */ | |
| 180 | protected abstract int calcNextNode(int sourceNodeNum, int minNodeNum); | |
| 181 | ||
| 182 | /** | |
| 183 | * Check whether a given node is contained in the graph. | |
| 184 | * | |
| 185 | * @param v the node to be checked | |
| 186 | * @return true if v is contained in the graph | |
| 187 | */ | |
| 188 | public final boolean contains(NumberNode v) { | |
| 189 | 2385 | return FIRSTNODE <= v.getNodeNum() && |
| 190 | v.getNodeNum() < getNumNodes(); | |
| 191 | } | |
| 192 | ||
| 193 | /** | |
| 194 | * Check if the graph is empty. | |
| 195 | * | |
| 196 | * @return true if the graph does not contain any node. | |
| 197 | */ | |
| 198 | public final boolean empty() { | |
| 199 | 1761 | return getNumNodes() == 0; |
| 200 | } | |
| 201 | } |