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 MutableGraph} with user-defined properties. 027 * 028 * <p>A graph built by this class will have the following properties by default: 029 * 030 * <ul> 031 * <li>does not allow self-loops 032 * <li>orders {@link Graph#nodes()} in the order in which the elements were added 033 * </ul> 034 * 035 * <p>Example of use: 036 * 037 * <pre>{@code 038 * MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build(); 039 * graph.putEdge("bread", "bread"); 040 * graph.putEdge("chocolate", "peanut butter"); 041 * graph.putEdge("peanut butter", "jelly"); 042 * }</pre> 043 * 044 * @author James Sexton 045 * @author Joshua O'Madadhain 046 * @since 20.0 047 */ 048@Beta 049public final class GraphBuilder<N> extends AbstractGraphBuilder<N> { 050 051 /** Creates a new instance with the specified edge directionality. */ 052 private GraphBuilder(boolean directed) { 053 super(directed); 054 } 055 056 /** Returns a {@link GraphBuilder} for building directed graphs. */ 057 public static GraphBuilder<Object> directed() { 058 return new GraphBuilder<Object>(true); 059 } 060 061 /** Returns a {@link GraphBuilder} for building undirected graphs. */ 062 public static GraphBuilder<Object> undirected() { 063 return new GraphBuilder<Object>(false); 064 } 065 066 /** 067 * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. 068 * 069 * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, 070 * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, 071 * are not set in the new builder. 072 */ 073 public static <N> GraphBuilder<N> from(Graph<N> graph) { 074 return new GraphBuilder<Object>(graph.isDirected()) 075 .allowsSelfLoops(graph.allowsSelfLoops()) 076 .nodeOrder(graph.nodeOrder()); 077 } 078 079 /** 080 * Specifies whether the graph will allow self-loops (edges that connect a node to itself). 081 * Attempting to add a self-loop to a graph that does not allow them will throw an {@link 082 * UnsupportedOperationException}. 083 */ 084 public GraphBuilder<N> allowsSelfLoops(boolean allowsSelfLoops) { 085 this.allowsSelfLoops = allowsSelfLoops; 086 return this; 087 } 088 089 /** 090 * Specifies the expected number of nodes in the graph. 091 * 092 * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 093 */ 094 public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { 095 this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 096 return this; 097 } 098 099 /** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ 100 public <N1 extends N> GraphBuilder<N1> nodeOrder(ElementOrder<N1> nodeOrder) { 101 GraphBuilder<N1> newBuilder = cast(); 102 newBuilder.nodeOrder = checkNotNull(nodeOrder); 103 return newBuilder; 104 } 105 106 /** Returns an empty {@link MutableGraph} with the properties of this {@link GraphBuilder}. */ 107 public <N1 extends N> MutableGraph<N1> build() { 108 return new ConfigurableMutableGraph<N1>(this); 109 } 110 111 @SuppressWarnings("unchecked") 112 private <N1 extends N> GraphBuilder<N1> cast() { 113 return (GraphBuilder<N1>) this; 114 } 115}