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 Graph} which adds mutation methods. When mutation is not required, users 025 * should prefer the {@link Graph} interface. 026 * 027 * @author James Sexton 028 * @author Joshua O'Madadhain 029 * @param <N> Node parameter type 030 * @since 20.0 031 */ 032@Beta 033public interface MutableGraph<N> extends Graph<N> { 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 graph was modified as a result of this call 041 */ 042 @CanIgnoreReturnValue 043 boolean addNode(N node); 044 045 /** 046 * Adds an edge connecting {@code nodeU} to {@code nodeV} if one is not already present. In an 047 * undirected graph, the edge will also connect {@code nodeV} to {@code nodeU}. 048 * 049 * <p>Behavior if {@code nodeU} and {@code nodeV} are not already present in this graph is 050 * implementation-dependent. Suggested behaviors include (a) silently {@link #addNode(Object) 051 * adding} {@code nodeU} and {@code nodeV} to the graph (this is the behavior of the default 052 * implementations) or (b) throwing {@code IllegalArgumentException}. 053 * 054 * @return {@code true} if the graph was modified as a result of this call 055 * @throws IllegalArgumentException if the introduction of the edge would violate {@link 056 * #allowsSelfLoops()} 057 */ 058 @CanIgnoreReturnValue 059 boolean putEdge(N nodeU, N nodeV); 060 061 /** 062 * Removes {@code node} if it is present; all edges incident to {@code node} will also be removed. 063 * 064 * @return {@code true} if the graph was modified as a result of this call 065 */ 066 @CanIgnoreReturnValue 067 boolean removeNode(@CompatibleWith("N") Object node); 068 069 /** 070 * Removes the edge connecting {@code nodeU} to {@code nodeV}, if it is present. 071 * 072 * @return {@code true} if the graph was modified as a result of this call 073 */ 074 @CanIgnoreReturnValue 075 boolean removeEdge(@CompatibleWith("N") Object nodeU, @CompatibleWith("N") Object nodeV); 076}