View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.graph;
18  
19  import com.google.common.graph.GraphConstants.Presence;
20  
21  /**
22   * Configurable implementation of {@link MutableGraph} that supports both directed and undirected
23   * graphs. Instances of this class should be constructed with {@link GraphBuilder}.
24   *
25   * <p>Time complexities for mutation methods are all O(1) except for {@code removeNode(N node)},
26   * which is in O(d_node) where d_node is the degree of {@code node}.
27   *
28   * @author James Sexton
29   * @param <N> Node parameter type
30   */
31  final class ConfigurableMutableGraph<N> extends ForwardingGraph<N> implements MutableGraph<N> {
32    private final MutableValueGraph<N, Presence> backingValueGraph;
33  
34    /** Constructs a {@link MutableGraph} with the properties specified in {@code builder}. */
35    ConfigurableMutableGraph(AbstractGraphBuilder<? super N> builder) {
36      this.backingValueGraph = new ConfigurableMutableValueGraph<>(builder);
37    }
38  
39    @Override
40    protected BaseGraph<N> delegate() {
41      return backingValueGraph;
42    }
43  
44    @Override
45    public boolean addNode(N node) {
46      return backingValueGraph.addNode(node);
47    }
48  
49    @Override
50    public boolean putEdge(N nodeU, N nodeV) {
51      return backingValueGraph.putEdgeValue(nodeU, nodeV, Presence.EDGE_EXISTS) == null;
52    }
53  
54    @Override
55    public boolean removeNode(N node) {
56      return backingValueGraph.removeNode(node);
57    }
58  
59    @Override
60    public boolean removeEdge(N nodeU, N nodeV) {
61      return backingValueGraph.removeEdge(nodeU, nodeV) != null;
62    }
63  }