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;
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 */
031@Beta
032public interface MutableValueGraph<N, V> extends ValueGraph<N, V> {
033
034  /**
035   * Adds {@code node} if it is not already present.
036   *
037   * <p><b>Nodes must be unique</b>, just as {@code Map} keys must be. They must also be non-null.
038   *
039   * @return {@code true} if the graph was modified as a result of this call
040   */
041  @CanIgnoreReturnValue
042  boolean addNode(N node);
043
044  /**
045   * Adds an edge connecting {@code nodeU} to {@code nodeV} if one is not already present; associate
046   * that edge with {@code value}. In an undirected graph, the edge will also connect {@code nodeV}
047   * to {@code nodeU}.
048   *
049   * <p>Values do not have to be unique. However, values must be non-null.
050   *
051   * <p>If {@code nodeU} and {@code nodeV} are not already present in this graph, this method
052   * will silently {@link #addNode(Object) add} {@code nodeU} and {@code nodeV} to the graph.
053   *
054   * @return the value previously associated with the edge connecting {@code nodeU} to {@code
055   *     nodeV}, or null if there was no such edge.
056   * @throws IllegalArgumentException if the introduction of the edge would violate {@link
057   *     #allowsSelfLoops()}
058   */
059  @CanIgnoreReturnValue
060  V putEdgeValue(N nodeU, N nodeV, V value);
061
062  /**
063   * Removes {@code node} if it is present; all edges incident to {@code node} will also be removed.
064   *
065   * @return {@code true} if the graph was modified as a result of this call
066   */
067  @CanIgnoreReturnValue
068  boolean removeNode(N node);
069
070  /**
071   * Removes the edge connecting {@code nodeU} to {@code nodeV}, if it is present.
072   *
073   * @return the value previously associated with the edge connecting {@code nodeU} to {@code
074   *     nodeV}, or null if there was no such edge.
075   */
076  @CanIgnoreReturnValue
077  V removeEdge(N nodeU, N nodeV);
078}