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 * @param <N> The most general node type this builder will support. This is normally {@code Object}
048 *     unless it is constrained by using a method like {@link #nodeOrder}, or the builder is
049 *     constructed based on an existing {@code ValueGraph} using {@link #from(ValueGraph)}.
050 * @param <V> The most general value type this builder will support. This is normally {@code Object}
051 *     unless the builder is constructed based on an existing {@code Graph} using {@link
052 *     #from(ValueGraph)}.
053 * @since 20.0
054 */
055@Beta
056public final class ValueGraphBuilder<N, V> extends AbstractGraphBuilder<N> {
057
058  /** Creates a new instance with the specified edge directionality. */
059  private ValueGraphBuilder(boolean directed) {
060    super(directed);
061  }
062
063  /** Returns a {@link ValueGraphBuilder} for building directed graphs. */
064  public static ValueGraphBuilder<Object, Object> directed() {
065    return new ValueGraphBuilder<>(true);
066  }
067
068  /** Returns a {@link ValueGraphBuilder} for building undirected graphs. */
069  public static ValueGraphBuilder<Object, Object> undirected() {
070    return new ValueGraphBuilder<>(false);
071  }
072
073  /**
074   * Returns a {@link ValueGraphBuilder} initialized with all properties queryable from {@code
075   * graph}.
076   *
077   * <p>The "queryable" properties are those that are exposed through the {@link ValueGraph}
078   * interface, such as {@link ValueGraph#isDirected()}. Other properties, such as {@link
079   * #expectedNodeCount(int)}, are not set in the new builder.
080   */
081  public static <N, V> ValueGraphBuilder<N, V> from(ValueGraph<N, V> graph) {
082    return new ValueGraphBuilder<N, V>(graph.isDirected())
083        .allowsSelfLoops(graph.allowsSelfLoops())
084        .nodeOrder(graph.nodeOrder());
085  }
086
087  /**
088   * Specifies whether the graph will allow self-loops (edges that connect a node to itself).
089   * Attempting to add a self-loop to a graph that does not allow them will throw an {@link
090   * UnsupportedOperationException}.
091   */
092  public ValueGraphBuilder<N, V> allowsSelfLoops(boolean allowsSelfLoops) {
093    this.allowsSelfLoops = allowsSelfLoops;
094    return this;
095  }
096
097  /**
098   * Specifies the expected number of nodes in the graph.
099   *
100   * @throws IllegalArgumentException if {@code expectedNodeCount} is negative
101   */
102  public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) {
103    this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount));
104    return this;
105  }
106
107  /** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */
108  public <N1 extends N> ValueGraphBuilder<N1, V> nodeOrder(ElementOrder<N1> nodeOrder) {
109    ValueGraphBuilder<N1, V> newBuilder = cast();
110    newBuilder.nodeOrder = checkNotNull(nodeOrder);
111    return newBuilder;
112  }
113
114  /**
115   * Returns an empty {@link MutableValueGraph} with the properties of this {@link
116   * ValueGraphBuilder}.
117   */
118  public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build() {
119    return new ConfigurableMutableValueGraph<>(this);
120  }
121
122  @SuppressWarnings("unchecked")
123  private <N1 extends N, V1 extends V> ValueGraphBuilder<N1, V1> cast() {
124    return (ValueGraphBuilder<N1, V1>) this;
125  }
126}