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