• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base.Preconditions.checkNotNull;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Iterators;
23 import com.google.common.collect.UnmodifiableIterator;
24 import java.util.AbstractSet;
25 import java.util.Map;
26 import org.checkerframework.checker.nullness.qual.Nullable;
27 
28 /**
29  * A class to represent the set of edges connecting an (implicit) origin node to a target node.
30  *
31  * <p>The {@link #nodeToOutEdge} map means this class only works on networks without parallel edges.
32  * See {@link MultiEdgesConnecting} for a class that works with parallel edges.
33  *
34  * @author James Sexton
35  * @param <E> Edge parameter type
36  */
37 final class EdgesConnecting<E> extends AbstractSet<E> {
38 
39   private final Map<?, E> nodeToOutEdge;
40   private final Object targetNode;
41 
EdgesConnecting(Map<?, E> nodeToEdgeMap, Object targetNode)42   EdgesConnecting(Map<?, E> nodeToEdgeMap, Object targetNode) {
43     this.nodeToOutEdge = checkNotNull(nodeToEdgeMap);
44     this.targetNode = checkNotNull(targetNode);
45   }
46 
47   @Override
iterator()48   public UnmodifiableIterator<E> iterator() {
49     E connectingEdge = getConnectingEdge();
50     return (connectingEdge == null)
51         ? ImmutableSet.<E>of().iterator()
52         : Iterators.singletonIterator(connectingEdge);
53   }
54 
55   @Override
size()56   public int size() {
57     return getConnectingEdge() == null ? 0 : 1;
58   }
59 
60   @Override
contains(@ullable Object edge)61   public boolean contains(@Nullable Object edge) {
62     E connectingEdge = getConnectingEdge();
63     return (connectingEdge != null && connectingEdge.equals(edge));
64   }
65 
getConnectingEdge()66   private @Nullable E getConnectingEdge() {
67     return nodeToOutEdge.get(targetNode);
68   }
69 }
70