commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1136173 - in /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model: BaseGraph.java BaseMutableGraph.java
Date Wed, 15 Jun 2011 20:20:59 GMT
Author: simonetripodi
Date: Wed Jun 15 20:20:59 2011
New Revision: 1136173

URL: http://svn.apache.org/viewvc?rev=1136173&view=rev
Log:
first checkin of BaseMutableGraph (it partially behaves like a directed Graph since populates
the adjacency list)
checkEdge() method moved in the BaseMutableGraph, not needed in the BaseGraph

Added:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
  (with props)
Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java?rev=1136173&r1=1136172&r2=1136173&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
(original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
Wed Jun 15 20:20:59 2011
@@ -19,7 +19,6 @@ package org.apache.commons.graph.model;
  * under the License.
  */
 
-import static java.lang.String.format;
 import static java.util.Collections.unmodifiableSet;
 
 import java.util.HashMap;
@@ -29,7 +28,6 @@ import java.util.Set;
 
 import org.apache.commons.graph.Edge;
 import org.apache.commons.graph.Graph;
-import org.apache.commons.graph.GraphException;
 import org.apache.commons.graph.Vertex;
 
 /**
@@ -106,21 +104,4 @@ public abstract class BaseGraph<V extend
         return allEdges;
     }
 
-    /**
-     * Utility method to check if Vertices in the given Edge are present in the Graph.
-     *
-     * @param e the Edge which Vertices have to be checked
-     */
-    protected final void checkEdge( E e )
-    {
-        if ( !adjacencyList.containsKey( e.getHead() ) )
-        {
-            throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getHead()
) );
-        }
-        if ( !adjacencyList.containsKey( e.getTail() ) )
-        {
-            throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getTail()
) );
-        }
-    }
-
 }

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java?rev=1136173&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
(added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
Wed Jun 15 20:20:59 2011
@@ -0,0 +1,150 @@
+package org.apache.commons.graph.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static java.lang.String.format;
+
+import java.util.HashSet;
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.GraphException;
+import org.apache.commons.graph.MutableGraph;
+import org.apache.commons.graph.Vertex;
+
+/**
+ * Basic abstract in-memory based of a simple mutable {@link Graph} implementation.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ */
+public abstract class BaseMutableGraph<V extends Vertex, E extends Edge<V>>
+    extends BaseGraph<V, E>
+    implements MutableGraph<V, E>
+{
+
+    /**
+     * {@inheritDoc}
+     */
+    public void addVertex( V v )
+    {
+        if ( v == null )
+        {
+            throw new GraphException( "Impossible to add a null Vertex to the Graph" );
+        }
+
+        if ( getAdjacencyList().containsKey( v ) ){
+            throw new GraphException( format( "Vertex '%s' already present in the Graph",
v ) );
+        }
+
+        getAdjacencyList().put( v, new HashSet<E>() );
+
+        decorateAddVertex( v );
+    }
+
+    /**
+     * 
+     *
+     * @param v
+     */
+    protected abstract void decorateAddVertex( V v );
+
+    /**
+     * {@inheritDoc}
+     */
+    public void removeVertex( V v )
+    {
+        if ( v == null )
+        {
+            throw new GraphException( "Impossible to remove a null Vertex from the Graph"
);
+        }
+
+        if ( !getAdjacencyList().containsKey( v ) ){
+            throw new GraphException( format( "Vertex '%s' not present in the Graph", v )
);
+        }
+
+        getAdjacencyList().remove( v );
+
+        decorateRemoveVertex( v );
+    }
+
+    /**
+     * 
+     *
+     * @param v
+     */
+    protected abstract void decorateRemoveVertex( V v );
+
+    /**
+     * {@inheritDoc}
+     */
+    public void addEdge( E e )
+    {
+        checkEdge( e );
+
+        getAdjacencyList().get( e.getHead() ).add( e );
+
+        decorateAddEdge( e );
+    }
+
+    /**
+     * 
+     *
+     * @param e
+     */
+    protected abstract void decorateAddEdge( E e );
+
+    /**
+     * {@inheritDoc}
+     */
+    public void removeEdge( E e )
+    {
+        checkEdge( e );
+
+        getAdjacencyList().get( e.getHead() ).remove( e );
+
+        decorateRemoveEdge( e );
+    }
+
+    /**
+     * 
+     *
+     * @param e
+     */
+    protected abstract void decorateRemoveEdge( E e );
+
+    /**
+     * Utility method to check if Vertices in the given Edge are present in the Graph.
+     *
+     * @param e the Edge which Vertices have to be checked
+     */
+    private final void checkEdge( E e )
+    {
+        if ( !getAdjacencyList().containsKey( e.getHead() ) )
+        {
+            throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getHead()
) );
+        }
+        if ( !getAdjacencyList().containsKey( e.getTail() ) )
+        {
+            throw new GraphException( format( "Vertex '%s' not present in the Graph", e.getTail()
) );
+        }
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message