Package com.google.common.graph
Class GraphBuilder<N>
- java.lang.Object
-
- com.google.common.graph.GraphBuilder<N>
-
@Beta public final class GraphBuilder<N> extends java.lang.Object
A builder for constructing instances ofMutableGraph
with user-defined properties.A graph built by this class will have the following properties by default:
- does not allow self-loops
- orders
Graph.nodes()
in the order in which the elements were added
Example of use:
MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build(); graph.putEdge("bread", "bread"); graph.putEdge("chocolate", "peanut butter"); graph.putEdge("peanut butter", "jelly");
- Since:
- 20.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description GraphBuilder<N>
allowsSelfLoops(boolean allowsSelfLoops)
Specifies whether the graph will allow self-loops (edges that connect a node to itself).<N1 extends N>
MutableGraph<N1>build()
Returns an emptyMutableGraph
with the properties of thisGraphBuilder
.static GraphBuilder<java.lang.Object>
directed()
Returns aGraphBuilder
for building directed graphs.GraphBuilder<N>
expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the graph.static <N> GraphBuilder<N>
from(Graph<N> graph)
Returns aGraphBuilder
initialized with all properties queryable fromgraph
.<N1 extends N>
GraphBuilder<N1>nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofGraph.nodes()
.static GraphBuilder<java.lang.Object>
undirected()
Returns aGraphBuilder
for building undirected graphs.
-
-
-
Method Detail
-
directed
public static GraphBuilder<java.lang.Object> directed()
Returns aGraphBuilder
for building directed graphs.
-
undirected
public static GraphBuilder<java.lang.Object> undirected()
Returns aGraphBuilder
for building undirected graphs.
-
from
public static <N> GraphBuilder<N> from(Graph<N> graph)
Returns aGraphBuilder
initialized with all properties queryable fromgraph
.The "queryable" properties are those that are exposed through the
Graph
interface, such asGraph.isDirected()
. Other properties, such asexpectedNodeCount(int)
, are not set in the new builder.
-
allowsSelfLoops
public GraphBuilder<N> allowsSelfLoops(boolean allowsSelfLoops)
Specifies whether the graph will allow self-loops (edges that connect a node to itself). Attempting to add a self-loop to a graph that does not allow them will throw anUnsupportedOperationException
.
-
expectedNodeCount
public GraphBuilder<N> expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the graph.- Throws:
java.lang.IllegalArgumentException
- ifexpectedNodeCount
is negative
-
nodeOrder
public <N1 extends N> GraphBuilder<N1> nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofGraph.nodes()
.
-
build
public <N1 extends N> MutableGraph<N1> build()
Returns an emptyMutableGraph
with the properties of thisGraphBuilder
.
-
-