001/*
002 * Copyright (C) 2016 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 static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.graph.GraphConstants.EDGE_REMOVED_FROM_GRAPH;
022import static com.google.common.graph.GraphConstants.ENDPOINTS_MISMATCH;
023import static com.google.common.graph.GraphConstants.MULTIPLE_EDGES_CONNECTING;
024import static com.google.common.graph.GraphConstants.NODE_PAIR_REMOVED_FROM_GRAPH;
025import static com.google.common.graph.GraphConstants.NODE_REMOVED_FROM_GRAPH;
026import static java.util.Collections.unmodifiableSet;
027
028import com.google.common.annotations.Beta;
029import com.google.common.base.Predicate;
030import com.google.common.collect.ImmutableSet;
031import com.google.common.collect.Iterators;
032import com.google.common.collect.Maps;
033import com.google.common.collect.Sets;
034import com.google.common.math.IntMath;
035import java.util.AbstractSet;
036import java.util.Iterator;
037import java.util.Map;
038import java.util.Set;
039import org.jspecify.annotations.Nullable;
040
041/**
042 * This class provides a skeletal implementation of {@link Network}. It is recommended to extend
043 * this class rather than implement {@link Network} directly.
044 *
045 * <p>The methods implemented in this class should not be overridden unless the subclass admits a
046 * more efficient implementation.
047 *
048 * @author James Sexton
049 * @param <N> Node parameter type
050 * @param <E> Edge parameter type
051 * @since 20.0
052 */
053@Beta
054public abstract class AbstractNetwork<N, E> implements Network<N, E> {
055  /** Constructor for use by subclasses. */
056  public AbstractNetwork() {}
057
058  @Override
059  public Graph<N> asGraph() {
060    return new AbstractGraph<N>() {
061      @Override
062      public Set<N> nodes() {
063        return AbstractNetwork.this.nodes();
064      }
065
066      @Override
067      public Set<EndpointPair<N>> edges() {
068        if (allowsParallelEdges()) {
069          return super.edges(); // Defer to AbstractGraph implementation.
070        }
071
072        // Optimized implementation assumes no parallel edges (1:1 edge to EndpointPair mapping).
073        return new AbstractSet<EndpointPair<N>>() {
074          @Override
075          public Iterator<EndpointPair<N>> iterator() {
076            return Iterators.transform(
077                AbstractNetwork.this.edges().iterator(), edge -> incidentNodes(edge));
078          }
079
080          @Override
081          public int size() {
082            return AbstractNetwork.this.edges().size();
083          }
084
085          // Mostly safe: We check contains(u) before calling successors(u), so we perform unsafe
086          // operations only in weird cases like checking for an EndpointPair<ArrayList> in a
087          // Network<LinkedList>.
088          @SuppressWarnings("unchecked")
089          @Override
090          public boolean contains(@Nullable Object obj) {
091            if (!(obj instanceof EndpointPair)) {
092              return false;
093            }
094            EndpointPair<?> endpointPair = (EndpointPair<?>) obj;
095            return isOrderingCompatible(endpointPair)
096                && nodes().contains(endpointPair.nodeU())
097                && successors((N) endpointPair.nodeU()).contains(endpointPair.nodeV());
098          }
099        };
100      }
101
102      @Override
103      public ElementOrder<N> nodeOrder() {
104        return AbstractNetwork.this.nodeOrder();
105      }
106
107      @Override
108      public ElementOrder<N> incidentEdgeOrder() {
109        // TODO(b/142723300): Return AbstractNetwork.this.incidentEdgeOrder() once Network has that
110        //   method.
111        return ElementOrder.unordered();
112      }
113
114      @Override
115      public boolean isDirected() {
116        return AbstractNetwork.this.isDirected();
117      }
118
119      @Override
120      public boolean allowsSelfLoops() {
121        return AbstractNetwork.this.allowsSelfLoops();
122      }
123
124      @Override
125      public Set<N> adjacentNodes(N node) {
126        return AbstractNetwork.this.adjacentNodes(node);
127      }
128
129      @Override
130      public Set<N> predecessors(N node) {
131        return AbstractNetwork.this.predecessors(node);
132      }
133
134      @Override
135      public Set<N> successors(N node) {
136        return AbstractNetwork.this.successors(node);
137      }
138
139      // DO NOT override the AbstractGraph *degree() implementations.
140    };
141  }
142
143  @Override
144  public int degree(N node) {
145    if (isDirected()) {
146      return IntMath.saturatedAdd(inEdges(node).size(), outEdges(node).size());
147    } else {
148      return IntMath.saturatedAdd(incidentEdges(node).size(), edgesConnecting(node, node).size());
149    }
150  }
151
152  @Override
153  public int inDegree(N node) {
154    return isDirected() ? inEdges(node).size() : degree(node);
155  }
156
157  @Override
158  public int outDegree(N node) {
159    return isDirected() ? outEdges(node).size() : degree(node);
160  }
161
162  @Override
163  public Set<E> adjacentEdges(E edge) {
164    EndpointPair<N> endpointPair = incidentNodes(edge); // Verifies that edge is in this network.
165    Set<E> endpointPairIncidentEdges =
166        Sets.union(incidentEdges(endpointPair.nodeU()), incidentEdges(endpointPair.nodeV()));
167    return edgeInvalidatableSet(
168        Sets.difference(endpointPairIncidentEdges, ImmutableSet.of(edge)), edge);
169  }
170
171  @Override
172  public Set<E> edgesConnecting(N nodeU, N nodeV) {
173    Set<E> outEdgesU = outEdges(nodeU);
174    Set<E> inEdgesV = inEdges(nodeV);
175    return nodePairInvalidatableSet(
176        outEdgesU.size() <= inEdgesV.size()
177            ? unmodifiableSet(Sets.filter(outEdgesU, connectedPredicate(nodeU, nodeV)))
178            : unmodifiableSet(Sets.filter(inEdgesV, connectedPredicate(nodeV, nodeU))),
179        nodeU,
180        nodeV);
181  }
182
183  @Override
184  public Set<E> edgesConnecting(EndpointPair<N> endpoints) {
185    validateEndpoints(endpoints);
186    return edgesConnecting(endpoints.nodeU(), endpoints.nodeV());
187  }
188
189  private Predicate<E> connectedPredicate(final N nodePresent, final N nodeToCheck) {
190    return new Predicate<E>() {
191      @Override
192      public boolean apply(E edge) {
193        return incidentNodes(edge).adjacentNode(nodePresent).equals(nodeToCheck);
194      }
195    };
196  }
197
198  @Override
199  public @Nullable E edgeConnectingOrNull(N nodeU, N nodeV) {
200    Set<E> edgesConnecting = edgesConnecting(nodeU, nodeV);
201    switch (edgesConnecting.size()) {
202      case 0:
203        return null;
204      case 1:
205        return edgesConnecting.iterator().next();
206      default:
207        throw new IllegalArgumentException(String.format(MULTIPLE_EDGES_CONNECTING, nodeU, nodeV));
208    }
209  }
210
211  @Override
212  public @Nullable E edgeConnectingOrNull(EndpointPair<N> endpoints) {
213    validateEndpoints(endpoints);
214    return edgeConnectingOrNull(endpoints.nodeU(), endpoints.nodeV());
215  }
216
217  @Override
218  public boolean hasEdgeConnecting(N nodeU, N nodeV) {
219    checkNotNull(nodeU);
220    checkNotNull(nodeV);
221    return nodes().contains(nodeU) && successors(nodeU).contains(nodeV);
222  }
223
224  @Override
225  public boolean hasEdgeConnecting(EndpointPair<N> endpoints) {
226    checkNotNull(endpoints);
227    if (!isOrderingCompatible(endpoints)) {
228      return false;
229    }
230    return hasEdgeConnecting(endpoints.nodeU(), endpoints.nodeV());
231  }
232
233  /**
234   * Throws an IllegalArgumentException if the ordering of {@code endpoints} is not compatible with
235   * the directionality of this graph.
236   */
237  protected final void validateEndpoints(EndpointPair<?> endpoints) {
238    checkNotNull(endpoints);
239    checkArgument(isOrderingCompatible(endpoints), ENDPOINTS_MISMATCH);
240  }
241
242  protected final boolean isOrderingCompatible(EndpointPair<?> endpoints) {
243    return endpoints.isOrdered() == this.isDirected();
244  }
245
246  @Override
247  public final boolean equals(@Nullable Object obj) {
248    if (obj == this) {
249      return true;
250    }
251    if (!(obj instanceof Network)) {
252      return false;
253    }
254    Network<?, ?> other = (Network<?, ?>) obj;
255
256    return isDirected() == other.isDirected()
257        && nodes().equals(other.nodes())
258        && edgeIncidentNodesMap(this).equals(edgeIncidentNodesMap(other));
259  }
260
261  @Override
262  public final int hashCode() {
263    return edgeIncidentNodesMap(this).hashCode();
264  }
265
266  /** Returns a string representation of this network. */
267  @Override
268  public String toString() {
269    return "isDirected: "
270        + isDirected()
271        + ", allowsParallelEdges: "
272        + allowsParallelEdges()
273        + ", allowsSelfLoops: "
274        + allowsSelfLoops()
275        + ", nodes: "
276        + nodes()
277        + ", edges: "
278        + edgeIncidentNodesMap(this);
279  }
280
281  /**
282   * Returns a {@link Set} whose methods throw {@link IllegalStateException} when the given edge is
283   * not present in this network.
284   *
285   * @since 33.1.0
286   */
287  protected final <T> Set<T> edgeInvalidatableSet(Set<T> set, E edge) {
288    return InvalidatableSet.of(
289        set, () -> edges().contains(edge), () -> String.format(EDGE_REMOVED_FROM_GRAPH, edge));
290  }
291
292  /**
293   * Returns a {@link Set} whose methods throw {@link IllegalStateException} when the given node is
294   * not present in this network.
295   *
296   * @since 33.1.0
297   */
298  protected final <T> Set<T> nodeInvalidatableSet(Set<T> set, N node) {
299    return InvalidatableSet.of(
300        set, () -> nodes().contains(node), () -> String.format(NODE_REMOVED_FROM_GRAPH, node));
301  }
302
303  /**
304   * Returns a {@link Set} whose methods throw {@link IllegalStateException} when either of the
305   * given nodes is not present in this network.
306   *
307   * @since 33.1.0
308   */
309  protected final <T> Set<T> nodePairInvalidatableSet(Set<T> set, N nodeU, N nodeV) {
310    return InvalidatableSet.of(
311        set,
312        () -> nodes().contains(nodeU) && nodes().contains(nodeV),
313        () -> String.format(NODE_PAIR_REMOVED_FROM_GRAPH, nodeU, nodeV));
314  }
315
316  private static <N, E> Map<E, EndpointPair<N>> edgeIncidentNodesMap(final Network<N, E> network) {
317    return Maps.asMap(network.edges(), network::incidentNodes);
318  }
319}