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