KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > graph > DefaultGraphMapping


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * DefaultGraphMapping.java
27  * -----------------
28  * (C) Copyright 2005-2006, by Assaf Lehr and Contributors.
29  *
30  * Original Author: Assaf Lehr
31  * Contributor(s): -
32  *
33  * $Id: DefaultGraphMapping.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  */

38 package org.jgrapht.graph;
39
40 import java.util.*;
41
42 import org.jgrapht.*;
43
44
45 /**
46  * Implementation of the GraphMapping interface. The performance of <code>
47  * getVertex/EdgeCorrespondence</code> is based on the performance of the
48  * concrete Map class which is passed in the constructor. For example, using
49  * hashmaps will provide O(1) performence.
50  *
51  * @author Assaf
52  * @since Jul 30, 2005
53  */

54 public class DefaultGraphMapping<V, E>
55     implements GraphMapping<V, E>
56 {
57
58     //~ Instance fields -------------------------------------------------------
59

60     private Map<V, V> graphMappingForward;
61     private Map<V, V> graphMappingReverse;
62
63     private Graph<V, E> graph1;
64     private Graph<V, E> graph2;
65
66     //~ Constructors ----------------------------------------------------------
67

68     /**
69      * The maps themselves are used. There is no defensive-copy. Assumption:
70      * The key and value in the mappings are of valid graph objects. It is not
71      * checked.
72      *
73      * @param g1ToG2
74      * @param g2ToG1
75      * @param g1
76      * @param g2
77      */

78     public DefaultGraphMapping(
79         Map<V, V> g1ToG2,
80         Map<V, V> g2ToG1,
81         Graph<V, E> g1,
82         Graph<V, E> g2)
83     {
84         this.graph1 = g1;
85         this.graph2 = g2;
86         this.graphMappingForward = g1ToG2;
87         this.graphMappingReverse = g2ToG1;
88     }
89
90     //~ Methods ---------------------------------------------------------------
91

92     public E getEdgeCorrespondence(E currEdge, boolean forward)
93     {
94         Graph<V, E> sourceGraph, targetGraph;
95
96         if (forward) {
97             sourceGraph = this.graph1;
98             targetGraph = this.graph2;
99         } else {
100             sourceGraph = this.graph2;
101             targetGraph = this.graph1;
102         }
103
104         V mappedSourceVertex =
105             getVertexCorrespondence(
106                 sourceGraph.getEdgeSource(currEdge),
107                 forward);
108         V mappedTargetVertex =
109             getVertexCorrespondence(
110                 sourceGraph.getEdgeTarget(currEdge),
111                 forward);
112         if ((mappedSourceVertex == null) || (mappedTargetVertex == null)) {
113             return null;
114         } else {
115             return targetGraph.getEdge(
116                     mappedSourceVertex,
117                     mappedTargetVertex);
118         }
119     }
120
121     public V getVertexCorrespondence(
122         V keyVertex,
123         boolean forward)
124     {
125         Map<V, V> graphMapping;
126         if (forward) {
127             graphMapping = graphMappingForward;
128         } else {
129             graphMapping = graphMappingReverse;
130         }
131
132         return graphMapping.get(keyVertex);
133     }
134 }
135
Popular Tags