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 MutableValueGraph} 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 * MutableValueGraph<String, Double> graph = 039 * ValueGraphBuilder.undirected().allowsSelfLoops(true).build(); 040 * graph.putEdgeValue("San Francisco", "San Francisco", 0.0); 041 * graph.putEdgeValue("San Jose", "San Jose", 0.0); 042 * graph.putEdgeValue("San Francisco", "San Jose", 48.4); 043 * }</pre> 044 * 045 * @author James Sexton 046 * @author Joshua O'Madadhain 047 * @since 20.0 048 */ 049@Beta 050public final class ValueGraphBuilder<N, V> extends AbstractGraphBuilder<N> { 051 052 /** Creates a new instance with the specified edge directionality. */ 053 private ValueGraphBuilder(boolean directed) { 054 super(directed); 055 } 056 057 /** Returns a {@link ValueGraphBuilder} for building directed graphs. */ 058 public static ValueGraphBuilder<Object, Object> directed() { 059 return new ValueGraphBuilder<Object, Object>(true); 060 } 061 062 /** Returns a {@link ValueGraphBuilder} for building undirected graphs. */ 063 public static ValueGraphBuilder<Object, Object> undirected() { 064 return new ValueGraphBuilder<Object, Object>(false); 065 } 066 067 /** 068 * Returns a {@link ValueGraphBuilder} initialized with all properties queryable from {@code 069 * graph}. 070 * 071 * <p>The "queryable" properties are those that are exposed through the {@link ValueGraph} 072 * interface, such as {@link ValueGraph#isDirected()}. Other properties, such as {@link 073 * #expectedNodeCount(int)}, are not set in the new builder. 074 */ 075 public static <N> ValueGraphBuilder<N, Object> from(ValueGraph<N, ?> graph) { 076 return new ValueGraphBuilder<N, Object>(graph.isDirected()) 077 .allowsSelfLoops(graph.allowsSelfLoops()) 078 .nodeOrder(graph.nodeOrder()); 079 } 080 081 /** 082 * Specifies whether the graph will allow self-loops (edges that connect a node to itself). 083 * Attempting to add a self-loop to a graph that does not allow them will throw an {@link 084 * UnsupportedOperationException}. 085 */ 086 public ValueGraphBuilder<N, V> allowsSelfLoops(boolean allowsSelfLoops) { 087 this.allowsSelfLoops = allowsSelfLoops; 088 return this; 089 } 090 091 /** 092 * Specifies the expected number of nodes in the graph. 093 * 094 * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 095 */ 096 public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) { 097 this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 098 return this; 099 } 100 101 /** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ 102 public <N1 extends N> ValueGraphBuilder<N1, V> nodeOrder(ElementOrder<N1> nodeOrder) { 103 ValueGraphBuilder<N1, V> newBuilder = cast(); 104 newBuilder.nodeOrder = checkNotNull(nodeOrder); 105 return newBuilder; 106 } 107 108 /** 109 * Returns an empty {@link MutableValueGraph} with the properties of this {@link 110 * ValueGraphBuilder}. 111 */ 112 public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build() { 113 return new ConfigurableMutableValueGraph<N1, V1>(this); 114 } 115 116 @SuppressWarnings("unchecked") 117 private <N1 extends N, V1 extends V> ValueGraphBuilder<N1, V1> cast() { 118 return (ValueGraphBuilder<N1, V1>) this; 119 } 120}