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

com.google.common.graph.UndirectedMultiNetworkConnections Maven / Gradle / Ivy

/*
 * 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.INNER_CAPACITY;
import static com.google.common.graph.GraphConstants.INNER_LOAD_FACTOR;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multiset;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
 * An implementation of {@link NetworkConnections} for undirected networks with parallel edges.
 *
 * @author James Sexton
 * @param  Node parameter type
 * @param  Edge parameter type
 */
final class UndirectedMultiNetworkConnections
    extends AbstractUndirectedNetworkConnections {

  private UndirectedMultiNetworkConnections(Map incidentEdges) {
    super(incidentEdges);
  }

  static  UndirectedMultiNetworkConnections of() {
    return new UndirectedMultiNetworkConnections<>(
        new HashMap(INNER_CAPACITY, INNER_LOAD_FACTOR));
  }

  static  UndirectedMultiNetworkConnections ofImmutable(Map incidentEdges) {
    return new UndirectedMultiNetworkConnections<>(ImmutableMap.copyOf(incidentEdges));
  }

  @LazyInit private transient Reference> adjacentNodesReference;

  @Override
  public Set adjacentNodes() {
    return Collections.unmodifiableSet(adjacentNodesMultiset().elementSet());
  }

  private Multiset adjacentNodesMultiset() {
    Multiset adjacentNodes = getReference(adjacentNodesReference);
    if (adjacentNodes == null) {
      adjacentNodes = HashMultiset.create(incidentEdgeMap.values());
      adjacentNodesReference = new SoftReference<>(adjacentNodes);
    }
    return adjacentNodes;
  }

  @Override
  public Set edgesConnecting(final N node) {
    return new MultiEdgesConnecting(incidentEdgeMap, node) {
      @Override
      public int size() {
        return adjacentNodesMultiset().count(node);
      }
    };
  }

  @Override
  public N removeInEdge(E edge, boolean isSelfLoop) {
    if (!isSelfLoop) {
      return removeOutEdge(edge);
    }
    return null;
  }

  @Override
  public N removeOutEdge(E edge) {
    N node = super.removeOutEdge(edge);
    Multiset adjacentNodes = getReference(adjacentNodesReference);
    if (adjacentNodes != null) {
      checkState(adjacentNodes.remove(node));
    }
    return node;
  }

  @Override
  public void addInEdge(E edge, N node, boolean isSelfLoop) {
    if (!isSelfLoop) {
      addOutEdge(edge, node);
    }
  }

  @Override
  public void addOutEdge(E edge, N node) {
    super.addOutEdge(edge, node);
    Multiset adjacentNodes = getReference(adjacentNodesReference);
    if (adjacentNodes != null) {
      checkState(adjacentNodes.add(node));
    }
  }

  @NullableDecl
  private static  T getReference(@NullableDecl Reference reference) {
    return (reference == null) ? null : reference.get();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy