Coverage Report - edu.uci.ics.jung.graph.UndirectedOrderedSparseMultigraph
 
Classes in this File Line Coverage Branch Coverage Complexity
UndirectedOrderedSparseMultigraph
47%
11/23
40%
4/10
2.6
UndirectedOrderedSparseMultigraph$1
100%
2/2
N/A
2.6
 
 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.Collection;
 15  
 import java.util.Collections;
 16  
 import java.util.LinkedHashMap;
 17  
 import java.util.LinkedHashSet;
 18  
 import java.util.Set;
 19  
 
 20  
 import org.apache.commons.collections15.Factory;
 21  
 
 22  
 import edu.uci.ics.jung.graph.util.Pair;
 23  
 
 24  
 /**
 25  
  * An implementation of <code>UndirectedGraph</code> that is suitable for sparse graphs,
 26  
  * orders its vertex and edge collections according to insertion time, and permits
 27  
  * parallel edges.
 28  
  */
 29  
 @SuppressWarnings("serial")
 30  
 public class UndirectedOrderedSparseMultigraph<V,E> 
 31  
     extends UndirectedSparseMultigraph<V,E>
 32  
     implements UndirectedGraph<V,E> {
 33  
         
 34  
     /**
 35  
      * Returns a {@code Factory} that creates an instance of this graph type.
 36  
      * @param <V> the vertex type for the graph factory
 37  
      * @param <E> the edge type for the graph factory
 38  
      */
 39  
         public static <V,E> Factory<UndirectedGraph<V,E>> getFactory() {
 40  2
                 return new Factory<UndirectedGraph<V,E>> () {
 41  6
                         public UndirectedGraph<V,E> create() {
 42  4
                                 return new UndirectedOrderedSparseMultigraph<V,E>();
 43  
                         }
 44  
                 };
 45  
         }
 46  
 
 47  
         /**
 48  
          * Creates a new instance.
 49  
          */
 50  4
     public UndirectedOrderedSparseMultigraph() {
 51  4
         vertices = new LinkedHashMap<V, Set<E>>();
 52  4
         edges = new LinkedHashMap<E, Pair<V>>();
 53  4
     }
 54  
 
 55  
     @Override
 56  
     public boolean addVertex(V vertex) {
 57  10
             if(vertex == null) {
 58  2
                     throw new IllegalArgumentException("vertex may not be null");
 59  
             }
 60  8
         if (!containsVertex(vertex))
 61  
         {
 62  6
             vertices.put(vertex, new LinkedHashSet<E>());
 63  6
             return true;
 64  
         } else {
 65  2
             return false;
 66  
         }
 67  
     }
 68  
 
 69  
     @Override
 70  
     public Collection<V> getNeighbors(V vertex) {
 71  0
         if (!containsVertex(vertex))
 72  0
             return null;
 73  
         
 74  0
         Set<V> neighbors = new LinkedHashSet<V>();
 75  0
         for (E edge : getIncident_internal(vertex))
 76  
         {
 77  0
             Pair<V> endpoints = this.getEndpoints(edge);
 78  0
             V e_a = endpoints.getFirst();
 79  0
             V e_b = endpoints.getSecond();
 80  0
             if (vertex.equals(e_a))
 81  0
                 neighbors.add(e_b);
 82  
             else
 83  0
                 neighbors.add(e_a);
 84  0
         }
 85  
         
 86  0
         return Collections.unmodifiableCollection(neighbors);
 87  
     }
 88  
 }