incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1179434 - in /incubator/isis/trunk/framework/viewer/json: json-applib/src/main/java/org/apache/isis/viewer/json/applib/ json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/ json-viewer/src/main/java/org/apache/isis...
Date Wed, 05 Oct 2011 20:49:03 GMT
Author: danhaywood
Date: Wed Oct  5 20:49:02 2011
New Revision: 1179434

URL: http://svn.apache.org/viewvc?rev=1179434&view=rev
Log:
ISIS-109: x-ro-follow-links now working correctly for graphs of links (as well as lists of
links)

Added:
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/PathFollowerTest_asGraph.java
Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/PathFollower.java
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/capabilities/CapabilitiesReprRenderer.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java?rev=1179434&r1=1179433&r2=1179434&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
Wed Oct  5 20:49:02 2011
@@ -217,6 +217,9 @@ public abstract class Parser<T> {
 
             @Override
             public List<List<String>> valueOf(String str) {
+                if(str == null || str.isEmpty()) {
+                    return Collections.emptyList();
+                }
                 final Iterable<String> listOfStrings = Splitter.on(',').split(str);
                 return Lists.transform(Lists.newArrayList(listOfStrings), new Function<String,
List<String>>() {
 

Modified: incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/PathFollower.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/PathFollower.java?rev=1179434&r1=1179433&r2=1179434&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/PathFollower.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/representations/PathFollower.java
Wed Oct  5 20:49:02 2011
@@ -1,47 +1,61 @@
 package org.apache.isis.viewer.json.viewer.representations;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
-import com.google.common.base.Objects;
 import com.google.common.collect.Maps;
 
 public final class PathFollower {
 
-    public final static PathFollower initial(List<List<String>> followLinks)
{
-        final Map<String, List<String>> map = Maps.newHashMap();
-        for (List<String> list : followLinks) {
-            int size = list.size();
-            if(size == 0) { continue; }
-            String path = list.get(0);
-            list.remove(0);
-            map.put(path, list);
+    public final static Map<String,Map> asGraph(List<List<String>> links)
{
+        if(links == null) {
+            return Collections.emptyMap();
         }
-        return new PathFollower(map, null, Mode.FINDING_PATH);
+        final Map<String, Map> map = Maps.newHashMap();
+        for (List<String> link : links) {
+            mergeInto(link, map);
+        }
+        return map;
+    }
+
+    private static void mergeInto(List<String> list, Map<String, Map> map) {
+        if(list.size() == 0) {
+            return;
+        }
+        final String str = list.get(0);
+        Map submap = map.get(str);
+        if(submap == null) {
+            submap = Maps.newHashMap(); 
+            map.put(str, submap);
+        }
+        mergeInto(list.subList(1, list.size()), submap);
+    }
+
+    public final static PathFollower initial(List<List<String>> links) {
+        final Map<String, Map> graph = asGraph(links);
+        return new PathFollower(graph, Mode.FOLLOWING_PATH);
     }
 
-    public final static PathFollower following(List<String> links) {
-        return new PathFollower(null, links, Mode.FOLLOWING_PATH);
+    public final static PathFollower following(Map<String,Map> graph) {
+        return new PathFollower(graph, Mode.FOLLOWING_PATH);
     }
 
     private static PathFollower terminated() {
-        return new PathFollower(null, null, Mode.TERMINATED);
+        return new PathFollower(null, Mode.TERMINATED);
     }
 
 
     private enum Mode {
-        FINDING_PATH,
         FOLLOWING_PATH,
         TERMINATED;
     }
 
-    private final Map<String, List<String>> linkCandidatesByPath;
-    private final List<String> links;
+    private final Map<String, Map> graph;
     private Mode mode;
 
-    private PathFollower(Map<String, List<String>> linkCandidatesByPath, List<String>
links, Mode mode) {
-        this.linkCandidatesByPath = linkCandidatesByPath;
-        this.links = links;
+    private PathFollower(Map<String, Map> graph, Mode mode) {
+        this.graph = graph;
         this.mode = mode;
     }
 
@@ -49,30 +63,17 @@ public final class PathFollower {
      * A little algebra...
      */
     public PathFollower follow(String path) {
-        if(path == null) {
+        if(path == null || mode == Mode.TERMINATED) {
             return PathFollower.terminated();
         }
-        if(mode == Mode.FINDING_PATH) {
-            List<String> remaining = linkCandidatesByPath.get(path);
+        if(mode == Mode.FOLLOWING_PATH) {
+            Map remaining = graph.get(path);
             if(remaining != null) {
                 return PathFollower.following(remaining);
             } else {
                 return PathFollower.terminated();
             }
         }
-        if(mode == Mode.FOLLOWING_PATH) {
-            if(links.size() == 0) {
-                return PathFollower.terminated();
-            } else {
-                String firstPath = links.get(0);
-                if(Objects.equal(path, firstPath)) {
-                    List<String> remaining = links.subList(1, links.size());
-                    return PathFollower.following(remaining);
-                } else {
-                    return PathFollower.terminated();
-                }
-            }
-        }
         return PathFollower.terminated();
     }
     

Modified: incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/capabilities/CapabilitiesReprRenderer.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/capabilities/CapabilitiesReprRenderer.java?rev=1179434&r1=1179433&r2=1179434&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/capabilities/CapabilitiesReprRenderer.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/capabilities/CapabilitiesReprRenderer.java
Wed Oct  5 20:49:02 2011
@@ -23,7 +23,6 @@ public class CapabilitiesReprRenderer ex
     
     private CapabilitiesReprRenderer(ResourceContext resourceContext, PathFollower pathFollower,
RepresentationType representationType, JsonRepresentation representation) {
         super(resourceContext, pathFollower, representationType, representation);
-        includesSelf();
     }
 
     @Override

Added: incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/PathFollowerTest_asGraph.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/PathFollowerTest_asGraph.java?rev=1179434&view=auto
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/PathFollowerTest_asGraph.java
(added)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/test/java/org/apache/isis/viewer/json/viewer/representations/PathFollowerTest_asGraph.java
Wed Oct  5 20:49:02 2011
@@ -0,0 +1,57 @@
+package org.apache.isis.viewer.json.viewer.representations;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.isis.viewer.json.applib.Parser;
+import org.junit.Test;
+
+public class PathFollowerTest_asGraph {
+
+
+    @Test
+    public void simple() throws Exception {
+        List<List<String>> links = asListOfLists("a.b.c,a.b.d,d.b,e,e");
+        final Map<String, Map> root = PathFollower.asGraph(links);
+        
+        assertThat(root.size(), is(3));
+        Map<String,Map> nodeA = root.get("a");
+        assertThat(nodeA.size(), is(1));
+        Map<String,Map> nodeAB = nodeA.get("b");
+        assertThat(nodeAB.size(), is(2));
+        Map<String,Map> nodeABC = nodeAB.get("c");
+        assertThat(nodeABC.size(), is(0));
+        Map<String,Map> nodeABD = nodeAB.get("d");
+        assertThat(nodeABD.size(), is(0));
+        
+        Map<String,Map> nodeD = root.get("d");
+        assertThat(nodeD.size(), is(1));
+        Map<String,Map> nodeDB = nodeD.get("b");
+        assertThat(nodeDB.size(), is(0));
+        
+        Map<String,Map> nodeE = root.get("e");
+        assertThat(nodeE.size(), is(0));
+    }
+
+    @Test
+    public void empty() throws Exception {
+        List<List<String>> links = asListOfLists("");
+        final Map<String, Map> root = PathFollower.asGraph(links);
+        
+        assertThat(root.size(), is(0));
+    }
+
+    @Test
+    public void whenNull() throws Exception {
+        final Map<String, Map> root = PathFollower.asGraph(null);
+        
+        assertThat(root.size(), is(0));
+    }
+
+    private List<List<String>> asListOfLists(String string) {
+        return Parser.forListOfListOfStrings().valueOf(string);
+    }
+}



Mime
View raw message