View Javadoc

1   package org.apache.commons.graph.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.commons.graph.DirectedGraph;
28  import org.apache.commons.graph.Edge;
29  import org.apache.commons.graph.Vertex;
30  
31  /**
32   * A memory-based implementation of a mutable directed Graph.
33   *
34   * @param <V> the Graph vertices type
35   * @param <E> the Graph edges type
36   */
37  public class DirectedMutableGraph<V extends Vertex, E extends Edge<V>>
38      extends BaseMutableGraph<V, E>
39      implements DirectedGraph<V, E>
40  {
41  
42      private final Map<V, Set<E>> inbound = new HashMap<V, Set<E>>();
43  
44      /**
45       * {@inheritDoc}
46       */
47      public Set<E> getInbound( V v )
48      {
49          return inbound.get( v );
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      public Set<E> getOutbound( V v )
56      {
57          return getAdjacencyList().get( v );
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      protected void decorateAddVertex( V v )
65      {
66          inbound.put( v, new HashSet<E>() );
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      protected void decorateRemoveVertex( V v )
74      {
75          inbound.remove( v );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      protected void decorateAddEdge( E e )
83      {
84          inbound.get( e.getTail() ).add( e );
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      protected void decorateRemoveEdge( E e )
92      {
93          inbound.get( e.getTail() ).remove( e );
94      }
95  
96  }