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