1 /* 2 * Copyright (C) 2016 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.graph; 18 19 import static com.google.common.graph.GraphConstants.EXPECTED_DEGREE; 20 21 import com.google.common.collect.BiMap; 22 import com.google.common.collect.HashBiMap; 23 import com.google.common.collect.ImmutableBiMap; 24 import java.util.Collections; 25 import java.util.Map; 26 import java.util.Set; 27 28 /** 29 * An implementation of {@link NetworkConnections} for directed networks. 30 * 31 * @author James Sexton 32 * @param <N> Node parameter type 33 * @param <E> Edge parameter type 34 */ 35 final class DirectedNetworkConnections<N, E> extends AbstractDirectedNetworkConnections<N, E> { 36 DirectedNetworkConnections( Map<E, N> inEdgeMap, Map<E, N> outEdgeMap, int selfLoopCount)37 protected DirectedNetworkConnections( 38 Map<E, N> inEdgeMap, Map<E, N> outEdgeMap, int selfLoopCount) { 39 super(inEdgeMap, outEdgeMap, selfLoopCount); 40 } 41 of()42 static <N, E> DirectedNetworkConnections<N, E> of() { 43 return new DirectedNetworkConnections<>( 44 HashBiMap.<E, N>create(EXPECTED_DEGREE), HashBiMap.<E, N>create(EXPECTED_DEGREE), 0); 45 } 46 ofImmutable( Map<E, N> inEdges, Map<E, N> outEdges, int selfLoopCount)47 static <N, E> DirectedNetworkConnections<N, E> ofImmutable( 48 Map<E, N> inEdges, Map<E, N> outEdges, int selfLoopCount) { 49 return new DirectedNetworkConnections<>( 50 ImmutableBiMap.copyOf(inEdges), ImmutableBiMap.copyOf(outEdges), selfLoopCount); 51 } 52 53 @Override predecessors()54 public Set<N> predecessors() { 55 return Collections.unmodifiableSet(((BiMap<E, N>) inEdgeMap).values()); 56 } 57 58 @Override successors()59 public Set<N> successors() { 60 return Collections.unmodifiableSet(((BiMap<E, N>) outEdgeMap).values()); 61 } 62 63 @Override edgesConnecting(N node)64 public Set<E> edgesConnecting(N node) { 65 return new EdgesConnecting<E>(((BiMap<E, N>) outEdgeMap).inverse(), node); 66 } 67 } 68