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 static java.lang.String.format;
23  
24  import java.util.HashSet;
25  
26  import org.apache.commons.graph.Edge;
27  import org.apache.commons.graph.Graph;
28  import org.apache.commons.graph.GraphException;
29  import org.apache.commons.graph.MutableGraph;
30  import org.apache.commons.graph.Vertex;
31  
32  /**
33   * Basic abstract in-memory based of a simple mutable {@link Graph} implementation.
34   *
35   * @param <V> the Graph vertices type
36   * @param <E> the Graph edges type
37   */
38  public abstract class BaseMutableGraph<V extends Vertex, E extends Edge<V>>
39      extends BaseGraph<V, E>
40      implements MutableGraph<V, E>
41  {
42  
43      /**
44       * {@inheritDoc}
45       */
46      public final void addVertex( V v )
47      {
48          if ( v == null )
49          {
50              throw new GraphException( "Impossible to add a null Vertex to the Graph" );
51          }
52  
53          if ( getAdjacencyList().containsKey( v ) ){
54              throw new GraphException( format( "Vertex '%s' already present in the Graph", v ) );
55          }
56  
57          getAdjacencyList().put( v, new HashSet<E>() );
58  
59          decorateAddVertex( v );
60      }
61  
62      /**
63       * 
64       *
65       * @param v
66       */
67      protected abstract void decorateAddVertex( V v );
68  
69      /**
70       * {@inheritDoc}
71       */
72      public final void removeVertex( V v )
73      {
74          if ( v == null )
75          {
76              throw new GraphException( "Impossible to remove a null Vertex from the Graph" );
77          }
78  
79          if ( !getAdjacencyList().containsKey( v ) ){
80              throw new GraphException( format( "Vertex '%s' not present in the Graph", v ) );
81          }
82  
83          getAdjacencyList().remove( v );
84  
85          decorateRemoveVertex( v );
86      }
87  
88      /**
89       * 
90       *
91       * @param v
92       */
93      protected abstract void decorateRemoveVertex( V v );
94  
95      /**
96       * {@inheritDoc}
97       */
98      public final void addEdge( E e )
99      {
100         checkEdge( e );
101 
102         getAllEdges().add( e );
103         getAdjacencyList().get( e.getHead() ).add( e );
104 
105         decorateAddEdge( e );
106     }
107 
108     /**
109      * 
110      *
111      * @param e
112      */
113     protected abstract void decorateAddEdge( E e );
114 
115     /**
116      * {@inheritDoc}
117      */
118     public final void removeEdge( E e )
119     {
120         checkEdge( e );
121 
122         getAllEdges().remove( e );
123         getAdjacencyList().get( e.getHead() ).remove( e );
124 
125         decorateRemoveEdge( e );
126     }
127 
128     /**
129      * 
130      *
131      * @param e
132      */
133     protected abstract void decorateRemoveEdge( E e );
134 
135     /**
136      * Utility method to check if Vertices in the given Edge are present in the Graph.
137      *
138      * @param e the Edge which Vertices have to be checked
139      */
140     private final void checkEdge( E e )
141     {
142         if ( !getAdjacencyList().containsKey( e.getHead() ) )
143         {
144             throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getHead() ) );
145         }
146         if ( !getAdjacencyList().containsKey( e.getTail() ) )
147         {
148             throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getTail() ) );
149         }
150     }
151 
152 }