Coverage Report - com.sdmetrics.metrics.ChangeDirectionsAdapter - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ChangeDirectionsAdapter
100%
16/16
100%
10/10
2,667
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.HashMap;
 26  
 import java.util.HashSet;
 27  
 
 28  
 /**
 29  
  * Constructs a new graph from an existing one, reversing or removing the
 30  
  * directions of the edges.
 31  
  * <p>
 32  
  * The new graph has the same set of nodes as the original graph, and for every
 33  
  * edge (A,B) from node A to node B in the original graph
 34  
  * <ul>
 35  
  * <li>an edge (B,A) in the reversed graph
 36  
  * <li>edges (A,B) and (B,A) in the undirected graph
 37  
  * </ul>
 38  
  * 
 39  
  * @param <T> Type of the nodes on the graph
 40  
  */
 41  
 public class ChangeDirectionsAdapter<T> implements
 42  
                 StronglyConnectedComponents.Graph<T> {
 43  
         /** The set of nodes of the graph. */
 44  
         private final Collection<T> nodes;
 45  
         /** Map with the nodes (key) and new set of new neighbors (value). */
 46  
         private final HashMap<T, Collection<T>> edgeMap;
 47  
 
 48  
         /**
 49  
          * @param graph The original graph.
 50  
          * @param makeUndirected Set to <code>true</code> to obtain an undirected
 51  
          *        graph, or <code>false</code> to obtain the graph with reversed
 52  
          *        edges.
 53  
          * @throws SDMetricsException The neighbors for a node could not be
 54  
          *         determined
 55  
          */
 56  3
         public ChangeDirectionsAdapter(StronglyConnectedComponents.Graph<T> graph,
 57  
                         boolean makeUndirected) throws SDMetricsException {
 58  
 
 59  
                 // Create a new set of edges for each node, initially empty.
 60  3
                 nodes = graph.getNodes();
 61  3
                 edgeMap = new HashMap<T, Collection<T>>(nodes.size());
 62  19
                 for (T node : nodes)
 63  13
                         edgeMap.put(node, new HashSet<T>());
 64  
 
 65  
                 // Walk through old graph, add the appropriate edges in the new graph.
 66  19
                 for (T nodeA : nodes) {
 67  13
                         Collection<T> nodeANeighbors = edgeMap.get(nodeA);
 68  
 
 69  37
                         for (T nodeB : graph.getNeighbors(nodeA)) {
 70  11
                                 Collection<T> nodeBNeighbors = edgeMap.get(nodeB);
 71  11
                                 if (nodeBNeighbors != null) { // nodeB is a node in the graph
 72  
                                         // We have an edge from A to B in the old graph.
 73  
                                         // Add edge from B to A in the new graph, and optionally
 74  
                                         // also retain the edge from A to B in the new graph
 75  9
                                         nodeBNeighbors.add(nodeA);
 76  9
                                         if (makeUndirected)
 77  7
                                                 nodeANeighbors.add(nodeB);
 78  
                                 }
 79  
                         }
 80  
                 }
 81  3
         }
 82  
 
 83  
         // Implementation of the Graph interface methods
 84  
 
 85  
         @Override
 86  
         public Collection<T> getNodes() {
 87  3
                 return nodes;
 88  
         }
 89  
 
 90  
         @Override
 91  
         public Collection<T> getNeighbors(T o) {
 92  13
                 return edgeMap.get(o);
 93  
         }
 94  
 }