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

public final Class NetworkBuilder<N, E>

extends AbstractGraphBuilder<N>
Class Inheritance
Annotations
@Beta
@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 Network using from(Network).
<E>
The most general edge type this builder will support. This is normally Object unless it is constrained by using a method like edgeOrder, or the builder is constructed based on an existing Network using from(Network).
Static Imports
com.google.common.base.Preconditions.checkNotNull, com.google.common.graph.Graphs.checkNonNegative

A builder for constructing instances of MutableNetwork or ImmutableNetwork with user-defined properties.

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

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

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv ElementOrder<? super E>
pack-priv Optional<Integer>
Inherited from com.google.common.graph.AbstractGraphBuilder:
allowsSelfLoopsdirectedexpectedNodeCountincidentEdgeOrdernodeOrder

Constructor Summary

AccessConstructor and Description
private
NetworkBuilder(boolean directed)

Creates a new instance with the specified edge directionality.

Method Summary

Modifier and TypeMethod and Description
public NetworkBuilder<N, E>
allowsParallelEdges(boolean allowsParallelEdges)

Specifies whether the network will allow parallel edges.

public NetworkBuilder<N, E>
allowsSelfLoops(boolean allowsSelfLoops)

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

public <N1 extends N, E1 extends E> MutableNetwork<N1, E1>
build()

Returns an empty MutableNetwork with the properties of this NetworkBuilder.

private <N1 extends N, E1 extends E> NetworkBuilder<N1, E1>
cast()

public static NetworkBuilder<Object, Object>
directed()

Returns a NetworkBuilder for building directed networks.

public <E1 extends E> NetworkBuilder<N, E1>
edgeOrder(ElementOrder<E1> edgeOrder)

Specifies the order of iteration for the elements of Network#edges().

public NetworkBuilder<N, E>
expectedEdgeCount(int expectedEdgeCount)

Specifies the expected number of edges in the network.

public NetworkBuilder<N, E>
expectedNodeCount(int expectedNodeCount)

Specifies the expected number of nodes in the network.

public static <N, E> NetworkBuilder<N, E>
from(Network<N, E> network)

Returns a NetworkBuilder initialized with all properties queryable from network.

public <N1 extends N, E1 extends E> ImmutableNetwork.Builder<N1, E1>
immutable()

Returns an ImmutableNetwork.Builder with the properties of this NetworkBuilder.

public <N1 extends N> NetworkBuilder<N1, E>
nodeOrder(ElementOrder<N1> nodeOrder)

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

public static NetworkBuilder<Object, Object>
undirected()

Returns a NetworkBuilder for building undirected networks.

Field Detail

allowsParallelEdgesback to summary
pack-priv boolean allowsParallelEdges
edgeOrderback to summary
pack-priv ElementOrder<? super E> edgeOrder
expectedEdgeCountback to summary
pack-priv Optional<Integer> expectedEdgeCount

Constructor Detail

NetworkBuilderback to summary
private NetworkBuilder(boolean directed)

Creates a new instance with the specified edge directionality.

Method Detail

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

The default value is false.

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

The default value is false.

Annotations
@CanIgnoreReturnValue
buildback to summary
public <N1 extends N, E1 extends E> MutableNetwork<N1, E1> build()

Returns an empty MutableNetwork with the properties of this NetworkBuilder.

castback to summary
private <N1 extends N, E1 extends E> NetworkBuilder<N1, E1> cast()
Annotations
@SuppressWarnings:unchecked
directedback to summary
public static NetworkBuilder<Object, Object> directed()

Returns a NetworkBuilder for building directed networks.

edgeOrderback to summary
public <E1 extends E> NetworkBuilder<N, E1> edgeOrder(ElementOrder<E1> edgeOrder)

Specifies the order of iteration for the elements of Network#edges().

The default value is insertion order.

expectedEdgeCountback to summary
public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount)

Specifies the expected number of edges in the network.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if expectedEdgeCount is negative
expectedNodeCountback to summary
public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount)

Specifies the expected number of nodes in the network.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if expectedNodeCount is negative
fromback to summary
public static <N, E> NetworkBuilder<N, E> from(Network<N, E> network)

Returns a NetworkBuilder initialized with all properties queryable from network.

The "queryable" properties are those that are exposed through the Network interface, such as Network#isDirected(). Other properties, such as expectedNodeCount(int), are not set in the new builder.

immutableback to summary
public <N1 extends N, E1 extends E> ImmutableNetwork.Builder<N1, E1> immutable()

Returns an ImmutableNetwork.Builder with the properties of this NetworkBuilder.

The returned builder can be used for populating an ImmutableNetwork.

Since
28.0
nodeOrderback to summary
public <N1 extends N> NetworkBuilder<N1, E> nodeOrder(ElementOrder<N1> nodeOrder)

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

The default value is insertion order.

undirectedback to summary
public static NetworkBuilder<Object, Object> undirected()

Returns a NetworkBuilder for building undirected networks.