Coverage Summary for Class: GraphBuilder (com.google.common.graph)

Class Class, % Method, % Line, %
GraphBuilder 0% (0/1) 0% (0/12) 0% (0/30)


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.checkArgument; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 import static com.google.common.graph.Graphs.checkNonNegative; 22  23 import com.google.common.annotations.Beta; 24 import com.google.common.base.Optional; 25 import com.google.errorprone.annotations.DoNotMock; 26  27 /** 28  * A builder for constructing instances of {@link MutableGraph} or {@link ImmutableGraph} with 29  * user-defined properties. 30  * 31  * <p>A graph built by this class will have the following properties by default: 32  * 33  * <ul> 34  * <li>does not allow self-loops 35  * <li>orders {@link Graph#nodes()} in the order in which the elements were added 36  * </ul> 37  * 38  * <p>Examples of use: 39  * 40  * <pre>{@code 41  * // Building a mutable graph 42  * MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build(); 43  * graph.putEdge("bread", "bread"); 44  * graph.putEdge("chocolate", "peanut butter"); 45  * graph.putEdge("peanut butter", "jelly"); 46  * 47  * // Building an immutable graph 48  * ImmutableGraph<String> immutableGraph = 49  * GraphBuilder.undirected() 50  * .allowsSelfLoops(true) 51  * .<String>immutable() 52  * .putEdge("bread", "bread") 53  * .putEdge("chocolate", "peanut butter") 54  * .putEdge("peanut butter", "jelly") 55  * .build(); 56  * }</pre> 57  * 58  * @author James Sexton 59  * @author Joshua O'Madadhain 60  * @param <N> The most general node type this builder will support. This is normally {@code Object} 61  * unless it is constrained by using a method like {@link #nodeOrder}, or the builder is 62  * constructed based on an existing {@code Graph} using {@link #from(Graph)}. 63  * @since 20.0 64  */ 65 @Beta 66 @DoNotMock 67 @ElementTypesAreNonnullByDefault 68 public final class GraphBuilder<N> extends AbstractGraphBuilder<N> { 69  70  /** Creates a new instance with the specified edge directionality. */ 71  private GraphBuilder(boolean directed) { 72  super(directed); 73  } 74  75  /** Returns a {@link GraphBuilder} for building directed graphs. */ 76  public static GraphBuilder<Object> directed() { 77  return new GraphBuilder<>(true); 78  } 79  80  /** Returns a {@link GraphBuilder} for building undirected graphs. */ 81  public static GraphBuilder<Object> undirected() { 82  return new GraphBuilder<>(false); 83  } 84  85  /** 86  * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. 87  * 88  * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, 89  * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, 90  * are not set in the new builder. 91  */ 92  public static <N> GraphBuilder<N> from(Graph<N> graph) { 93  return new GraphBuilder<N>(graph.isDirected()) 94  .allowsSelfLoops(graph.allowsSelfLoops()) 95  .nodeOrder(graph.nodeOrder()) 96  .incidentEdgeOrder(graph.incidentEdgeOrder()); 97  } 98  99  /** 100  * Returns an {@link ImmutableGraph.Builder} with the properties of this {@link GraphBuilder}. 101  * 102  * <p>The returned builder can be used for populating an {@link ImmutableGraph}. 103  * 104  * <p>Note that the returned builder will always have {@link #incidentEdgeOrder} set to {@link 105  * ElementOrder#stable()}, regardless of the value that was set in this builder. 106  * 107  * @since 28.0 108  */ 109  public <N1 extends N> ImmutableGraph.Builder<N1> immutable() { 110  GraphBuilder<N1> castBuilder = cast(); 111  return new ImmutableGraph.Builder<>(castBuilder); 112  } 113  114  /** 115  * Specifies whether the graph will allow self-loops (edges that connect a node to itself). 116  * Attempting to add a self-loop to a graph that does not allow them will throw an {@link 117  * UnsupportedOperationException}. 118  * 119  * <p>The default value is {@code false}. 120  */ 121  public GraphBuilder<N> allowsSelfLoops(boolean allowsSelfLoops) { 122  this.allowsSelfLoops = allowsSelfLoops; 123  return this; 124  } 125  126  /** 127  * Specifies the expected number of nodes in the graph. 128  * 129  * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 130  */ 131  public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { 132  this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 133  return this; 134  } 135  136  /** 137  * Specifies the order of iteration for the elements of {@link Graph#nodes()}. 138  * 139  * <p>The default value is {@link ElementOrder#insertion() insertion order}. 140  */ 141  public <N1 extends N> GraphBuilder<N1> nodeOrder(ElementOrder<N1> nodeOrder) { 142  GraphBuilder<N1> newBuilder = cast(); 143  newBuilder.nodeOrder = checkNotNull(nodeOrder); 144  return newBuilder; 145  } 146  147  /** 148  * Specifies the order of iteration for the elements of {@link Graph#edges()}, {@link 149  * Graph#adjacentNodes(Object)}, {@link Graph#predecessors(Object)}, {@link 150  * Graph#successors(Object)} and {@link Graph#incidentEdges(Object)}. 151  * 152  * <p>The default value is {@link ElementOrder#unordered() unordered} for mutable graphs. For 153  * immutable graphs, this value is ignored; they always have a {@link ElementOrder#stable() 154  * stable} order. 155  * 156  * @throws IllegalArgumentException if {@code incidentEdgeOrder} is not either {@code 157  * ElementOrder.unordered()} or {@code ElementOrder.stable()}. 158  * @since 29.0 159  */ 160  public <N1 extends N> GraphBuilder<N1> incidentEdgeOrder(ElementOrder<N1> incidentEdgeOrder) { 161  checkArgument( 162  incidentEdgeOrder.type() == ElementOrder.Type.UNORDERED 163  || incidentEdgeOrder.type() == ElementOrder.Type.STABLE, 164  "The given elementOrder (%s) is unsupported. incidentEdgeOrder() only supports" 165  + " ElementOrder.unordered() and ElementOrder.stable().", 166  incidentEdgeOrder); 167  GraphBuilder<N1> newBuilder = cast(); 168  newBuilder.incidentEdgeOrder = checkNotNull(incidentEdgeOrder); 169  return newBuilder; 170  } 171  172  /** Returns an empty {@link MutableGraph} with the properties of this {@link GraphBuilder}. */ 173  public <N1 extends N> MutableGraph<N1> build() { 174  return new StandardMutableGraph<N1>(this); 175  } 176  177  GraphBuilder<N> copy() { 178  GraphBuilder<N> newBuilder = new GraphBuilder<>(directed); 179  newBuilder.allowsSelfLoops = allowsSelfLoops; 180  newBuilder.nodeOrder = nodeOrder; 181  newBuilder.expectedNodeCount = expectedNodeCount; 182  newBuilder.incidentEdgeOrder = incidentEdgeOrder; 183  return newBuilder; 184  } 185  186  @SuppressWarnings("unchecked") 187  private <N1 extends N> GraphBuilder<N1> cast() { 188  return (GraphBuilder<N1>) this; 189  } 190 }