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 static java.util.Collections.unmodifiableSet;
023    
024    import java.util.HashMap;
025    import java.util.HashSet;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import org.apache.commons.graph.Edge;
030    import org.apache.commons.graph.Graph;
031    import org.apache.commons.graph.Vertex;
032    
033    /**
034     * Basic abstract in-memory based of a simple read-only {@link Graph} implementation.
035     *
036     * Subclasses may load adjacency list/edges set in the constructor,
037     * or expose {@link org.apache.commons.graph.MutableGraph} APIs.
038     *
039     * @param <V> the Graph vertices type
040     * @param <E> the Graph edges type
041     */
042    public abstract class BaseGraph<V extends Vertex, E extends Edge<V>>
043        implements Graph<V, E>
044    {
045    
046        private final Map<V, Set<E>> adjacencyList = new HashMap<V, Set<E>>();
047    
048        private final Set<E> allEdges = new HashSet<E>();
049    
050        /**
051         * {@inheritDoc}
052         */
053        public final Set<V> getVertices()
054        {
055            return unmodifiableSet( adjacencyList.keySet() );
056        }
057    
058        /**
059         * {@inheritDoc}
060         */
061        public final Set<E> getEdges()
062        {
063            return unmodifiableSet( allEdges );
064        }
065    
066        /**
067         * {@inheritDoc}
068         */
069        public final Set<E> getEdges( V v )
070        {
071            return unmodifiableSet( adjacencyList.get( v ) );
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        public final Set<V> getVertices( E e )
078        {
079            Set<V> vertices = new HashSet<V>();
080    
081            vertices.add( e.getHead() );
082            vertices.add( e.getTail() );
083    
084            return unmodifiableSet( vertices );
085        }
086    
087        /**
088         * Returns the adjacency list where stored vertex/edges.
089         *
090         * @return the adjacency list where stored vertex/edges.
091         */
092        protected final Map<V, Set<E>> getAdjacencyList()
093        {
094            return adjacencyList;
095        }
096    
097        /**
098         * Returns the set with all Graph edges.
099         *
100         * @return the set with all Graph edges.
101         */
102        protected final Set<E> getAllEdges()
103        {
104            return allEdges;
105        }
106    
107    }