Top Description Fields Constructors Methods
com.google.common.graph

public final Class GraphBuilder<N>

extends AbstractGraphBuilder<N>
Class Inheritance
Annotations
@Beta
@DoNotMock
@ElementTypesAreNonnullByDefault
Type Parameters
<N>
The most general node type this builder will support. This is normally Object unless it is constrained by using a method like nodeOrder, or the builder is constructed based on an existing Graph using from(Graph).
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.graph.Graphs.checkNonNegative

A builder for constructing instances of MutableGraph or ImmutableGraph with user-defined properties.

A Graph built by this class has the following default properties:

Graphs built by this class also guarantee that each collection-returning accessor returns a (live) unmodifiable view; see the external documentation for details.

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();
Authors
James Sexton, Joshua O'Madadhain
Since
20.0

Field Summary

Inherited from com.google.common.graph.AbstractGraphBuilder:
allowsSelfLoopsdirectedexpectedNodeCountincidentEdgeOrdernodeOrder

Constructor Summary

AccessConstructor and Description
private
GraphBuilder(boolean directed)

Creates a new instance with the specified edge directionality.

Method Summary

Modifier and TypeMethod and Description
public GraphBuilder<N>
allowsSelfLoops(boolean allowsSelfLoops)

Specifies whether the graph will allow self-loops (edges that connect a node to itself).

public <N1 extends N> MutableGraph<N1>
build()

Returns an empty MutableGraph with the properties of this GraphBuilder.

private <N1 extends N> GraphBuilder<N1>
cast()

pack-priv GraphBuilder<N>
copy()

public static GraphBuilder<Object>
directed()

Returns a GraphBuilder for building directed graphs.

public GraphBuilder<N>
expectedNodeCount(int expectedNodeCount)

Specifies the expected number of nodes in the graph.

public static <N> GraphBuilder<N>
from(Graph<N> graph)

Returns a GraphBuilder initialized with all properties queryable from graph.

public <N1 extends N> ImmutableGraph.Builder<N1>
immutable()

Returns an ImmutableGraph.Builder with the properties of this GraphBuilder.

public <N1 extends N> GraphBuilder<N1>
public <N1 extends N> GraphBuilder<N1>
nodeOrder(ElementOrder<N1> nodeOrder)

Specifies the order of iteration for the elements of Graph#nodes().

public static GraphBuilder<Object>
undirected()

Returns a GraphBuilder for building undirected graphs.

Constructor Detail

GraphBuilderback to summary
private GraphBuilder(boolean directed)

Creates a new instance with the specified edge directionality.

Method Detail

allowsSelfLoopsback to summary
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 an UnsupportedOperationException.

The default value is false.

Annotations
@CanIgnoreReturnValue
buildback to summary
public <N1 extends N> MutableGraph<N1> build()

Returns an empty MutableGraph with the properties of this GraphBuilder.

castback to summary
private <N1 extends N> GraphBuilder<N1> cast()
Annotations
@SuppressWarnings:unchecked
copyback to summary
pack-priv GraphBuilder<N> copy()
directedback to summary
public static GraphBuilder<Object> directed()

Returns a GraphBuilder for building directed graphs.

expectedNodeCountback to summary
public GraphBuilder<N> expectedNodeCount(int expectedNodeCount)

Specifies the expected number of nodes in the graph.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if expectedNodeCount is negative
fromback to summary
public static <N> GraphBuilder<N> from(Graph<N> graph)

Returns a 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.

immutableback to summary
public <N1 extends N> ImmutableGraph.Builder<N1> immutable()

Returns an ImmutableGraph.Builder with the properties of this GraphBuilder.

The returned builder can be used for populating an ImmutableGraph.

Note that the returned builder will always have incidentEdgeOrder set to ElementOrder#stable(), regardless of the value that was set in this builder.

Since
28.0
incidentEdgeOrderback to summary
public <N1 extends N> GraphBuilder<N1> incidentEdgeOrder(ElementOrder<N1> incidentEdgeOrder)

Specifies the order of iteration for the elements of Graph#edges(), Graph#adjacentNodes(Object), Graph#predecessors(Object), Graph#successors(Object) and Graph#incidentEdges(Object).

The default value is unordered for mutable graphs. For immutable graphs, this value is ignored; they always have a stable order.

Exceptions
IllegalArgumentException:
if incidentEdgeOrder is not either ElementOrder.unordered() or ElementOrder.stable().
Since
29.0
nodeOrderback to summary
public <N1 extends N> GraphBuilder<N1> nodeOrder(ElementOrder<N1> nodeOrder)

Specifies the order of iteration for the elements of Graph#nodes().

The default value is insertion order.

undirectedback to summary
public static GraphBuilder<Object> undirected()

Returns a GraphBuilder for building undirected graphs.