N
 The most general node type this builder will support. This is normally Object
unless it is constrained by using a method like nodeOrder(com.google.common.graph.ElementOrder<N1>)
, or the builder is
constructed based on an existing Graph
using from(Graph)
.@Beta public final class GraphBuilder<N> extends Object
MutableGraph
or ImmutableGraph
with
userdefined properties.
A graph built by this class will have the following properties by default:
Graph.nodes()
in the order in which the elements were added
Examples of use:
// Building a mutable graph
MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build();
graph.putEdge("bread", "bread");
graph.putEdge("chocolate", "peanut butter");
graph.putEdge("peanut butter", "jelly");
// Building an immutable graph
ImmutableGraph<String> immutableGraph =
GraphBuilder.undirected()
.allowsSelfLoops(true)
.<String>immutable()
.putEdge("bread", "bread")
.putEdge("chocolate", "peanut butter")
.putEdge("peanut butter", "jelly")
.build();
Modifier and Type  Method and Description 

GraphBuilder<N> 
allowsSelfLoops(boolean allowsSelfLoops)
Specifies whether the graph will allow selfloops (edges that connect a node to itself).

<N1 extends N> 
build()
Returns an empty
MutableGraph with the properties of this GraphBuilder . 
static GraphBuilder<Object> 
directed()
Returns a
GraphBuilder 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 a
GraphBuilder initialized with all properties queryable from graph . 
<N1 extends N> 
immutable()
Returns an
ImmutableGraph.Builder with the properties of this GraphBuilder . 
<N1 extends N> 
nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements of
Graph.nodes() . 
static GraphBuilder<Object> 
undirected()
Returns a
GraphBuilder for building undirected graphs. 
public static GraphBuilder<Object> directed()
GraphBuilder
for building directed graphs.public static GraphBuilder<Object> undirected()
GraphBuilder
for building undirected graphs.public static <N> GraphBuilder<N> from(Graph<N> graph)
GraphBuilder
initialized with all properties queryable from graph
.
The "queryable" properties are those that are exposed through the Graph
interface,
such as Graph.isDirected()
. Other properties, such as expectedNodeCount(int)
,
are not set in the new builder.
public <N1 extends N> ImmutableGraph.Builder<N1> immutable()
ImmutableGraph.Builder
with the properties of this GraphBuilder
.
The returned builder can be used for populating an ImmutableGraph
.
public GraphBuilder<N> allowsSelfLoops(boolean allowsSelfLoops)
UnsupportedOperationException
.public GraphBuilder<N> expectedNodeCount(int expectedNodeCount)
IllegalArgumentException
 if expectedNodeCount
is negativepublic <N1 extends N> GraphBuilder<N1> nodeOrder(ElementOrder<N1> nodeOrder)
Graph.nodes()
.public <N1 extends N> MutableGraph<N1> build()
MutableGraph
with the properties of this GraphBuilder
.Copyright © 2010–2019. All rights reserved.