/*
 * Copyright (C) 2016 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.graph;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.graph.GraphConstants.GRAPH_STRING_FORMAT;

import com.google.common.annotations.Beta;
import com.google.common.collect.UnmodifiableIterator;
import com.google.common.math.IntMath;
import com.google.common.primitives.Ints;
import java.util.AbstractSet;
import java.util.Set;
import javax.annotation.Nullable;

/**
 * This class provides a skeletal implementation of {@link Graph}. It is recommended to extend this
 * class rather than implement {@link Graph} directly.
 *
 * @author James Sexton
 * @param <N> Node parameter type
 * @since 20.0
 */
@Beta
public abstract class AbstractGraph<N> implements Graph<N> {

  /**
   * Returns the number of edges in this graph; used to calculate the size of {@link #edges()}. The
   * default implementation is O(|N|). You can manually keep track of the number of edges and
   * override this method for better performance.
   */
  protected long edgeCount() {
    long degreeSum = 0L;
    for (N node : nodes()) {
      degreeSum += degree(node);
    }
    // According to the degree sum formula, this is equal to twice the number of edges.
    checkState((degreeSum & 1) == 0);
    return degreeSum >>> 1;
  }

  /**
   * A reasonable default implementation of {@link Graph#edges()} defined in terms of {@link
   * #nodes()} and {@link #successors(Object)}.
   */
  @Override
  public Set<EndpointPair<N>> edges() {
    return new GraphEdges();
  }

  @Override
  public int degree(Object node) {
    if (isDirected()) {
      return IntMath.saturatedAdd(predecessors(node).size(), successors(node).size());
    } else {
      Set<N> neighbors = adjacentNodes(node);
      int selfLoopCount = (allowsSelfLoops() && neighbors.contains(node)) ? 1 : 0;
      return IntMath.saturatedAdd(neighbors.size(), selfLoopCount);
    }
  }

  @Override
  public int inDegree(Object node) {
    return isDirected() ? predecessors(node).size() : degree(node);
  }

  @Override
  public int outDegree(Object node) {
    return isDirected() ? successors(node).size() : degree(node);
  }

  /** Returns a string representation of this graph. */
  @Override
  public String toString() {
    String propertiesString =
        String.format("isDirected: %s, allowsSelfLoops: %s", isDirected(), allowsSelfLoops());
    return String.format(GRAPH_STRING_FORMAT, propertiesString, nodes(), edges());
  }

  private class GraphEdges extends AbstractSet<EndpointPair<N>> {
    @Override
    public UnmodifiableIterator<EndpointPair<N>> iterator() {
      return EndpointPairIterator.of(AbstractGraph.this);
    }

    @Override
    public int size() {
      return Ints.saturatedCast(edgeCount());
    }

    @Override
    public boolean contains(@Nullable Object obj) {
      if (!(obj instanceof EndpointPair)) {
        return false;
      }
      EndpointPair<?> endpointPair = (EndpointPair<?>) obj;
      return isDirected() == endpointPair.isOrdered()
          && contains(endpointPair.nodeU(), endpointPair.nodeV());
    }

    /** Returns true iff this set contains an edge connecting {@code nodeU} to {@code nodeV}. */
    private boolean contains(@Nullable Object nodeU, @Nullable Object nodeV) {
      return nodes().contains(nodeU) && successors(nodeU).contains(nodeV);
    }
  }
}

@@ -2103,20 +2103,704 @@
 new 
-GraphEdges()
+AbstractSet%3CEndpointPair%3CN%3E%3E() %7B%0A      @Override%0A      public UnmodifiableIterator%3CEndpointPair%3CN%3E%3E iterator() %7B%0A        return EndpointPairIterator.of(AbstractGraph.this);%0A      %7D%0A%0A      @Override%0A      public int size() %7B%0A        return Ints.saturatedCast(edgeCount());%0A      %7D%0A%0A      @Override%0A      public boolean contains(@Nullable Object obj) %7B%0A        if (!(obj instanceof EndpointPair)) %7B%0A          return false;%0A        %7D%0A        EndpointPair%3C?%3E endpointPair = (EndpointPair%3C?%3E) obj;%0A        return isDirected() == endpointPair.isOrdered()%0A            && nodes().contains(endpointPair.nodeU())%0A            && successors(endpointPair.nodeU()).contains(endpointPair.nodeV());%0A      %7D%0A    %7D
 ;%0A  
@@ -3736,895 +3736,7 @@
 %0A  %7D
-%0A%0A  private class GraphEdges extends AbstractSet%3CEndpointPair%3CN%3E%3E %7B%0A    @Override%0A    public UnmodifiableIterator%3CEndpointPair%3CN%3E%3E iterator() %7B%0A      return EndpointPairIterator.of(AbstractGraph.this);%0A    %7D%0A%0A    @Override%0A    public int size() %7B%0A      return Ints.saturatedCast(edgeCount());%0A    %7D%0A%0A    @Override%0A    public boolean contains(@Nullable Object obj) %7B%0A      if (!(obj instanceof EndpointPair)) %7B%0A        return false;%0A      %7D%0A      EndpointPair%3C?%3E endpointPair = (EndpointPair%3C?%3E) obj;%0A      return isDirected() == endpointPair.isOrdered()%0A          && contains(endpointPair.nodeU(), endpointPair.nodeV());%0A    %7D%0A%0A    /** Returns true iff this set contains an edge connecting %7B@code nodeU%7D to %7B@code nodeV%7D. */%0A    private boolean contains(@Nullable Object nodeU, @Nullable Object nodeV) %7B%0A      return nodes().contains(nodeU) && successors(nodeU).contains(nodeV);%0A    %7D%0A  %7D
 %0A%7D%0A

Use an anonymous class for AbstractGraph.edges() This was changed recently ([] because of some planned changes we ended up going a different direction with, so now I think it makes more sense inlined.