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 com.google.common.annotations.Beta;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import javax.annotation.CheckForNull;
022
023/**
024 * A subinterface of {@link ValueGraph} which adds mutation methods. When mutation is not required,
025 * users should prefer the {@link ValueGraph} interface.
026 *
027 * @author James Sexton
028 * @param <N> Node parameter type
029 * @param <V> Value parameter type
030 * @since 20.0
031 */
032@Beta
033@ElementTypesAreNonnullByDefault
034public interface MutableValueGraph<N, V> extends ValueGraph<N, V> {
035
036  /**
037   * Adds {@code node} if it is not already present.
038   *
039   * <p><b>Nodes must be unique</b>, just as {@code Map} keys must be. They must also be non-null.
040   *
041   * @return {@code true} if the graph was modified as a result of this call
042   */
043  @CanIgnoreReturnValue
044  boolean addNode(N node);
045
046  /**
047   * Adds an edge connecting {@code nodeU} to {@code nodeV} if one is not already present, and sets
048   * a value for that edge to {@code value} (overwriting the existing value, if any).
049   *
050   * <p>If the graph is directed, the resultant edge will be directed; otherwise, it will be
051   * undirected.
052   *
053   * <p>Values do not have to be unique. However, values must be non-null.
054   *
055   * <p>If {@code nodeU} and {@code nodeV} are not already present in this graph, this method will
056   * silently {@link #addNode(Object) add} {@code nodeU} and {@code nodeV} to the graph.
057   *
058   * @return the value previously associated with the edge connecting {@code nodeU} to {@code
059   *     nodeV}, or null if there was no such edge.
060   * @throws IllegalArgumentException if the introduction of the edge would violate {@link
061   *     #allowsSelfLoops()}
062   */
063  @CanIgnoreReturnValue
064  @CheckForNull
065  V putEdgeValue(N nodeU, N nodeV, V value);
066
067  /**
068   * Adds an edge connecting {@code endpoints} if one is not already present, and sets a value for
069   * that edge to {@code value} (overwriting the existing value, if any).
070   *
071   * <p>If the graph is directed, the resultant edge will be directed; otherwise, it will be
072   * undirected.
073   *
074   * <p>If this graph is directed, {@code endpoints} must be ordered.
075   *
076   * <p>Values do not have to be unique. However, values must be non-null.
077   *
078   * <p>If either or both endpoints are not already present in this graph, this method will silently
079   * {@link #addNode(Object) add} each missing endpoint to the graph.
080   *
081   * @return the value previously associated with the edge connecting {@code nodeU} to {@code
082   *     nodeV}, or null if there was no such edge.
083   * @throws IllegalArgumentException if the introduction of the edge would violate {@link
084   *     #allowsSelfLoops()}
085   * @throws IllegalArgumentException if the endpoints are unordered and the graph is directed
086   * @since 27.1
087   */
088  @CanIgnoreReturnValue
089  @CheckForNull
090  V putEdgeValue(EndpointPair<N> endpoints, V value);
091
092  /**
093   * Removes {@code node} if it is present; all edges incident to {@code node} will also be removed.
094   *
095   * @return {@code true} if the graph was modified as a result of this call
096   */
097  @CanIgnoreReturnValue
098  boolean removeNode(N node);
099
100  /**
101   * Removes the edge connecting {@code nodeU} to {@code nodeV}, if it is present.
102   *
103   * @return the value previously associated with the edge connecting {@code nodeU} to {@code
104   *     nodeV}, or null if there was no such edge.
105   */
106  @CanIgnoreReturnValue
107  @CheckForNull
108  V removeEdge(N nodeU, N nodeV);
109
110  /**
111   * Removes the edge connecting {@code endpoints}, if it is present.
112   *
113   * <p>If this graph is directed, {@code endpoints} must be ordered.
114   *
115   * @return the value previously associated with the edge connecting {@code endpoints}, or null if
116   *     there was no such edge.
117   * @since 27.1
118   */
119  @CanIgnoreReturnValue
120  @CheckForNull
121  V removeEdge(EndpointPair<N> endpoints);
122}