001    package org.apache.commons.graph.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import org.apache.commons.graph.DirectedGraph;
028    import org.apache.commons.graph.Edge;
029    import org.apache.commons.graph.Vertex;
030    
031    /**
032     * A memory-based implementation of a mutable directed Graph.
033     *
034     * @param <V> the Graph vertices type
035     * @param <E> the Graph edges type
036     */
037    public class DirectedMutableGraph<V extends Vertex, E extends Edge<V>>
038        extends BaseMutableGraph<V, E>
039        implements DirectedGraph<V, E>
040    {
041    
042        private final Map<V, Set<E>> inbound = new HashMap<V, Set<E>>();
043    
044        /**
045         * {@inheritDoc}
046         */
047        public Set<E> getInbound( V v )
048        {
049            return inbound.get( v );
050        }
051    
052        /**
053         * {@inheritDoc}
054         */
055        public Set<E> getOutbound( V v )
056        {
057            return getAdjacencyList().get( v );
058        }
059    
060        /**
061         * {@inheritDoc}
062         */
063        @Override
064        protected void decorateAddVertex( V v )
065        {
066            inbound.put( v, new HashSet<E>() );
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        @Override
073        protected void decorateRemoveVertex( V v )
074        {
075            inbound.remove( v );
076        }
077    
078        /**
079         * {@inheritDoc}
080         */
081        @Override
082        protected void decorateAddEdge( E e )
083        {
084            inbound.get( e.getTail() ).add( e );
085        }
086    
087        /**
088         * {@inheritDoc}
089         */
090        @Override
091        protected void decorateRemoveEdge( E e )
092        {
093            inbound.get( e.getTail() ).remove( e );
094        }
095    
096    }