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} or {@link ImmutableValueGraph} 027 * with user-defined properties. 028 * 029 * <p>A graph built by this class will have the following properties by default: 030 * 031 * <ul> 032 * <li>does not allow self-loops 033 * <li>orders {@link Graph#nodes()} in the order in which the elements were added 034 * </ul> 035 * 036 * <p>Examples of use: 037 * 038 * <pre>{@code 039 * // Building a mutable value graph 040 * MutableValueGraph<String, Double> graph = 041 * ValueGraphBuilder.undirected().allowsSelfLoops(true).build(); 042 * graph.putEdgeValue("San Francisco", "San Francisco", 0.0); 043 * graph.putEdgeValue("San Jose", "San Jose", 0.0); 044 * graph.putEdgeValue("San Francisco", "San Jose", 48.4); 045 * 046 * // Building an immutable value graph 047 * ImmutableValueGraph<String, Double> immutableGraph = 048 * ValueGraphBuilder.undirected() 049 * .allowsSelfLoops(true) 050 * .<String, Double>immutable() 051 * .putEdgeValue("San Francisco", "San Francisco", 0.0) 052 * .putEdgeValue("San Jose", "San Jose", 0.0) 053 * .putEdgeValue("San Francisco", "San Jose", 48.4) 054 * .build(); 055 * }</pre> 056 * 057 * @author James Sexton 058 * @author Joshua O'Madadhain 059 * @param <N> The most general node type this builder will support. This is normally {@code Object} 060 * unless it is constrained by using a method like {@link #nodeOrder}, or the builder is 061 * constructed based on an existing {@code ValueGraph} using {@link #from(ValueGraph)}. 062 * @param <V> The most general value type this builder will support. This is normally {@code Object} 063 * unless the builder is constructed based on an existing {@code Graph} using {@link 064 * #from(ValueGraph)}. 065 * @since 20.0 066 */ 067@Beta 068public final class ValueGraphBuilder<N, V> extends AbstractGraphBuilder<N> { 069 070 /** Creates a new instance with the specified edge directionality. */ 071 private ValueGraphBuilder(boolean directed) { 072 super(directed); 073 } 074 075 /** Returns a {@link ValueGraphBuilder} for building directed graphs. */ 076 public static ValueGraphBuilder<Object, Object> directed() { 077 return new ValueGraphBuilder<>(true); 078 } 079 080 /** Returns a {@link ValueGraphBuilder} for building undirected graphs. */ 081 public static ValueGraphBuilder<Object, Object> undirected() { 082 return new ValueGraphBuilder<>(false); 083 } 084 085 /** 086 * Returns a {@link ValueGraphBuilder} initialized with all properties queryable from {@code 087 * graph}. 088 * 089 * <p>The "queryable" properties are those that are exposed through the {@link ValueGraph} 090 * interface, such as {@link ValueGraph#isDirected()}. Other properties, such as {@link 091 * #expectedNodeCount(int)}, are not set in the new builder. 092 */ 093 public static <N, V> ValueGraphBuilder<N, V> from(ValueGraph<N, V> graph) { 094 return new ValueGraphBuilder<N, V>(graph.isDirected()) 095 .allowsSelfLoops(graph.allowsSelfLoops()) 096 .nodeOrder(graph.nodeOrder()); 097 } 098 099 /** 100 * Returns an {@link ImmutableValueGraph.Builder} with the properties of this {@link 101 * ValueGraphBuilder}. 102 * 103 * <p>The returned builder can be used for populating an {@link ImmutableValueGraph}. 104 * 105 * @since 28.0 106 */ 107 public <N1 extends N, V1 extends V> ImmutableValueGraph.Builder<N1, V1> immutable() { 108 ValueGraphBuilder<N1, V1> castBuilder = cast(); 109 return new ImmutableValueGraph.Builder<>(castBuilder); 110 } 111 112 /** 113 * Specifies whether the graph will allow self-loops (edges that connect a node to itself). 114 * Attempting to add a self-loop to a graph that does not allow them will throw an {@link 115 * UnsupportedOperationException}. 116 */ 117 public ValueGraphBuilder<N, V> allowsSelfLoops(boolean allowsSelfLoops) { 118 this.allowsSelfLoops = allowsSelfLoops; 119 return this; 120 } 121 122 /** 123 * Specifies the expected number of nodes in the graph. 124 * 125 * @throws IllegalArgumentException if {@code expectedNodeCount} is negative 126 */ 127 public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) { 128 this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); 129 return this; 130 } 131 132 /** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ 133 public <N1 extends N> ValueGraphBuilder<N1, V> nodeOrder(ElementOrder<N1> nodeOrder) { 134 ValueGraphBuilder<N1, V> newBuilder = cast(); 135 newBuilder.nodeOrder = checkNotNull(nodeOrder); 136 return newBuilder; 137 } 138 139 /** 140 * Returns an empty {@link MutableValueGraph} with the properties of this {@link 141 * ValueGraphBuilder}. 142 */ 143 public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build() { 144 return new ConfigurableMutableValueGraph<>(this); 145 } 146 147 @SuppressWarnings("unchecked") 148 private <N1 extends N, V1 extends V> ValueGraphBuilder<N1, V1> cast() { 149 return (ValueGraphBuilder<N1, V1>) this; 150 } 151}