All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.google.common.graph.AbstractNetwork Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2016 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in org.testifyproject.testifyprojectpliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org.testifyproject/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 org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph;

import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph.GraphConstants.GRAPH_STRING_FORMAT;

import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.annotations.Beta;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.base.Function;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.ImmutableSet;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Iterators;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Maps;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Sets;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.math.IntMath;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

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

  @Override
  public Graph asGraph() {
    return new AbstractGraph() {
      @Override
      public Set nodes() {
        return AbstractNetwork.this.nodes();
      }

      @Override
      public Set> edges() {
        if (allowsParallelEdges()) {
          return super.edges(); // Defer to AbstractGraph implementation.
        }

        // Optimized implementation assumes no parallel edges (1:1 edge to EndpointPair mapping).
        return new AbstractSet>() {
          @Override
          public Iterator> iterator() {
            return Iterators.transform(
                AbstractNetwork.this.edges().iterator(),
                new Function>() {
                  @Override
                  public EndpointPair apply(E edge) {
                    return incidentNodes(edge);
                  }
                });
          }

          @Override
          public int size() {
            return AbstractNetwork.this.edges().size();
          }

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

      @Override
      public ElementOrder nodeOrder() {
        return AbstractNetwork.this.nodeOrder();
      }

      @Override
      public boolean isDirected() {
        return AbstractNetwork.this.isDirected();
      }

      @Override
      public boolean allowsSelfLoops() {
        return AbstractNetwork.this.allowsSelfLoops();
      }

      @Override
      public Set adjacentNodes(Object node) {
        return AbstractNetwork.this.adjacentNodes(node);
      }

      @Override
      public Set predecessors(Object node) {
        return AbstractNetwork.this.predecessors(node);
      }

      @Override
      public Set successors(Object node) {
        return AbstractNetwork.this.successors(node);
      }

      // DO NOT override the AbstractGraph *degree() implementations.
    };
  }

  @Override
  public int degree(Object node) {
    if (isDirected()) {
      return IntMath.saturatedAdd(inEdges(node).size(), outEdges(node).size());
    } else {
      return IntMath.saturatedAdd(incidentEdges(node).size(), edgesConnecting(node, node).size());
    }
  }

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

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

  @Override
  public Set adjacentEdges(Object edge) {
    EndpointPair endpointPair = incidentNodes(edge); // Verifies that edge is in this network.
    Set endpointPairIncidentEdges =
        Sets.union(incidentEdges(endpointPair.nodeU()), incidentEdges(endpointPair.nodeV()));
    return Sets.difference(endpointPairIncidentEdges, ImmutableSet.of(edge));
  }

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

  private Map> edgeIncidentNodesMap() {
    Function> edgeToIncidentNodesFn =
        new Function>() {
          @Override
          public EndpointPair apply(E edge) {
            return incidentNodes(edge);
          }
        };
    return Maps.asMap(edges(), edgeToIncidentNodesFn);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy