Package com.google.common.graph
Class NetworkBuilder<N,E>
- java.lang.Object
-
- com.google.common.graph.NetworkBuilder<N,E>
-
@Beta public final class NetworkBuilder<N,E> extends java.lang.Object
A builder for constructing instances ofMutableNetwork
with user-defined properties.A network built by this class will have the following properties by default:
- does not allow parallel edges
- does not allow self-loops
- orders
Network.nodes()
andNetwork.edges()
in the order in which the elements were added
Example of use:
MutableNetwork<String, Integer> flightNetwork = NetworkBuilder.directed().allowsParallelEdges(true).build(); flightNetwork.addEdge("LAX", "ATL", 3025); flightNetwork.addEdge("LAX", "ATL", 1598); flightNetwork.addEdge("ATL", "LAX", 2450);
- Since:
- 20.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NetworkBuilder<N,E>
allowsParallelEdges(boolean allowsParallelEdges)
Specifies whether the network will allow parallel edges.NetworkBuilder<N,E>
allowsSelfLoops(boolean allowsSelfLoops)
Specifies whether the network will allow self-loops (edges that connect a node to itself).<N1 extends N,E1 extends E>
MutableNetwork<N1,E1>build()
Returns an emptyMutableNetwork
with the properties of thisNetworkBuilder
.static NetworkBuilder<java.lang.Object,java.lang.Object>
directed()
Returns aNetworkBuilder
for building directed networks.<E1 extends E>
NetworkBuilder<N,E1>edgeOrder(ElementOrder<E1> edgeOrder)
Specifies the order of iteration for the elements ofNetwork.edges()
.NetworkBuilder<N,E>
expectedEdgeCount(int expectedEdgeCount)
Specifies the expected number of edges in the network.NetworkBuilder<N,E>
expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the network.static <N,E>
NetworkBuilder<N,E>from(Network<N,E> network)
Returns aNetworkBuilder
initialized with all properties queryable fromnetwork
.<N1 extends N>
NetworkBuilder<N1,E>nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofNetwork.nodes()
.static NetworkBuilder<java.lang.Object,java.lang.Object>
undirected()
Returns aNetworkBuilder
for building undirected networks.
-
-
-
Method Detail
-
directed
public static NetworkBuilder<java.lang.Object,java.lang.Object> directed()
Returns aNetworkBuilder
for building directed networks.
-
undirected
public static NetworkBuilder<java.lang.Object,java.lang.Object> undirected()
Returns aNetworkBuilder
for building undirected networks.
-
from
public static <N,E> NetworkBuilder<N,E> from(Network<N,E> network)
Returns aNetworkBuilder
initialized with all properties queryable fromnetwork
.The "queryable" properties are those that are exposed through the
Network
interface, such asNetwork.isDirected()
. Other properties, such asexpectedNodeCount(int)
, are not set in the new builder.
-
allowsParallelEdges
public NetworkBuilder<N,E> allowsParallelEdges(boolean allowsParallelEdges)
Specifies whether the network will allow parallel edges. Attempting to add a parallel edge to a network that does not allow them will throw anUnsupportedOperationException
.
-
allowsSelfLoops
public NetworkBuilder<N,E> allowsSelfLoops(boolean allowsSelfLoops)
Specifies whether the network will allow self-loops (edges that connect a node to itself). Attempting to add a self-loop to a network that does not allow them will throw anUnsupportedOperationException
.
-
expectedNodeCount
public NetworkBuilder<N,E> expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the network.- Throws:
java.lang.IllegalArgumentException
- ifexpectedNodeCount
is negative
-
expectedEdgeCount
public NetworkBuilder<N,E> expectedEdgeCount(int expectedEdgeCount)
Specifies the expected number of edges in the network.- Throws:
java.lang.IllegalArgumentException
- ifexpectedEdgeCount
is negative
-
nodeOrder
public <N1 extends N> NetworkBuilder<N1,E> nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofNetwork.nodes()
.
-
edgeOrder
public <E1 extends E> NetworkBuilder<N,E1> edgeOrder(ElementOrder<E1> edgeOrder)
Specifies the order of iteration for the elements ofNetwork.edges()
.
-
build
public <N1 extends N,E1 extends E> MutableNetwork<N1,E1> build()
Returns an emptyMutableNetwork
with the properties of thisNetworkBuilder
.
-
-