org.testifyproject.google.common.graph.DirectedMultiNetworkConnections 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.base.Preconditions.checkState;
import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph.GraphConstants.INNER_CAPACITY;
import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph.GraphConstants.INNER_LOAD_FACTOR;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.HashMultiset;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.ImmutableMap;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Multiset;
import org.testifyproject.testifyproject.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 javax.annotation.Nullable;
/**
* An implementation of {@link NetworkConnections} for directed networks with parallel edges.
*
* @author James Sexton
* @param Node parameter type
* @param Edge parameter type
*/
final class DirectedMultiNetworkConnections extends AbstractDirectedNetworkConnections {
private DirectedMultiNetworkConnections(
Map inEdges, Map outEdges, int selfLoopCount) {
super(inEdges, outEdges, selfLoopCount);
}
static DirectedMultiNetworkConnections of() {
return new DirectedMultiNetworkConnections(
new HashMap(INNER_CAPACITY, INNER_LOAD_FACTOR),
new HashMap(INNER_CAPACITY, INNER_LOAD_FACTOR),
0);
}
static DirectedMultiNetworkConnections ofImmutable(
Map inEdges, Map outEdges, int selfLoopCount) {
return new DirectedMultiNetworkConnections(
ImmutableMap.copyOf(inEdges), ImmutableMap.copyOf(outEdges), selfLoopCount);
}
@LazyInit
private transient Reference> predecessorsReference;
@Override
public Set predecessors() {
return Collections.unmodifiableSet(predecessorsMultiset().elementSet());
}
private Multiset predecessorsMultiset() {
Multiset predecessors = getReference(predecessorsReference);
if (predecessors == null) {
predecessors = HashMultiset.create(inEdgeMap.values());
predecessorsReference = new SoftReference>(predecessors);
}
return predecessors;
}
@LazyInit
private transient Reference> successorsReference;
@Override
public Set successors() {
return Collections.unmodifiableSet(successorsMultiset().elementSet());
}
private Multiset successorsMultiset() {
Multiset successors = getReference(successorsReference);
if (successors == null) {
successors = HashMultiset.create(outEdgeMap.values());
successorsReference = new SoftReference>(successors);
}
return successors;
}
@Override
public Set edgesConnecting(final Object node) {
return new MultiEdgesConnecting(outEdgeMap, node) {
@Override
public int size() {
return successorsMultiset().count(node);
}
};
}
@Override
public N removeInEdge(Object edge, boolean isSelfLoop) {
N node = super.removeInEdge(edge, isSelfLoop);
Multiset predecessors = getReference(predecessorsReference);
if (predecessors != null) {
checkState(predecessors.remove(node));
}
return node;
}
@Override
public N removeOutEdge(Object edge) {
N node = super.removeOutEdge(edge);
Multiset successors = getReference(successorsReference);
if (successors != null) {
checkState(successors.remove(node));
}
return node;
}
@Override
public void addInEdge(E edge, N node, boolean isSelfLoop) {
super.addInEdge(edge, node, isSelfLoop);
Multiset predecessors = getReference(predecessorsReference);
if (predecessors != null) {
checkState(predecessors.add(node));
}
}
@Override
public void addOutEdge(E edge, N node) {
super.addOutEdge(edge, node);
Multiset successors = getReference(successorsReference);
if (successors != null) {
checkState(successors.add(node));
}
}
@Nullable
private static T getReference(@Nullable Reference reference) {
return (reference == null) ? null : reference.get();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy