Class ImmutableNetwork.Builder<N,E>
- java.lang.Object
-
- com.google.common.graph.ImmutableNetwork.Builder<N,E>
-
- Enclosing class:
- ImmutableNetwork<N,E>
public static class ImmutableNetwork.Builder<N,E> extends java.lang.Object
A builder for creatingImmutableNetwork
instances, especiallystatic final
networks. Example:static final ImmutableNetwork<City, Train> TRAIN_NETWORK = NetworkBuilder.undirected() .allowsParallelEdges(true) .<City, Train>immutable() .addEdge(PARIS, BRUSSELS, Thalys.trainNumber("1111")) .addEdge(PARIS, BRUSSELS, RegionalTrain.trainNumber("2222")) .addEdge(LONDON, PARIS, Eurostar.trainNumber("3333")) .addEdge(LONDON, BRUSSELS, Eurostar.trainNumber("4444")) .addNode(REYKJAVIK) .build();
Builder instances can be reused; it is safe to call
build()
multiple times to build multiple networks in series. Each new network contains all the elements of the ones created before it.- Since:
- 28.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ImmutableNetwork.Builder<N,E>
addEdge(EndpointPair<N> endpoints, E edge)
Addsedge
connectingendpoints
.ImmutableNetwork.Builder<N,E>
addEdge(N nodeU, N nodeV, E edge)
Addsedge
connectingnodeU
tonodeV
.ImmutableNetwork.Builder<N,E>
addNode(N node)
Addsnode
if it is not already present.ImmutableNetwork<N,E>
build()
Returns a newly-createdImmutableNetwork
based on the contents of thisBuilder
.
-
-
-
Method Detail
-
addNode
@CanIgnoreReturnValue public ImmutableNetwork.Builder<N,E> addNode(N node)
Addsnode
if it is not already present.Nodes must be unique, just as
Map
keys must be. They must also be non-null.- Returns:
- this
Builder
object
-
addEdge
@CanIgnoreReturnValue public ImmutableNetwork.Builder<N,E> addEdge(N nodeU, N nodeV, E edge)
Addsedge
connectingnodeU
tonodeV
.If the network is directed,
edge
will be directed in this network; otherwise, it will be undirected.edge
must be unique to this network, just as aMap
key must be. It must also be non-null.If
nodeU
andnodeV
are not already present in this network, this method will silentlyadd
nodeU
andnodeV
to the network.If
edge
already connectsnodeU
tonodeV
(in the specified order if this networkNetwork.isDirected()
, else in any order), then this method will have no effect.- Returns:
- this
Builder
object - Throws:
java.lang.IllegalArgumentException
- ifedge
already exists in the network and does not connectnodeU
tonodeV
java.lang.IllegalArgumentException
- if the introduction of the edge would violateNetwork.allowsParallelEdges()
orNetwork.allowsSelfLoops()
-
addEdge
@CanIgnoreReturnValue public ImmutableNetwork.Builder<N,E> addEdge(EndpointPair<N> endpoints, E edge)
Addsedge
connectingendpoints
. In an undirected network,edge
will also connectnodeV
tonodeU
.If this network is directed,
edge
will be directed in this network; if it is undirected,edge
will be undirected in this network.If this network is directed,
endpoints
must be ordered.edge
must be unique to this network, just as aMap
key must be. It must also be non-null.If either or both endpoints are not already present in this network, this method will silently
add
each missing endpoint to the network.If
edge
already connects an endpoint pair equal toendpoints
, then this method will have no effect.- Returns:
- this
Builder
object - Throws:
java.lang.IllegalArgumentException
- ifedge
already exists in the network and connects some other endpoint pair that is not equal toendpoints
java.lang.IllegalArgumentException
- if the introduction of the edge would violateNetwork.allowsParallelEdges()
orNetwork.allowsSelfLoops()
java.lang.IllegalArgumentException
- if the endpoints are unordered and the network is directed
-
build
public ImmutableNetwork<N,E> build()
Returns a newly-createdImmutableNetwork
based on the contents of thisBuilder
.
-
-