KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > generate > LinearGraphGenerator


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  * LinearGraphGenerator.java
27  * -------------------
28  * (C) Copyright 2003-2006, by John V. Sichi and Contributors.
29  *
30  * Original Author: John V. Sichi
31  * Contributor(s): -
32  *
33  * $Id: LinearGraphGenerator.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 16-Sep-2003 : Initial revision (JVS);
38  *
39  */

40 package org.jgrapht.generate;
41
42 import java.util.*;
43
44 import org.jgrapht.*;
45
46
47 /**
48  * Generates a linear graph of any size. For a directed graph, the edges are
49  * oriented from START_VERTEX to END_VERTEX.
50  *
51  * @author John V. Sichi
52  * @since Sep 16, 2003
53  */

54 public class LinearGraphGenerator<V, E>
55     implements GraphGenerator<V, E, V>
56 {
57
58     //~ Static fields/initializers --------------------------------------------
59

60     /**
61      * Role for the first vertex generated.
62      */

63     public static final String JavaDoc START_VERTEX = "Start Vertex";
64
65     /**
66      * Role for the last vertex generated.
67      */

68     public static final String JavaDoc END_VERTEX = "End Vertex";
69
70     //~ Instance fields -------------------------------------------------------
71

72     private int size;
73
74     //~ Constructors ----------------------------------------------------------
75

76     /**
77      * Construct a new LinearGraphGenerator.
78      *
79      * @param size number of vertices to be generated
80      *
81      * @throws IllegalArgumentException if the specified size is negative.
82      */

83     public LinearGraphGenerator(int size)
84     {
85         if (size < 0) {
86             throw new IllegalArgumentException JavaDoc("must be non-negative");
87         }
88
89         this.size = size;
90     }
91
92     //~ Methods ---------------------------------------------------------------
93

94     /**
95      * {@inheritDoc}
96      */

97     public void generateGraph(
98         Graph<V, E> target,
99         VertexFactory<V> vertexFactory,
100         Map<String JavaDoc, V> resultMap)
101     {
102         V lastVertex = null;
103
104         for (int i = 0; i < size; ++i) {
105             V newVertex = vertexFactory.createVertex();
106             target.addVertex(newVertex);
107
108             if (lastVertex == null) {
109                 if (resultMap != null) {
110                     resultMap.put(START_VERTEX, newVertex);
111                 }
112             } else {
113                 target.addEdge(lastVertex, newVertex);
114             }
115
116             lastVertex = newVertex;
117         }
118
119         if ((resultMap != null) && (lastVertex != null)) {
120             resultMap.put(END_VERTEX, lastVertex);
121         }
122     }
123 }
124
Popular Tags