| 39 | |
| 40 | |
| 41 | @Beta |
| 42 | public final class GraphBuilder<N> { |
| 43 | final boolean directed; |
| 44 | boolean allowsSelfLoops = true; |
| 45 | |
| 46 | Comparator<N> nodeComparator = null; |
| 47 | |
| 48 | Optional<Integer> expectedNodeCount = Optional.absent(); |
| 49 | |
| 50 | /** |
| 51 | * Creates a new instance with the specified edge directionality. |
| 52 | * |
| 53 | * @param directed if true, creates an instance for graphs whose edges are each directed; |
| 54 | * if false, creates an instance for graphs whose edges are each undirected. |
| 55 | */ |
| 56 | |
| 57 | private GraphBuilder(boolean directed) { |
| 58 | this.directed = directed; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns a {@link GraphBuilder} for building directed graphs. |
| 63 | */ |
| 64 | |
| 65 | |
| 66 | public static GraphBuilder<Object> directed() { |
| 67 | return new GraphBuilder<Object>(true); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns a {@link GraphBuilder} for building undirected graphs. |
| 72 | */ |
| 73 | |
| 74 | |
| 75 | public static GraphBuilder<Object> undirected() { |
| 76 | return new GraphBuilder<Object>(false); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. |
| 81 | * |
| 82 | * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, |
| 83 | * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, |
| 84 | * are not set in the new builder. |
| 85 | */ |
| 86 | |
| 87 | |
| 88 | public static <N> GraphBuilder<N> from(Graph<N> graph) { |
| 89 | // TODO(b/28087289): add allowsParallelEdges() once we support them |
| 90 | return new GraphBuilder<N>(graph.isDirected()).allowsSelfLoops(graph.allowsSelfLoops()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Specifies whether the graph will allow self-loops (edges that connect a node to itself). |
| 95 | * Attempting to add a self-loop to a graph that does not allow them will throw an |
| 96 | * {@link UnsupportedOperationException}. |
| 97 | */ |
| 98 | |