Coverage Report - edu.uci.ics.jung.graph.AbstractGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractGraph
32%
27/84
28%
15/52
3
 
 1  
 /*
 2  
  * Created on Apr 2, 2006
 3  
  *
 4  
  * Copyright (c) 2006, 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;
 13  
 
 14  
 import java.io.Serializable;
 15  
 import java.util.ArrayList;
 16  
 import java.util.Collection;
 17  
 import java.util.Collections;
 18  
 
 19  
 import edu.uci.ics.jung.graph.util.EdgeType;
 20  
 import edu.uci.ics.jung.graph.util.Pair;
 21  
 
 22  
 /**
 23  
  * Abstract implementation of the <code>Graph</code> interface.  
 24  
  * Designed to simplify implementation of new graph classes.
 25  
  * 
 26  
  * @author Joshua O'Madadhain
 27  
  */
 28  
 @SuppressWarnings("serial")
 29  356
 public abstract class AbstractGraph<V, E> implements Graph<V,E>, Serializable 
 30  
 {
 31  
         public boolean addEdge(E edge, Collection<? extends V> vertices) 
 32  
         {
 33  300
                 return addEdge(edge, vertices, this.getDefaultEdgeType());
 34  
         }
 35  
 
 36  
         @SuppressWarnings("unchecked")
 37  
         public boolean addEdge(E edge, Collection<? extends V> vertices, EdgeType edgeType) {
 38  300
             if (vertices == null)
 39  20
                 throw new IllegalArgumentException("'vertices' parameter must not be null");
 40  280
             if (vertices.size() == 2)
 41  80
                 return addEdge(edge, 
 42  
                                            vertices instanceof Pair ? (Pair<V>)vertices : new Pair<V>(vertices), 
 43  
                                            edgeType);
 44  200
         else if (vertices.size() == 1)
 45  
         {
 46  0
             V vertex = vertices.iterator().next();
 47  0
             return addEdge(edge, new Pair<V>(vertex, vertex), edgeType);
 48  
         }
 49  
         else
 50  200
             throw new IllegalArgumentException("Graph objects connect 1 or 2 vertices; vertices arg has " + vertices.size());
 51  
         }
 52  
         
 53  
         public boolean addEdge(E e, V v1, V v2)
 54  
         {
 55  1830
                 return addEdge(e, v1, v2, this.getDefaultEdgeType());
 56  
         }
 57  
 
 58  
         public boolean addEdge(E e, V v1, V v2, EdgeType edge_type)
 59  
         {
 60  2264
                 return addEdge(e, new Pair<V>(v1, v2), edge_type);
 61  
         }
 62  
         
 63  
         /**
 64  
          * Adds {@code edge} to this graph with the specified {@code endpoints},
 65  
          * with the default edge type.
 66  
          * @return {@code} true iff the graph was modified as a result of this call
 67  
          */
 68  
         public boolean addEdge(E edge, Pair<? extends V> endpoints)
 69  
         {
 70  0
                 return addEdge(edge, endpoints, this.getDefaultEdgeType());
 71  
         }
 72  
         
 73  
     /**
 74  
      * Adds {@code edge} to this graph with the specified {@code endpoints}
 75  
      * and {@code EdgeType}.
 76  
      * @return {@code} true iff the graph was modified as a result of this call
 77  
      */
 78  
         public abstract boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType);
 79  
 
 80  
     protected Pair<V> getValidatedEndpoints(E edge, Pair<? extends V> endpoints)
 81  
     {
 82  2488
         if (edge == null)
 83  0
             throw new IllegalArgumentException("input edge may not be null");
 84  
         
 85  2488
         if (endpoints == null)
 86  0
             throw new IllegalArgumentException("endpoints may not be null");
 87  
         
 88  2488
         Pair<V> new_endpoints = new Pair<V>(endpoints.getFirst(), endpoints.getSecond());
 89  2488
         if (containsEdge(edge))
 90  
         {
 91  60
             Pair<V> existing_endpoints = getEndpoints(edge);
 92  60
             if (!existing_endpoints.equals(new_endpoints)) {
 93  20
                 throw new IllegalArgumentException("edge " + edge + 
 94  
                         " already exists in this graph with endpoints " + existing_endpoints + 
 95  
                         " and cannot be added with endpoints " + endpoints);
 96  
             } else {
 97  40
                 return null;
 98  
             }
 99  
         }
 100  2428
         return new_endpoints;
 101  
     }
 102  
     
 103  
     public int inDegree(V vertex)
 104  
     {
 105  0
         return this.getInEdges(vertex).size();
 106  
     }
 107  
 
 108  
     public int outDegree(V vertex)
 109  
     {
 110  0
         return this.getOutEdges(vertex).size();
 111  
     }
 112  
 
 113  
     public boolean isPredecessor(V v1, V v2)
 114  
     {
 115  0
         return this.getPredecessors(v1).contains(v2);
 116  
     }
 117  
 
 118  
     public boolean isSuccessor(V v1, V v2)
 119  
     {
 120  0
         return this.getSuccessors(v1).contains(v2);
 121  
     }
 122  
 
 123  
     public int getPredecessorCount(V vertex)
 124  
     {
 125  0
         return this.getPredecessors(vertex).size();
 126  
     }
 127  
 
 128  
     public int getSuccessorCount(V vertex)
 129  
     {
 130  12
         return this.getSuccessors(vertex).size();
 131  
     }
 132  
 
 133  
     public boolean isNeighbor(V v1, V v2)
 134  
     {
 135  0
         if (!containsVertex(v1) || !containsVertex(v2))
 136  0
             throw new IllegalArgumentException("At least one of these not in this graph: " + v1 + ", " + v2);
 137  0
         return this.getNeighbors(v1).contains(v2);
 138  
     }
 139  
 
 140  
     public boolean isIncident(V vertex, E edge)
 141  
     {
 142  0
         if (!containsVertex(vertex) || !containsEdge(edge))
 143  0
             throw new IllegalArgumentException("At least one of these not in this graph: " + vertex + ", " + edge);
 144  0
         return this.getIncidentEdges(vertex).contains(edge);
 145  
     }
 146  
 
 147  
     public int getNeighborCount(V vertex)
 148  
     {
 149  0
         if (!containsVertex(vertex))
 150  0
             throw new IllegalArgumentException(vertex + " is not a vertex in this graph");
 151  0
         return this.getNeighbors(vertex).size();
 152  
     }
 153  
 
 154  
     public int degree(V vertex)
 155  
     {
 156  0
         if (!containsVertex(vertex))
 157  0
             throw new IllegalArgumentException(vertex + " is not a vertex in this graph");
 158  0
         return this.getIncidentEdges(vertex).size();
 159  
     }
 160  
 
 161  
     public int getIncidentCount(E edge)
 162  
     {
 163  0
         Pair<V> incident = this.getEndpoints(edge);
 164  0
         if (incident == null)
 165  0
             return 0;
 166  0
         if (incident.getFirst() == incident.getSecond())
 167  0
             return 1;
 168  
         else
 169  0
             return 2;
 170  
     }
 171  
     
 172  
     public V getOpposite(V vertex, E edge)
 173  
     {
 174  106
         Pair<V> incident = this.getEndpoints(edge); 
 175  106
         V first = incident.getFirst();
 176  106
         V second = incident.getSecond();
 177  106
         if (vertex.equals(first))
 178  70
             return second;
 179  36
         else if (vertex.equals(second))
 180  36
             return first;
 181  
         else 
 182  0
             throw new IllegalArgumentException(vertex + " is not incident to " + edge + " in this graph");
 183  
     }
 184  
 
 185  
     public E findEdge(V v1, V v2)
 186  
     {
 187  0
         for (E e : getOutEdges(v1))
 188  
         {
 189  0
             if (getOpposite(v1, e).equals(v2))
 190  0
                 return e;
 191  
         }
 192  0
         return null;
 193  
     }
 194  
     
 195  
     public Collection<E> findEdgeSet(V v1, V v2)
 196  
     {
 197  0
         if (!getVertices().contains(v1))
 198  0
             throw new IllegalArgumentException(v1 + " is not an element of this graph");
 199  
         
 200  0
         if (!getVertices().contains(v2))
 201  0
             throw new IllegalArgumentException(v2 + " is not an element of this graph");
 202  
         
 203  0
         Collection<E> edges = new ArrayList<E>();
 204  0
         for (E e : getOutEdges(v1))
 205  
         {
 206  0
             if (getOpposite(v1, e).equals(v2))
 207  0
                 edges.add(e);
 208  
         }
 209  0
         return Collections.unmodifiableCollection(edges);
 210  
     }
 211  
     
 212  
     public Collection<V> getIncidentVertices(E edge)
 213  
     {
 214  0
         Pair<V> endpoints = this.getEndpoints(edge);
 215  0
         Collection<V> incident = new ArrayList<V>();
 216  0
         incident.add(endpoints.getFirst());
 217  0
         incident.add(endpoints.getSecond());
 218  
         
 219  0
         return Collections.unmodifiableCollection(incident);
 220  
     }
 221  
     
 222  
     @Override
 223  
     public String toString() {
 224  0
             StringBuffer sb = new StringBuffer("Vertices:");
 225  0
             for(V v : getVertices()) {
 226  0
                     sb.append(v+",");
 227  
             }
 228  0
             sb.setLength(sb.length()-1);
 229  0
             sb.append("\nEdges:");
 230  0
             for(E e : getEdges()) {
 231  0
                     Pair<V> ep = getEndpoints(e);
 232  0
                     sb.append(e+"["+ep.getFirst()+","+ep.getSecond()+"] ");
 233  0
             }
 234  0
         return sb.toString();
 235  
     }
 236  
 
 237  
 }