Coverage details for edu.uci.ics.jung.utils.ParallelEdgeIndexSingleton

LineHitsSource
1 /*
2  * Created on Sep 24, 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.utils;
13  
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.Set;
18  
19 import edu.uci.ics.jung.graph.Edge;
20 import edu.uci.ics.jung.graph.Graph;
21 import edu.uci.ics.jung.graph.Vertex;
22  
23 /**
24  * A class which creates and maintains indices for parallel edges.
25  * Parallel edges are defined here to be those edges of type <code>Edge</code>
26  * that are returned by <code>v.findEdgeSet(w)</code> for some
27  * <code>v</code> and <code>w</code>.
28  *
29  * <p>At this time, users are responsible for resetting the indices if changes to the
30  * graph make it appropriate.</p>
31  *
32  * @author Joshua O'Madadhain
33  */
34 public class ParallelEdgeIndexSingleton implements ParallelEdgeIndexFunction
35 {
36     
37     private static ParallelEdgeIndexFunction instance;
38     
390    private ParallelEdgeIndexSingleton() {}
40     
41     public static ParallelEdgeIndexFunction getInstance() {
420        if(instance == null) {
430            instance = new ParallelEdgeIndexSingleton();
44         }
450        return instance;
46     }
47  
48  
490    protected Map edge_index = new HashMap();
50     
51     /**
52      * Returns the index for the specified edge.
53      * Calculates the indices for <code>e</code> and for all edges parallel
54      * to <code>e</code>.
55      */
56     public int getIndex(Edge e)
57     {
580        Graph g = (Graph)e.getGraph();
590        if (g == null)
600            throw new IllegalArgumentException("Orphaned edges may not be indexed.");
610        Integer index = (Integer)edge_index.get(new Pair(e, g));
620        if(index == null)
630            index = getIndex_internal(e, g);
640        return index.intValue();
65     }
66  
67     protected Integer getIndex_internal(Edge e, Graph g)
68     {
690        Pair endpoints = e.getEndpoints();
700        Vertex u = (Vertex)(endpoints.getFirst());
710        Vertex v = (Vertex)(endpoints.getSecond());
720        Set commonEdgeSet = u.findEdgeSet(v);
730        int count = 0;
740        for(Iterator iterator=commonEdgeSet.iterator(); iterator.hasNext(); ) {
750            Edge other = (Edge)iterator.next();
760            if (e.equals(other) == false)
77             {
780                edge_index.put(new Pair(other, g), new Integer(count));
790                count++;
80             }
81         }
820        Integer index = new Integer(count);
830        edge_index.put(e, index);
84         
850        return index;
86     }
87     
88     /**
89      * Resets the indices for this edge and its parallel edges.
90      * Should be invoked when an edge parallel to <code>e</code>
91      * has been added or removed.
92      * @param e
93      */
94     public void reset(Edge e)
95     {
960        Graph g = (Graph)e.getGraph();
970        if (g == null)
980            throw new IllegalArgumentException("Orphaned edges may not be indexed.");
990        getIndex_internal(e, g);
1000    }
101     
102     /**
103      * Clears all edge indices for all edges in all graphs.
104      * Does not recalculate the indices.
105      */
106     public void reset()
107     {
1080        edge_index.clear();
1090    }
110 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.