You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/01/23 15:12:35 UTC

svn commit: r1234806 - in /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph: ./ export/

Author: simonetripodi
Date: Mon Jan 23 14:12:34 2012
New Revision: 1234806

URL: http://svn.apache.org/viewvc?rev=1234806&view=rev
Log:
export APIs moved to fluent APIs

Added:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java   (with props)
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java   (with props)
Removed:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/CommonsGraph.java
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ToStreamBuilder.java

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/CommonsGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/CommonsGraph.java?rev=1234806&r1=1234805&r2=1234806&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/CommonsGraph.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/CommonsGraph.java Mon Jan 23 14:12:34 2012
@@ -24,6 +24,7 @@ import static org.apache.commons.graph.u
 import org.apache.commons.graph.builder.DefaultLinkedConnectionBuilder;
 import org.apache.commons.graph.builder.GraphConnection;
 import org.apache.commons.graph.builder.LinkedConnectionBuilder;
+import org.apache.commons.graph.export.DefaultToStreamBuilder;
 import org.apache.commons.graph.export.ToStreamBuilder;
 import org.apache.commons.graph.model.DirectedMutableGraph;
 import org.apache.commons.graph.model.DirectedMutableWeightedGraph;
@@ -41,7 +42,7 @@ public final class CommonsGraph<V extend
     public static <V extends Vertex, E extends Edge, G extends Graph<V, E>> ToStreamBuilder<V, E, G> export( G graph )
     {
         graph = checkNotNull( graph, "Null graph can not be exported" );
-        return null;
+        return new DefaultToStreamBuilder<V, E, G>( graph );
     }
 
     /**

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java?rev=1234806&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java Mon Jan 23 14:12:34 2012
@@ -0,0 +1,156 @@
+package org.apache.commons.graph.export;
+
+/*
+ * 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 java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.Date;
+
+import org.apache.commons.graph.DirectedGraph;
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Labeled;
+import org.apache.commons.graph.Vertex;
+import org.apache.commons.graph.VertexPair;
+import org.apache.commons.graph.WeightedEdge;
+
+final class DefaultNamedEsportSelector<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+    implements NamedExportSelctor<V, E, G>
+{
+
+    private static final String GRAPH = "graph";
+
+    private static final String DIGRAPH = "digraph";
+
+    private static final String CONNECTOR = "--";
+
+    private static final String DICONNECTOR = "->";
+
+    private final G graph;
+
+    private final Writer writer;
+
+    private final String name;
+
+    public DefaultNamedEsportSelector( G graph, Writer writer )
+    {
+        this( graph, writer, null );
+    }
+
+    public DefaultNamedEsportSelector( G graph, Writer writer, String name )
+    {
+        this.graph = graph;
+        this.writer = writer;
+        this.name = name;
+    }
+
+    public void usingDotNotation()
+    {
+        String graphDeclaration;
+        String connector;
+
+        if ( graph instanceof DirectedGraph )
+        {
+            graphDeclaration = DIGRAPH;
+            connector = DICONNECTOR;
+        }
+        else
+        {
+            graphDeclaration = GRAPH;
+            connector = CONNECTOR;
+        }
+
+        PrintWriter printWriter = new PrintWriter( writer );
+        try
+        {
+            printWriter.format( "# Graph generated by Apache Commons Graph on %s%n", new Date() );
+            printWriter.format( "%s %s {%n", graphDeclaration, name != null ? name : 'G' );
+
+            for ( V vertex : graph.getVertices() )
+            {
+                printWriter.format( "  %s [label=\"%s\"];%n",
+                                    vertex.hashCode(),
+                                    ( vertex instanceof Labeled) ? ( (Labeled) vertex).getLabel() : vertex.toString() );
+            }
+
+            for ( E edge : graph.getEdges() )
+            {
+                VertexPair<V> vertexPair = graph.getVertices( edge );
+
+                printWriter.format( "  %s %s %s",
+                                    vertexPair.getHead().hashCode(),
+                                    connector,
+                                    vertexPair.getTail().hashCode() );
+
+                boolean attributesFound = false;
+
+                if ( edge instanceof Labeled )
+                {
+                    attributesFound = true;
+                    printWriter.format( " [label=\"%s\"", ( (Labeled) edge ).getLabel() );
+                }
+                if ( edge instanceof WeightedEdge )
+                {
+                    Object weight = ( (WeightedEdge<?>) edge ).getWeight();
+
+                    if ( weight instanceof Number )
+                    {
+                        printWriter.format( " " );
+
+                        if ( !attributesFound )
+                        {
+                            printWriter.format( "[" );
+                            attributesFound = true;
+                        }
+
+                        printWriter.format( "weight=%f", ( (Number) weight ).doubleValue() );
+                    }
+                }
+
+                if ( attributesFound )
+                {
+                    printWriter.format( "]" );
+                }
+
+                printWriter.format( "%n" );
+            }
+
+            printWriter.write( '}' );
+        }
+        finally
+        {
+            try
+            {
+                writer.close();
+            }
+            catch ( IOException e )
+            {
+                // swallow it
+            }
+        }
+    }
+
+    public ExportSelctor<V, E, G> withName( String name )
+    {
+        return new DefaultNamedEsportSelector<V, E, G>( graph, writer, name );
+    }
+
+}

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

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

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

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java?rev=1234806&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java Mon Jan 23 14:12:34 2012
@@ -0,0 +1,69 @@
+package org.apache.commons.graph.export;
+
+/*
+ * 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 org.apache.commons.graph.utils.Assertions.checkNotNull;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Vertex;
+
+public final class DefaultToStreamBuilder<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+    implements ToStreamBuilder<V, E, G>
+{
+
+    private final G graph;
+
+    public DefaultToStreamBuilder( G graph )
+    {
+        this.graph = graph;
+    }
+
+    public NamedExportSelctor<V, E, G> to( File outputFile )
+    {
+        try
+        {
+            return to( new FileOutputStream( checkNotNull( outputFile, "Impossibe to export the graph in a null file" ) ) );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    public NamedExportSelctor<V, E, G> to( OutputStream outputStream )
+    {
+        return to( new OutputStreamWriter( checkNotNull( outputStream, "Impossibe to export the graph in a null stream" ) ) );
+    }
+
+    public NamedExportSelctor<V, E, G> to( Writer writer )
+    {
+        writer = checkNotNull( writer, "Impossibe to export the graph in a null stream" );
+        return new DefaultNamedEsportSelector<V, E, G>( graph, writer );
+    }
+
+}

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

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

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

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java?rev=1234806&r1=1234805&r2=1234806&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java Mon Jan 23 14:12:34 2012
@@ -26,6 +26,9 @@ import org.apache.commons.graph.Vertex;
 public interface ExportSelctor<V extends Vertex, E extends Edge, G extends Graph<V, E>>
 {
 
+    /**
+     * Export Graphs in <a href="http://en.wikipedia.org/wiki/DOT_language">DOT language</a>.
+     */
     void usingDotNotation();
 
 }

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ToStreamBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ToStreamBuilder.java?rev=1234806&r1=1234805&r2=1234806&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ToStreamBuilder.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ToStreamBuilder.java Mon Jan 23 14:12:34 2012
@@ -21,7 +21,6 @@ package org.apache.commons.graph.export;
 
 import java.io.File;
 import java.io.OutputStream;
-import java.io.PrintStream;
 import java.io.Writer;
 
 import org.apache.commons.graph.Edge;
@@ -31,12 +30,28 @@ import org.apache.commons.graph.Vertex;
 public interface ToStreamBuilder<V extends Vertex, E extends Edge, G extends Graph<V, E>>
 {
 
+    /**
+     * Export the input Graph to a {@link File}.
+     *
+     * @param outputFile the file where exporting the Graph to
+     * @return
+     */
     NamedExportSelctor<V, E, G> to( File outputFile );
 
+    /**
+     * Export the input Graph to an {@link OutputStream}.
+     *
+     * @param outputStream the output stream where exporting the Graph to
+     * @return
+     */
     NamedExportSelctor<V, E, G> to( OutputStream outputStream );
 
+    /**
+     * Export the input Graph to a {@link Writer}.
+     *
+     * @param writer the writer where exporting the Graph to
+     * @return
+     */
     NamedExportSelctor<V, E, G> to( Writer writer );
 
-    NamedExportSelctor<V, E, G> to( PrintStream printStream );
-
 }