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