View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.graph;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  import static com.google.common.graph.Graphs.checkNonNegative;
21  
22  import com.google.common.annotations.Beta;
23  import com.google.common.base.Optional;
24  
25  /**
26   * A builder for constructing instances of {@link MutableNetwork} with user-defined properties.
27   *
28   * <p>A network built by this class will have the following properties by default:
29   *
30   * <ul>
31   *   <li>does not allow parallel edges
32   *   <li>does not allow self-loops
33   *   <li>orders {@link Network#nodes()} and {@link Network#edges()} in the order in which the
34   *       elements were added
35   * </ul>
36   *
37   * <p>Example of use:
38   *
39   * <pre>{@code
40   * MutableNetwork<String, Integer> flightNetwork =
41   *     NetworkBuilder.directed().allowsParallelEdges(true).build();
42   * flightNetwork.addEdge("LAX", "ATL", 3025);
43   * flightNetwork.addEdge("LAX", "ATL", 1598);
44   * flightNetwork.addEdge("ATL", "LAX", 2450);
45   * }</pre>
46   *
47   * @author James Sexton
48   * @author Joshua O'Madadhain
49   * @since 20.0
50   */
51  @Beta
52  public final class NetworkBuilder<N, E> extends AbstractGraphBuilder<N> {
53    boolean allowsParallelEdges = false;
54    ElementOrder<? super E> edgeOrder = ElementOrder.insertion();
55    Optional<Integer> expectedEdgeCount = Optional.absent();
56  
57    /** Creates a new instance with the specified edge directionality. */
58    private NetworkBuilder(boolean directed) {
59      super(directed);
60    }
61  
62    /** Returns a {@link NetworkBuilder} for building directed networks. */
63    public static NetworkBuilder<Object, Object> directed() {
64      return new NetworkBuilder<>(true);
65    }
66  
67    /** Returns a {@link NetworkBuilder} for building undirected networks. */
68    public static NetworkBuilder<Object, Object> undirected() {
69      return new NetworkBuilder<>(false);
70    }
71  
72    /**
73     * Returns a {@link NetworkBuilder} initialized with all properties queryable from {@code
74     * network}.
75     *
76     * <p>The "queryable" properties are those that are exposed through the {@link Network} interface,
77     * such as {@link Network#isDirected()}. Other properties, such as {@link
78     * #expectedNodeCount(int)}, are not set in the new builder.
79     */
80    public static <N, E> NetworkBuilder<N, E> from(Network<N, E> network) {
81      return new NetworkBuilder<N, E>(network.isDirected())
82          .allowsParallelEdges(network.allowsParallelEdges())
83          .allowsSelfLoops(network.allowsSelfLoops())
84          .nodeOrder(network.nodeOrder())
85          .edgeOrder(network.edgeOrder());
86    }
87  
88    /**
89     * Specifies whether the network will allow parallel edges. Attempting to add a parallel edge to a
90     * network that does not allow them will throw an {@link UnsupportedOperationException}.
91     */
92    public NetworkBuilder<N, E> allowsParallelEdges(boolean allowsParallelEdges) {
93      this.allowsParallelEdges = allowsParallelEdges;
94      return this;
95    }
96  
97    /**
98     * Specifies whether the network will allow self-loops (edges that connect a node to itself).
99     * Attempting to add a self-loop to a network that does not allow them will throw an {@link
100    * UnsupportedOperationException}.
101    */
102   public NetworkBuilder<N, E> allowsSelfLoops(boolean allowsSelfLoops) {
103     this.allowsSelfLoops = allowsSelfLoops;
104     return this;
105   }
106 
107   /**
108    * Specifies the expected number of nodes in the network.
109    *
110    * @throws IllegalArgumentException if {@code expectedNodeCount} is negative
111    */
112   public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) {
113     this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount));
114     return this;
115   }
116 
117   /**
118    * Specifies the expected number of edges in the network.
119    *
120    * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative
121    */
122   public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) {
123     this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount));
124     return this;
125   }
126 
127   /** Specifies the order of iteration for the elements of {@link Network#nodes()}. */
128   public <N1 extends N> NetworkBuilder<N1, E> nodeOrder(ElementOrder<N1> nodeOrder) {
129     NetworkBuilder<N1, E> newBuilder = cast();
130     newBuilder.nodeOrder = checkNotNull(nodeOrder);
131     return newBuilder;
132   }
133 
134   /** Specifies the order of iteration for the elements of {@link Network#edges()}. */
135   public <E1 extends E> NetworkBuilder<N, E1> edgeOrder(ElementOrder<E1> edgeOrder) {
136     NetworkBuilder<N, E1> newBuilder = cast();
137     newBuilder.edgeOrder = checkNotNull(edgeOrder);
138     return newBuilder;
139   }
140 
141   /** Returns an empty {@link MutableNetwork} with the properties of this {@link NetworkBuilder}. */
142   public <N1 extends N, E1 extends E> MutableNetwork<N1, E1> build() {
143     return new ConfigurableMutableNetwork<>(this);
144   }
145 
146   @SuppressWarnings("unchecked")
147   private <N1 extends N, E1 extends E> NetworkBuilder<N1, E1> cast() {
148     return (NetworkBuilder<N1, E1>) this;
149   }
150 }