| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | * Created on Apr 27, 2005 | |
| 3 | * | |
| 4 | * Copyright (c) 2005, the JUNG Project and the Regents of the University | |
| 5 | * of California | |
| 6 | * All rights reserved. | |
| 7 | * | |
| 8 | * This software is open-source under the BSD license; see either | |
| 9 | * "license.txt" or | |
| 10 | * http://jung.sourceforge.net/license.txt for a description. | |
| 11 | */ | |
| 12 | package edu.uci.ics.jung.graph.impl; | |
| 13 | ||
| 14 | import java.util.Collection; | |
| 15 | import java.util.Collections; | |
| 16 | import java.util.HashSet; | |
| 17 | import java.util.Iterator; | |
| 18 | import java.util.Set; | |
| 19 | ||
| 20 | import edu.uci.ics.jung.graph.ArchetypeEdge; | |
| 21 | import edu.uci.ics.jung.graph.ArchetypeGraph; | |
| 22 | import edu.uci.ics.jung.graph.Hyperedge; | |
| 23 | import edu.uci.ics.jung.graph.Hypergraph; | |
| 24 | import edu.uci.ics.jung.graph.Hypervertex; | |
| 25 | ||
| 26 | /** | |
| 27 | * This class provides a skeletal implementation of the <code>Hyperedge</code> | |
| 28 | * interface to minimize the effort required to implement this interface. | |
| 29 | * <P> | |
| 30 | * This class extends <code>UserData</code>, which provides storage and | |
| 31 | * retrieval mechanisms for user-defined data for each edge instance. | |
| 32 | * This allows users to attach data to edges without having to extend | |
| 33 | * this class. | |
| 34 | * | |
| 35 | * @author Joshua O'Madadhain | |
| 36 | * | |
| 37 | * @see SetHypergraph | |
| 38 | * @see AbstractHypervertex | |
| 39 | */ | |
| 40 | public abstract class AbstractHyperedge extends AbstractArchetypeEdge implements | |
| 41 | Hyperedge | |
| 42 | { | |
| 43 | /** | |
| 44 | * The next edge ID. | |
| 45 | */ | |
| 46 | 0 | private static int nextGlobalEdgeID = 0; |
| 47 | ||
| 48 | public AbstractHyperedge() | |
| 49 | { | |
| 50 | 0 | super(); |
| 51 | 0 | this.id = nextGlobalEdgeID++; |
| 52 | 0 | initialize(); |
| 53 | 0 | } |
| 54 | ||
| 55 | protected void initialize() | |
| 56 | { | |
| 57 | 0 | super.initialize(); |
| 58 | 0 | } |
| 59 | ||
| 60 | /** | |
| 61 | * Connects <code>hv1</code> to this edge and vice versa. If <code>hv1</code> is already | |
| 62 | * incident to this edge, returns <code>false</code>; otherwise, returns <code>true</code>. | |
| 63 | * Throws <code>IllegalArgumentException</code> if this edge is an | |
| 64 | * orphan, or if <code>hv1</code> is either an orphan or part of a different graph than this edge. | |
| 65 | * | |
| 66 | * @see edu.uci.ics.jung.graph.Hyperedge#connectVertex(edu.uci.ics.jung.graph.Hypervertex) | |
| 67 | */ | |
| 68 | public boolean connectVertex(Hypervertex hv1) | |
| 69 | { | |
| 70 | 0 | ArchetypeGraph g = this.getGraph(); |
| 71 | 0 | if (g == null) |
| 72 | 0 | throw new IllegalArgumentException("Orphaned hyperedges may not be " + |
| 73 | "connected to (or disconnected from) vertices"); | |
| 74 | ||
| 75 | 0 | if (g != hv1.getGraph()) |
| 76 | 0 | throw new IllegalArgumentException("Hypervertex " + hv1 + " is either orphaned" + |
| 77 | "or an element of a graph other than " + g); | |
| 78 | ||
| 79 | 0 | if (hv1.isIncident(this)) |
| 80 | 0 | return false; |
| 81 | ||
| 82 | 0 | if (hv1 instanceof AbstractHypervertex) |
| 83 | { | |
| 84 | 0 | AbstractHypervertex av = (AbstractHypervertex)hv1; |
| 85 | 0 | av.getEdges_internal().add(this); |
| 86 | } | |
| 87 | 0 | getVertices_internal().add(hv1); |
| 88 | ||
| 89 | 0 | return true; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Disconnects <code>hv1</code> from this edge and vice versa. If <code>hv1</code> is not | |
| 94 | * incident to this edge, returns <code>false</code>; otherwise, returns <code>true</code>. | |
| 95 | * | |
| 96 | * @see edu.uci.ics.jung.graph.Hyperedge#disconnectVertex(edu.uci.ics.jung.graph.Hypervertex) | |
| 97 | */ | |
| 98 | public boolean disconnectVertex(Hypervertex hv1) | |
| 99 | { | |
| 100 | 0 | ArchetypeGraph g = this.getGraph(); |
| 101 | 0 | if (g == null) |
| 102 | 0 | throw new IllegalArgumentException("Orphaned hyperedges may not be " + |
| 103 | "connected to (or disconnected from) vertices"); | |
| 104 | ||
| 105 | 0 | if (g != hv1.getGraph()) |
| 106 | 0 | throw new IllegalArgumentException("Hypervertex " + hv1 + " is either orphaned" + |
| 107 | "or an element of a graph other than " + g); | |
| 108 | ||
| 109 | 0 | if (!hv1.isIncident(this)) |
| 110 | 0 | return false; |
| 111 | ||
| 112 | 0 | if (hv1 instanceof AbstractHypervertex) |
| 113 | { | |
| 114 | 0 | AbstractHypervertex av = (AbstractHypervertex)hv1; |
| 115 | 0 | av.getEdges_internal().remove(this); |
| 116 | } | |
| 117 | 0 | getVertices_internal().remove(hv1); |
| 118 | ||
| 119 | 0 | return true; |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Creates a copy of this edge in the specified graph <code>newGraph</code>, | |
| 124 | * and copies this edge's user data to the new edge. Connects this | |
| 125 | * | |
| 126 | * @see edu.uci.ics.jung.graph.ArchetypeEdge#copy(edu.uci.ics.jung.graph.ArchetypeGraph) | |
| 127 | */ | |
| 128 | public ArchetypeEdge copy(ArchetypeGraph newGraph) | |
| 129 | { | |
| 130 | 0 | Hyperedge e = (Hyperedge)super.copy(newGraph); |
| 131 | 0 | ((Hypergraph)newGraph).addEdge(e); |
| 132 | ||
| 133 | 0 | for (Iterator iter = getVertices_internal().iterator(); iter.hasNext(); ) |
| 134 | { | |
| 135 | 0 | Hypervertex v = (Hypervertex)iter.next(); |
| 136 | 0 | e.connectVertex((Hypervertex)v.getEqualVertex(newGraph)); |
| 137 | } | |
| 138 | 0 | return e; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * @see edu.uci.ics.jung.graph.ArchetypeEdge#getIncidentVertices() | |
| 143 | */ | |
| 144 | public Set getIncidentVertices() | |
| 145 | { | |
| 146 | 0 | return Collections.unmodifiableSet(new HashSet(getVertices_internal())); |
| 147 | } | |
| 148 | ||
| 149 | ||
| 150 | /** | |
| 151 | * Returns a human-readable representation of this edge. | |
| 152 | * | |
| 153 | * @see java.lang.Object#toString() | |
| 154 | */ | |
| 155 | public String toString() | |
| 156 | { | |
| 157 | 0 | String label = "HE" + id + "("; |
| 158 | 0 | for (Iterator iter = getVertices_internal().iterator(); iter.hasNext(); ) |
| 159 | { | |
| 160 | 0 | Hypervertex v = (Hypervertex)iter.next(); |
| 161 | 0 | label += v.toString(); |
| 162 | 0 | if (iter.hasNext()) |
| 163 | 0 | label += ","; |
| 164 | } | |
| 165 | 0 | return label + ")"; |
| 166 | } | |
| 167 | ||
| 168 | protected abstract Collection getVertices_internal(); | |
| 169 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |