Class NetworkBuilder<N,​E>

  • 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(com.google.common.graph.ElementOrder<N1>), 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).

    public final class NetworkBuilder<N,​E>
    extends AbstractGraphBuilder<N>
    A builder for constructing instances of MutableNetwork or ImmutableNetwork with user-defined properties.

    A network built by this class will have the following properties by default:

    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
    • Field Detail

      • allowsParallelEdges

        boolean allowsParallelEdges
      • expectedEdgeCount

        Optional<java.lang.Integer> expectedEdgeCount
    • Constructor Detail

      • NetworkBuilder

        private NetworkBuilder​(boolean directed)
        Creates a new instance with the specified edge directionality.
    • Method Detail

      • directed

        public static NetworkBuilder<java.lang.Object,​java.lang.Object> directed()
        Returns a NetworkBuilder for building directed networks.
      • undirected

        public static NetworkBuilder<java.lang.Object,​java.lang.Object> undirected()
        Returns a NetworkBuilder for building undirected networks.
      • 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 an UnsupportedOperationException.

        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 an UnsupportedOperationException.

        The default value is false.

      • expectedNodeCount

        public NetworkBuilder<N,​E> expectedNodeCount​(int expectedNodeCount)
        Specifies the expected number of nodes in the network.
        Throws:
        java.lang.IllegalArgumentException - if expectedNodeCount is negative
      • expectedEdgeCount

        public NetworkBuilder<N,​E> expectedEdgeCount​(int expectedEdgeCount)
        Specifies the expected number of edges in the network.
        Throws:
        java.lang.IllegalArgumentException - if expectedEdgeCount is negative
      • cast

        private <N1 extends N,​E1 extends ENetworkBuilder<N1,​E1> cast()