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 import com.google.errorprone.annotations.CanIgnoreReturnValue; 25 26 /** 27 * A builder for constructing instances of {@link MutableNetwork} or {@link ImmutableNetwork} with 28 * user-defined properties. 29 * 30 * <p>A {@code Network} built by this class has the following default properties: 31 * 32 * <ul> 33 * <li>does not allow parallel edges 34 * <li>does not allow self-loops 35 * <li>orders {@link Network#nodes()} and {@link Network#edges()} in the order in which the 36 * elements were added (insertion order) 37 * </ul> 38 * 39 * <p>{@code Network}s built by this class also guarantee that each collection-returning accessor 40 * returns a <b>(live) unmodifiable view</b>; see <a 41 * href="https://github.com/google/guava/wiki/GraphsExplained#accessor-behavior">the external 42 * documentation</a> for details. 43 * 44 * <p>Examples of use: 45 * 46 * <pre>{@code 47 * // Building a mutable network 48 * MutableNetwork<String, Integer> network = 49 * NetworkBuilder.directed().allowsParallelEdges(true).build(); 50 * flightNetwork.addEdge("LAX", "ATL", 3025); 51 * flightNetwork.addEdge("LAX", "ATL", 1598); 52 * flightNetwork.addEdge("ATL", "LAX", 2450); 53 * 54 * // Building a immutable network 55 * ImmutableNetwork<String, Integer> immutableNetwork = 56 * NetworkBuilder.directed() 57 * .allowsParallelEdges(true) 58 * .<String, Integer>immutable() 59 * .addEdge("LAX", "ATL", 3025) 60 * .addEdge("LAX", "ATL", 1598) 61 * .addEdge("ATL", "LAX", 2450) 62 * .build(); 63 * }</pre> 64 * 65 * @author James Sexton 66 * @author Joshua O'Madadhain 67 * @param <N> The most general node type this builder will support. This is normally {@code Object} 68 * unless it is constrained by using a method like {@link #nodeOrder}, or the builder is 69 * constructed based on an existing {@code Network} using {@link #from(Network)}. 70 * @param <E> The most general edge type this builder will support. This is normally {@code Object} 71 * unless it is constrained by using a method like {@link #edgeOrder}, or the builder is 72 * constructed based on an existing {@code Network} using {@link #from(Network)}. 73 * @since 20.0 74 */ 75 @Beta 76 @ElementTypesAreNonnullByDefault 77 public final class NetworkBuilder<N, E> extends AbstractGraphBuilder<N> { 78 boolean allowsParallelEdges = false; 79 ElementOrder<? super E> edgeOrder = ElementOrder.insertion(); 80 Optional<Integer> expectedEdgeCount = Optional.absent(); 81 82 /** Creates a new instance with the specified edge directionality. */ NetworkBuilder(boolean directed)83 private NetworkBuilder(boolean directed) { 84 super(directed); 85 } 86 87 /** Returns a {@link NetworkBuilder} for building directed networks. */ directed()88 public static NetworkBuilder<Object, Object> directed() { 89 return new NetworkBuilder<>(true); 90 } 91 92 /** Returns a {@link NetworkBuilder} for building undirected networks. */ undirected()93 public static NetworkBuilder<Object, Object> undirected() { 94 return new NetworkBuilder<>(false); 95 } 96 97 /** 98 * Returns a {@link NetworkBuilder} initialized with all properties queryable from {@code 99 * network}. 100 * 101 * <p>The "queryable" properties are those that are exposed through the {@link Network} interface, 102 * such as {@link Network#isDirected()}. Other properties, such as {@link 103 * #expectedNodeCount(int)}, are not set in the new builder. 104 */ from(Network<N, E> network)105 public static <N, E> NetworkBuilder<N, E> from(Network<N, E> network) { 106 return new NetworkBuilder<N, E>(network.isDirected()) 107 .allowsParallelEdges(network.allowsParallelEdges()) 108 .allowsSelfLoops(network.allowsSelfLoops()) 109 .nodeOrder(network.nodeOrder()) 110 .edgeOrder(network.edgeOrder()); 111 } 112 113 /** 114 * Returns an {@link ImmutableNetwork.Builder} with the properties of this {@link NetworkBuilder}. 115 * 116 * <p>The returned builder can be used for populating an {@link ImmutableNetwork}. 117 * 118 * @since 28.0 119 */ immutable()120 public <N1 extends N, E1 extends E> ImmutableNetwork.Builder<N1, E1> immutable() { 121 NetworkBuilder<N1, E1> castBuilder = cast(); 122 return new ImmutableNetwork.Builder<>(castBuilder); 123 } 124 125 /** 126 * Specifies whether the network will allow parallel edges. Attempting to add a parallel edge to a 127 * network that does not allow them will throw an {@link UnsupportedOperationException}. 128 * 129 * <p>The default value is {@code false}. 130 */ 131 @CanIgnoreReturnValue allowsParallelEdges(boolean allowsParallelEdges)132 public NetworkBuilder<N, E> allowsParallelEdges(boolean allowsParallelEdges) { 133 this.allowsParallelEdges = allowsParallelEdges; 134 return this; 135 } 136 137 /** 138 * Specifies whether the network will allow self-loops (edges that connect a node to itself). 139 * Attempting to add a self-loop to a network that does not allow them will throw an {@link 140 * UnsupportedOperationException}. 141 * 142 * <p>The default value is {@code false}. 143 */ 144 @CanIgnoreReturnValue allowsSelfLoops(boolean allowsSelfLoops)145 public NetworkBuilder<N, E> allowsSelfLoops(boolean allowsSelfLoops) { 146 this.allowsSelfLoops = allowsSelfLoops; 147 return this; 148 } 149 150 /** 151 * Specifies the expected number of nodes in the network. 152 * 153 * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 154 */ 155 @CanIgnoreReturnValue expectedNodeCount(int expectedNodeCount)156 public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) { 157 this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 158 return this; 159 } 160 161 /** 162 * Specifies the expected number of edges in the network. 163 * 164 * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative 165 */ 166 @CanIgnoreReturnValue expectedEdgeCount(int expectedEdgeCount)167 public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) { 168 this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount)); 169 return this; 170 } 171 172 /** 173 * Specifies the order of iteration for the elements of {@link Network#nodes()}. 174 * 175 * <p>The default value is {@link ElementOrder#insertion() insertion order}. 176 */ nodeOrder(ElementOrder<N1> nodeOrder)177 public <N1 extends N> NetworkBuilder<N1, E> nodeOrder(ElementOrder<N1> nodeOrder) { 178 NetworkBuilder<N1, E> newBuilder = cast(); 179 newBuilder.nodeOrder = checkNotNull(nodeOrder); 180 return newBuilder; 181 } 182 183 /** 184 * Specifies the order of iteration for the elements of {@link Network#edges()}. 185 * 186 * <p>The default value is {@link ElementOrder#insertion() insertion order}. 187 */ edgeOrder(ElementOrder<E1> edgeOrder)188 public <E1 extends E> NetworkBuilder<N, E1> edgeOrder(ElementOrder<E1> edgeOrder) { 189 NetworkBuilder<N, E1> newBuilder = cast(); 190 newBuilder.edgeOrder = checkNotNull(edgeOrder); 191 return newBuilder; 192 } 193 194 /** Returns an empty {@link MutableNetwork} with the properties of this {@link NetworkBuilder}. */ build()195 public <N1 extends N, E1 extends E> MutableNetwork<N1, E1> build() { 196 return new StandardMutableNetwork<>(this); 197 } 198 199 @SuppressWarnings("unchecked") cast()200 private <N1 extends N, E1 extends E> NetworkBuilder<N1, E1> cast() { 201 return (NetworkBuilder<N1, E1>) this; 202 } 203 } 204