Coverage Report - edu.uci.ics.jung.graph.SortedSparseMultigraph
 
Classes in this File Line Coverage Branch Coverage Complexity
SortedSparseMultigraph
72%
16/22
66%
4/6
1.833
SortedSparseMultigraph$1
100%
2/2
N/A
1.833
 
 1  
 /*
 2  
  * Created on Oct 18, 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;
 13  
 
 14  
 import java.util.Comparator;
 15  
 import java.util.Map;
 16  
 import java.util.Set;
 17  
 import java.util.TreeMap;
 18  
 import java.util.TreeSet;
 19  
 
 20  
 import org.apache.commons.collections15.Factory;
 21  
 import org.apache.commons.collections15.comparators.ComparableComparator;
 22  
 
 23  
 import edu.uci.ics.jung.graph.util.Pair;
 24  
 
 25  
 /**
 26  
  * An implementation of <code>Graph</code> that is suitable for sparse graphs,
 27  
  * orders its vertex and edge collections according to either specified <code>Comparator</code>
 28  
  * instances or the natural ordering of their elements, and permits directed, undirected,
 29  
  * and parallel edges. 
 30  
  * 
 31  
  * @author Joshua O'Madadhain
 32  
  */
 33  
 @SuppressWarnings("serial")
 34  
 public class SortedSparseMultigraph<V,E> 
 35  
     extends OrderedSparseMultigraph<V,E>
 36  
     implements MultiGraph<V,E> 
 37  
 {
 38  
     /**
 39  
      * Returns a {@code Factory} that creates an instance of this graph type.
 40  
      * @param <V> the vertex type for the graph factory
 41  
      * @param <E> the edge type for the graph factory
 42  
      */
 43  
         public static <V,E> Factory<Graph<V,E>> getFactory() 
 44  
         { 
 45  2
                 return new Factory<Graph<V,E>> () 
 46  
                 {
 47  6
                         public Graph<V,E> create() 
 48  
                         {
 49  4
                                 return new SortedSparseMultigraph<V,E>();
 50  
                         }
 51  
                 };
 52  
         }
 53  
     
 54  
     /**
 55  
      * <code>Comparator</code> used in ordering vertices.  Defaults to <code>util.ComparableComparator</code>
 56  
      * if no comparators are specified in the constructor.
 57  
      */
 58  
     protected Comparator<V> vertex_comparator;
 59  
 
 60  
     /**
 61  
      * <code>Comparator</code> used in ordering edges.  Defaults to <code>util.ComparableComparator</code>
 62  
      * if no comparators are specified in the constructor.
 63  
      */
 64  
     protected Comparator<E> edge_comparator;
 65  
     
 66  
     /**
 67  
      * Creates a new instance which sorts its vertices and edges according to the 
 68  
      * specified {@code Comparator}s.
 69  
      */
 70  
     public SortedSparseMultigraph(Comparator<V> vertex_comparator, Comparator<E> edge_comparator)
 71  68
     {
 72  68
         this.vertex_comparator = vertex_comparator;
 73  68
         this.edge_comparator = edge_comparator;
 74  68
         vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
 75  68
         edges = new TreeMap<E, Pair<V>>(edge_comparator);
 76  68
         directedEdges = new TreeSet<E>(edge_comparator);
 77  68
     }
 78  
     
 79  
     /**
 80  
      * Creates a new instance which sorts its vertices and edges according to 
 81  
      * their natural ordering.
 82  
      */
 83  
     @SuppressWarnings("unchecked")
 84  
     public SortedSparseMultigraph()
 85  
     {
 86  68
         this(new ComparableComparator(), new ComparableComparator());
 87  68
     }
 88  
 
 89  
     /**
 90  
      * Provides a new {@code Comparator} to be used in sorting the vertices.
 91  
      * @param vertex_comparator the comparator that defines the new ordering
 92  
      */
 93  
     public void setVertexComparator(Comparator<V> vertex_comparator)
 94  
     {
 95  0
         this.vertex_comparator = vertex_comparator;
 96  0
         Map<V, Pair<Set<E>>> tmp_vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
 97  0
         for (Map.Entry<V, Pair<Set<E>>> entry : vertices.entrySet())
 98  0
             tmp_vertices.put(entry.getKey(), entry.getValue());
 99  0
         this.vertices = tmp_vertices;
 100  0
     }
 101  
     
 102  
     @Override
 103  
     public boolean addVertex(V vertex) {
 104  364
         if(vertex == null) {
 105  2
             throw new IllegalArgumentException("vertex may not be null");
 106  
         }
 107  362
         if (!containsVertex(vertex)) 
 108  
         {
 109  328
             vertices.put(vertex, new Pair<Set<E>>(new TreeSet<E>(edge_comparator), 
 110  
                 new TreeSet<E>(edge_comparator)));
 111  328
             return true;
 112  
         } 
 113  
         else 
 114  
         {
 115  2
                 return false;
 116  
         }
 117  
     }
 118  
 }