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.annotations.GwtIncompatible; 024import com.google.common.base.Optional; 025 026/** 027 * A builder for constructing instances of {@link MutableNetwork} with user-defined properties. 028 * 029 * <p>A network built by this class will have the following properties by default: 030 * 031 * <ul> 032 * <li>does not allow parallel edges 033 * <li>does not allow self-loops 034 * <li>orders {@link Network#nodes()} and {@link Network#edges()} in the order in which the 035 * elements were added 036 * </ul> 037 * 038 * <p>Example of use: 039 * 040 * <pre>{@code 041 * MutableNetwork<String, Integer> flightNetwork = 042 * NetworkBuilder.directed().allowsParallelEdges(true).build(); 043 * flightNetwork.addEdge("LAX", "ATL", 3025); 044 * flightNetwork.addEdge("LAX", "ATL", 1598); 045 * flightNetwork.addEdge("ATL", "LAX", 2450); 046 * }</pre> 047 * 048 * @author James Sexton 049 * @author Joshua O'Madadhain 050 * @since 20.0 051 */ 052@Beta 053@GwtIncompatible 054public final class NetworkBuilder<N, E> extends AbstractGraphBuilder<N> { 055 boolean allowsParallelEdges = false; 056 ElementOrder<? super E> edgeOrder = ElementOrder.insertion(); 057 Optional<Integer> expectedEdgeCount = Optional.absent(); 058 059 /** Creates a new instance with the specified edge directionality. */ 060 private NetworkBuilder(boolean directed) { 061 super(directed); 062 } 063 064 /** Returns a {@link NetworkBuilder} for building directed networks. */ 065 public static NetworkBuilder<Object, Object> directed() { 066 return new NetworkBuilder<Object, Object>(true); 067 } 068 069 /** Returns a {@link NetworkBuilder} for building undirected networks. */ 070 public static NetworkBuilder<Object, Object> undirected() { 071 return new NetworkBuilder<Object, Object>(false); 072 } 073 074 /** 075 * Returns a {@link NetworkBuilder} initialized with all properties queryable from {@code 076 * network}. 077 * 078 * <p>The "queryable" properties are those that are exposed through the {@link Network} interface, 079 * such as {@link Network#isDirected()}. Other properties, such as {@link 080 * #expectedNodeCount(int)}, are not set in the new builder. 081 */ 082 public static <N, E> NetworkBuilder<N, E> from(Network<N, E> network) { 083 return new NetworkBuilder<N, E>(network.isDirected()) 084 .allowsParallelEdges(network.allowsParallelEdges()) 085 .allowsSelfLoops(network.allowsSelfLoops()) 086 .nodeOrder(network.nodeOrder()) 087 .edgeOrder(network.edgeOrder()); 088 } 089 090 /** 091 * Specifies whether the network will allow parallel edges. Attempting to add a parallel edge to a 092 * network that does not allow them will throw an {@link UnsupportedOperationException}. 093 */ 094 public NetworkBuilder<N, E> allowsParallelEdges(boolean allowsParallelEdges) { 095 this.allowsParallelEdges = allowsParallelEdges; 096 return this; 097 } 098 099 /** 100 * Specifies whether the network will allow self-loops (edges that connect a node to itself). 101 * Attempting to add a self-loop to a network that does not allow them will throw an {@link 102 * UnsupportedOperationException}. 103 */ 104 public NetworkBuilder<N, E> allowsSelfLoops(boolean allowsSelfLoops) { 105 this.allowsSelfLoops = allowsSelfLoops; 106 return this; 107 } 108 109 /** 110 * Specifies the expected number of nodes in the network. 111 * 112 * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 113 */ 114 public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) { 115 this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 116 return this; 117 } 118 119 /** 120 * Specifies the expected number of edges in the network. 121 * 122 * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative 123 */ 124 public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) { 125 this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount)); 126 return this; 127 } 128 129 /** Specifies the order of iteration for the elements of {@link Network#nodes()}. */ 130 public <N1 extends N> NetworkBuilder<N1, E> nodeOrder(ElementOrder<N1> nodeOrder) { 131 NetworkBuilder<N1, E> newBuilder = cast(); 132 newBuilder.nodeOrder = checkNotNull(nodeOrder); 133 return newBuilder; 134 } 135 136 /** Specifies the order of iteration for the elements of {@link Network#edges()}. */ 137 public <E1 extends E> NetworkBuilder<N, E1> edgeOrder(ElementOrder<E1> edgeOrder) { 138 NetworkBuilder<N, E1> newBuilder = cast(); 139 newBuilder.edgeOrder = checkNotNull(edgeOrder); 140 return newBuilder; 141 } 142 143 /** Returns an empty {@link MutableNetwork} with the properties of this {@link NetworkBuilder}. */ 144 public <N1 extends N, E1 extends E> MutableNetwork<N1, E1> build() { 145 return new ConfigurableMutableNetwork<N1, E1>(this); 146 } 147 148 @SuppressWarnings("unchecked") 149 private <N1 extends N, E1 extends E> NetworkBuilder<N1, E1> cast() { 150 return (NetworkBuilder<N1, E1>) this; 151 } 152}