Coverage Report - edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph
 
Classes in this File Line Coverage Branch Coverage Complexity
DirectedOrderedSparseMultigraph
29%
11/37
20%
4/20
3
DirectedOrderedSparseMultigraph$1
100%
2/2
N/A
3
 
 1  
 /*
 2  
  * Created on Oct 17, 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  
 /**
 26  
  * An implementation of <code>DirectedGraph</code>, suitable for sparse graphs, 
 27  
  * that orders its vertex and edge collections
 28  
  * according to insertion time.
 29  
  */
 30  
 @SuppressWarnings("serial")
 31  
 public class DirectedOrderedSparseMultigraph<V,E> 
 32  
     extends DirectedSparseMultigraph<V,E>
 33  
     implements DirectedGraph<V,E>, MultiGraph<V,E> 
 34  
 {
 35  
     /**
 36  
      * Returns a {@code Factory} that creates an instance of this graph type.
 37  
      * @param <V> the vertex type for the graph factory
 38  
      * @param <E> the edge type for the graph factory
 39  
      */
 40  
         public static <V,E> Factory<DirectedGraph<V,E>> getFactory() {
 41  2
                 return new Factory<DirectedGraph<V,E>> () {
 42  6
                         public DirectedGraph<V,E> create() {
 43  4
                                 return new DirectedOrderedSparseMultigraph<V,E>();
 44  
                         }
 45  
                 };
 46  
         }
 47  
 
 48  
     /**
 49  
      * Creates a new instance.
 50  
      */
 51  4
     public DirectedOrderedSparseMultigraph() {
 52  4
         vertices = new LinkedHashMap<V, Pair<Set<E>>>();
 53  4
         edges = new LinkedHashMap<E, Pair<V>>();
 54  4
     }
 55  
     
 56  
     @Override
 57  
     public boolean addVertex(V vertex) {
 58  10
             if(vertex == null) {
 59  2
                     throw new IllegalArgumentException("vertex may not be null");
 60  
             }
 61  8
         if (!containsVertex(vertex)) {
 62  6
             vertices.put(vertex, new Pair<Set<E>>(new LinkedHashSet<E>(), new LinkedHashSet<E>()));
 63  6
             return true;
 64  
         } else {
 65  2
             return false;
 66  
         }
 67  
     }
 68  
 
 69  
     @Override
 70  
     public Collection<V> getPredecessors(V vertex) {
 71  0
         if (!containsVertex(vertex)) 
 72  0
             return null;
 73  0
         Set<V> preds = new LinkedHashSet<V>();
 74  0
         for (E edge : getIncoming_internal(vertex))
 75  0
             preds.add(this.getSource(edge));
 76  
         
 77  0
         return Collections.unmodifiableCollection(preds);
 78  
     }
 79  
 
 80  
     @Override
 81  
     public Collection<V> getSuccessors(V vertex) {
 82  0
         if (!containsVertex(vertex)) 
 83  0
             return null;
 84  0
         Set<V> succs = new LinkedHashSet<V>();
 85  0
         for (E edge : getOutgoing_internal(vertex))
 86  0
             succs.add(this.getDest(edge));
 87  
         
 88  0
         return Collections.unmodifiableCollection(succs);
 89  
     }
 90  
 
 91  
     @Override
 92  
     public Collection<V> getNeighbors(V vertex) {
 93  0
         if (!containsVertex(vertex)) 
 94  0
             return null;
 95  0
         Collection<V> neighbors = new LinkedHashSet<V>();
 96  0
         for (E edge : getIncoming_internal(vertex))
 97  0
             neighbors.add(this.getSource(edge));
 98  0
         for (E edge : getOutgoing_internal(vertex))
 99  0
             neighbors.add(this.getDest(edge));
 100  0
         return Collections.unmodifiableCollection(neighbors);
 101  
     }
 102  
 
 103  
     @Override
 104  
     public Collection<E> getIncidentEdges(V vertex) {
 105  0
         if (!containsVertex(vertex)) 
 106  0
             return null;
 107  0
         Collection<E> incident = new LinkedHashSet<E>();
 108  0
         incident.addAll(getIncoming_internal(vertex));
 109  0
         incident.addAll(getOutgoing_internal(vertex));
 110  0
         return incident;
 111  
     }
 112  
 
 113  
 }