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}. In an undirected network, the edge
047   * will also connect {@code nodeV} to {@code nodeU}.
048   *
049   * <p><b>Edges must be unique</b>, just as {@code Map} keys must be. They must also be non-null.
050   *
051   * <p>Behavior if {@code nodeU} and {@code nodeV} are not already present in this network is
052   * implementation-dependent. Suggested behaviors include (a) silently {@link #addNode(Object)
053   * adding} {@code nodeU} and {@code nodeV} to the network (this is the behavior of the default
054   * implementations) or (b) throwing {@code IllegalArgumentException}.
055   *
056   * <p>If {@code edge} already connects {@code nodeU} to {@code nodeV} (in the specified order if
057   * this network {@link #isDirected()}, else in any order), then this method will have no effect.
058   *
059   * @return {@code true} if the network was modified as a result of this call
060   * @throws IllegalArgumentException if {@code edge} already exists and does not connect {@code
061   *     nodeU} to {@code nodeV}, or 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   * Removes {@code node} if it is present; all edges incident to {@code node} will also be removed.
069   *
070   * @return {@code true} if the network was modified as a result of this call
071   */
072  @CanIgnoreReturnValue
073  boolean removeNode(N node);
074
075  /**
076   * Removes {@code edge} from this network, if it is present.
077   *
078   * @return {@code true} if the network was modified as a result of this call
079   */
080  @CanIgnoreReturnValue
081  boolean removeEdge(E edge);
082}