Package com.google.common.graph
Class NetworkBuilder<N,E>
- java.lang.Object
-
- com.google.common.graph.NetworkBuilder<N,E>
-
- 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 existingNetwork
usingfrom(Network)
.E
- The most general edge type this builder will support. This is normallyObject
unless it is constrained by using a method likeedgeOrder
, or the builder is constructed based on an existingNetwork
usingfrom(Network)
.
@Beta public final class NetworkBuilder<N,E> extends Object
A builder for constructing instances ofMutableNetwork
orImmutableNetwork
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
Examples of use:
// Building a mutable network MutableNetwork<String, Integer> network = NetworkBuilder.directed().allowsParallelEdges(true).build(); flightNetwork.addEdge("LAX", "ATL", 3025); flightNetwork.addEdge("LAX", "ATL", 1598); flightNetwork.addEdge("ATL", "LAX", 2450); // Building a immutable network ImmutableNetwork<String, Integer> immutableNetwork = NetworkBuilder.directed() .allowsParallelEdges(true) .<String, Integer>immutable() .addEdge("LAX", "ATL", 3025) .addEdge("LAX", "ATL", 1598) .addEdge("ATL", "LAX", 2450) .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 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<Object,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,E1 extends E>
ImmutableNetwork.Builder<N1,E1>immutable()
Returns anImmutableNetwork.Builder
with the properties of thisNetworkBuilder
.<N1 extends N>
NetworkBuilder<N1,E>nodeOrder(ElementOrder<N1> nodeOrder)
Specifies the order of iteration for the elements ofNetwork.nodes()
.static NetworkBuilder<Object,Object>
undirected()
Returns aNetworkBuilder
for building undirected networks.
-
-
-
Method Detail
-
directed
public static NetworkBuilder<Object,Object> directed()
Returns aNetworkBuilder
for building directed networks.
-
undirected
public static NetworkBuilder<Object,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.
-
immutable
public <N1 extends N,E1 extends E> ImmutableNetwork.Builder<N1,E1> immutable()
Returns anImmutableNetwork.Builder
with the properties of thisNetworkBuilder
.The returned builder can be used for populating an
ImmutableNetwork
.- Since:
- 28.0
-
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
.The default value is
false
.
-
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
.The default value is
false
.
-
expectedNodeCount
public NetworkBuilder<N,E> expectedNodeCount(int expectedNodeCount)
Specifies the expected number of nodes in the network.- Throws:
IllegalArgumentException
- ifexpectedNodeCount
is negative
-
expectedEdgeCount
public NetworkBuilder<N,E> expectedEdgeCount(int expectedEdgeCount)
Specifies the expected number of edges in the network.- Throws:
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()
.The default value is
insertion order
.
-
edgeOrder
public <E1 extends E> NetworkBuilder<N,E1> edgeOrder(ElementOrder<E1> edgeOrder)
Specifies the order of iteration for the elements ofNetwork.edges()
.The default value is
insertion order
.
-
build
public <N1 extends N,E1 extends E> MutableNetwork<N1,E1> build()
Returns an emptyMutableNetwork
with the properties of thisNetworkBuilder
.
-
-