Class GraphBuilder<N>
- java.lang.Object
-
- com.google.common.graph.GraphBuilder<N>
-
- Type Parameters:
N
- The most general node type this builder will support. This is normallyObject
unless it is constrained by using a method likenodeOrder(com.google.common.graph.ElementOrder<N1>)
, or the builder is constructed based on an existingGraph
usingfrom(Graph)
.
@Beta @DoNotMock public final class GraphBuilder<N> extends Object
A builder for constructing instances ofMutableGraph
orImmutableGraph
with user-defined properties.A
Graph
built by this class has the following default properties:- does not allow self-loops
- orders
Graph.nodes()
in the order in which the elements were added (insertion order)
Graph
s 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();
- Since:
- 20.0
- Author:
- James Sexton, Joshua O'Madadhain
-
-
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<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>
ImmutableGraph.Builder<N1>immutable()
Returns anImmutableGraph.Builder
with the properties of thisGraphBuilder
.<N1 extends N>
GraphBuilder<N1>incidentEdgeOrder(ElementOrder<N1> incidentEdgeOrder)
Specifies the order of iteration for the elements ofGraph.edges()
,Graph.adjacentNodes(Object)
,Graph.predecessors(Object)
,Graph.successors(Object)
andGraph.incidentEdges(Object)
.<N1 extends N>
GraphBuilder<N1>nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofGraph.nodes()
.static GraphBuilder<Object>
undirected()
Returns aGraphBuilder
for building undirected graphs.
-
-
-
Method Detail
-
directed
public static GraphBuilder<Object> directed()
Returns aGraphBuilder
for building directed graphs.
-
undirected
public static GraphBuilder<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.
-
immutable
public <N1 extends N> ImmutableGraph.Builder<N1> immutable()
Returns anImmutableGraph.Builder
with the properties of thisGraphBuilder
.The returned builder can be used for populating an
ImmutableGraph
.Note that the returned builder will always have
incidentEdgeOrder(com.google.common.graph.ElementOrder<N1>)
set toElementOrder.stable()
, regardless of the value that was set in this builder.- Since:
- 28.0
-
allowsSelfLoops
@CanIgnoreReturnValue 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
.The default value is
false
.
-
expectedNodeCount
@CanIgnoreReturnValue public GraphBuilder<N> expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the graph.- Throws:
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()
.The default value is
insertion order
.
-
incidentEdgeOrder
public <N1 extends N> GraphBuilder<N1> incidentEdgeOrder(ElementOrder<N1> incidentEdgeOrder)
Specifies the order of iteration for the elements ofGraph.edges()
,Graph.adjacentNodes(Object)
,Graph.predecessors(Object)
,Graph.successors(Object)
andGraph.incidentEdges(Object)
.The default value is
unordered
for mutable graphs. For immutable graphs, this value is ignored; they always have astable
order.- Throws:
IllegalArgumentException
- ifincidentEdgeOrder
is not eitherElementOrder.unordered()
orElementOrder.stable()
.- Since:
- 29.0
-
build
public <N1 extends N> MutableGraph<N1> build()
Returns an emptyMutableGraph
with the properties of thisGraphBuilder
.
-
-