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