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;
021import com.google.errorprone.annotations.CompatibleWith;
022
023/**
024 * A subinterface of {@link Network} which adds mutation methods. When mutation is not required,
025 * users should prefer the {@link Network} interface.
026 *
027 * @author James Sexton
028 * @author Joshua O'Madadhain
029 * @param <N> Node parameter type
030 * @param <E> Edge parameter type
031 * @since 20.0
032 */
033@Beta
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}. In an undirected network, the edge
048   * will also connect {@code nodeV} to {@code nodeU}.
049   *
050   * <p><b>Edges must be unique</b>, just as {@code Map} keys must be. They must also be non-null.
051   *
052   * <p>Behavior if {@code nodeU} and {@code nodeV} are not already present in this network is
053   * implementation-dependent. Suggested behaviors include (a) silently {@link #addNode(Object)
054   * adding} {@code nodeU} and {@code nodeV} to the network (this is the behavior of the default
055   * implementations) or (b) throwing {@code IllegalArgumentException}.
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 and does not connect {@code
062   *     nodeU} to {@code nodeV}, or if the introduction of the edge would violate {@link
063   *     #allowsParallelEdges()} or {@link #allowsSelfLoops()}
064   */
065  @CanIgnoreReturnValue
066  boolean addEdge(N nodeU, N nodeV, E edge);
067
068  /**
069   * Removes {@code node} if it is present; all edges incident to {@code node} will also be removed.
070   *
071   * @return {@code true} if the network was modified as a result of this call
072   */
073  @CanIgnoreReturnValue
074  boolean removeNode(@CompatibleWith("N") Object node);
075
076  /**
077   * Removes {@code edge} from this network, if it is present.
078   *
079   * @return {@code true} if the network was modified as a result of this call
080   */
081  @CanIgnoreReturnValue
082  boolean removeEdge(@CompatibleWith("E") Object edge);
083}