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.lang.String.format;
023
024 import java.util.HashSet;
025
026 import org.apache.commons.graph.Edge;
027 import org.apache.commons.graph.Graph;
028 import org.apache.commons.graph.GraphException;
029 import org.apache.commons.graph.MutableGraph;
030 import org.apache.commons.graph.Vertex;
031
032 /**
033 * Basic abstract in-memory based of a simple mutable {@link Graph} implementation.
034 *
035 * @param <V> the Graph vertices type
036 * @param <E> the Graph edges type
037 */
038 public abstract class BaseMutableGraph<V extends Vertex, E extends Edge<V>>
039 extends BaseGraph<V, E>
040 implements MutableGraph<V, E>
041 {
042
043 /**
044 * {@inheritDoc}
045 */
046 public final void addVertex( V v )
047 {
048 if ( v == null )
049 {
050 throw new GraphException( "Impossible to add a null Vertex to the Graph" );
051 }
052
053 if ( getAdjacencyList().containsKey( v ) ){
054 throw new GraphException( format( "Vertex '%s' already present in the Graph", v ) );
055 }
056
057 getAdjacencyList().put( v, new HashSet<E>() );
058
059 decorateAddVertex( v );
060 }
061
062 /**
063 *
064 *
065 * @param v
066 */
067 protected abstract void decorateAddVertex( V v );
068
069 /**
070 * {@inheritDoc}
071 */
072 public final void removeVertex( V v )
073 {
074 if ( v == null )
075 {
076 throw new GraphException( "Impossible to remove a null Vertex from the Graph" );
077 }
078
079 if ( !getAdjacencyList().containsKey( v ) ){
080 throw new GraphException( format( "Vertex '%s' not present in the Graph", v ) );
081 }
082
083 getAdjacencyList().remove( v );
084
085 decorateRemoveVertex( v );
086 }
087
088 /**
089 *
090 *
091 * @param v
092 */
093 protected abstract void decorateRemoveVertex( V v );
094
095 /**
096 * {@inheritDoc}
097 */
098 public final void addEdge( E e )
099 {
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 }