001/* 002 * Copyright (C) 2016 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.graph; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020import static com.google.common.graph.Graphs.checkNonNegative; 021 022import com.google.common.annotations.Beta; 023import com.google.common.base.Optional; 024 025/** 026 * A builder for constructing instances of {@link MutableNetwork} with user-defined properties. 027 * 028 * <p>A network built by this class will have the following properties by default: 029 * 030 * <ul> 031 * <li>does not allow parallel edges 032 * <li>does not allow self-loops 033 * <li>orders {@link Network#nodes()} and {@link Network#edges()} in the order in which the elements 034 * were added 035 * </ul> 036 * 037 * <p>Example of use: 038 * 039 * <pre><code> 040 * MutableNetwork<String, Integer> flightNetwork = 041 * NetworkBuilder.directed().allowsParallelEdges(true).build(); 042 * flightNetwork.addEdge("LAX", "ATL", 3025); 043 * flightNetwork.addEdge("LAX", "ATL", 1598); 044 * flightNetwork.addEdge("ATL", "LAX", 2450); 045 * </code></pre> 046 * 047 * @author James Sexton 048 * @author Joshua O'Madadhain 049 * @since 20.0 050 */ 051@Beta 052public final class NetworkBuilder<N, E> extends AbstractGraphBuilder<N> { 053 boolean allowsParallelEdges = false; 054 ElementOrder<? super E> edgeOrder = ElementOrder.insertion(); 055 Optional<Integer> expectedEdgeCount = Optional.absent(); 056 057 /** Creates a new instance with the specified edge directionality. */ 058 private NetworkBuilder(boolean directed) { 059 super(directed); 060 } 061 062 /** Returns a {@link NetworkBuilder} for building directed networks. */ 063 public static NetworkBuilder<Object, Object> directed() { 064 return new NetworkBuilder<Object, Object>(true); 065 } 066 067 /** Returns a {@link NetworkBuilder} for building undirected networks. */ 068 public static NetworkBuilder<Object, Object> undirected() { 069 return new NetworkBuilder<Object, Object>(false); 070 } 071 072 /** 073 * Returns a {@link NetworkBuilder} initialized with all properties queryable from {@code 074 * network}. 075 * 076 * <p>The "queryable" properties are those that are exposed through the {@link Network} interface, 077 * such as {@link Network#isDirected()}. Other properties, such as {@link 078 * #expectedNodeCount(int)}, are not set in the new builder. 079 */ 080 public static <N, E> NetworkBuilder<N, E> from(Network<N, E> network) { 081 return new NetworkBuilder<Object, Object>(network.isDirected()) 082 .allowsParallelEdges(network.allowsParallelEdges()) 083 .allowsSelfLoops(network.allowsSelfLoops()) 084 .nodeOrder(network.nodeOrder()) 085 .edgeOrder(network.edgeOrder()); 086 } 087 088 /** 089 * Specifies whether the network will allow parallel edges. Attempting to add a parallel edge to a 090 * network that does not allow them will throw an {@link UnsupportedOperationException}. 091 */ 092 public NetworkBuilder<N, E> allowsParallelEdges(boolean allowsParallelEdges) { 093 this.allowsParallelEdges = allowsParallelEdges; 094 return this; 095 } 096 097 /** 098 * Specifies whether the network will allow self-loops (edges that connect a node to itself). 099 * 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<N1, E1>(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}