Initial opendaylight infrastructure commit!!
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / scoring / util / UniformInOut.java
1 /*
2  * Created on Jul 11, 2008
3  *
4  * Copyright (c) 2008, 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.algorithms.scoring.util;
13
14 import org.apache.commons.collections15.Transformer;
15
16 import edu.uci.ics.jung.graph.Graph;
17 import edu.uci.ics.jung.graph.util.EdgeType;
18
19 /**
20  * Assigns weights to directed edges (the edge of the vertex/edge pair) depending on 
21  * whether the vertex is the edge's source or its destination.
22  * If the vertex v is the edge's source, assigns 1/outdegree(v).
23  * Otherwise, assigns 1/indegree(w).
24  * Throws <code>IllegalArgumentException</code> if the edge is not directed.
25  */
26 public class UniformInOut<V,E> implements Transformer<VEPair<V,E>, Double>
27 {
28         /**
29          * The graph for which the edge weights are defined.
30          */
31     protected Graph<V,E> graph;
32     
33     /**
34      * Creates an instance for the specified graph.
35      * @param graph the graph for which the edge weights will be defined
36      */
37     public UniformInOut(Graph<V,E> graph)
38     {
39         this.graph = graph;
40     }
41     
42     /**
43      * @see org.apache.commons.collections15.Transformer#transform(Object)
44      * @throws IllegalArgumentException
45      */
46     public Double transform(VEPair<V,E> ve_pair)
47     {
48         V v = ve_pair.getV();
49         E e = ve_pair.getE();
50         if (graph.getEdgeType(e) != EdgeType.DIRECTED)
51                 throw new IllegalArgumentException("This transformer only" +
52                                 " operates on directed edges");
53         return 1.0 / (graph.isSource(v, e) ? 
54                         graph.outDegree(v) : 
55                         graph.inDegree(v));
56     }
57 }