1 25 40 package org.jgrapht.generate; 41 42 import java.util.*; 43 44 import org.jgrapht.*; 45 46 47 54 public class LinearGraphGenerator<V, E> 55 implements GraphGenerator<V, E, V> 56 { 57 58 60 63 public static final String START_VERTEX = "Start Vertex"; 64 65 68 public static final String END_VERTEX = "End Vertex"; 69 70 72 private int size; 73 74 76 83 public LinearGraphGenerator(int size) 84 { 85 if (size < 0) { 86 throw new IllegalArgumentException ("must be non-negative"); 87 } 88 89 this.size = size; 90 } 91 92 94 97 public void generateGraph( 98 Graph<V, E> target, 99 VertexFactory<V> vertexFactory, 100 Map<String , 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 |