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