Author: julien_viet
Date: 2010-09-12 18:54:07 -0400 (Sun, 12 Sep 2010)
New Revision: 4158
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java
Log:
improve encapsulation of internal route structure
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java 2010-09-12
21:53:26 UTC (rev 4157)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java 2010-09-12
22:54:07 UTC (rev 4158)
@@ -44,15 +44,11 @@
final List<String> chunks;
PatternRoute(
- Route parent,
Pattern pattern,
List<QualifiedName> parameterNames,
List<Pattern> parameterPatterns,
List<String> chunks)
{
- super(parent);
-
- //
if (chunks.size() != parameterNames.size() + 1)
{
throw new AssertionError("Was expecting chunk size " + chunks.size() +
" to be equals to " + parameterNames.size() + 1);
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-09-12
21:53:26 UTC (rev 4157)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-09-12
22:54:07 UTC (rev 4158)
@@ -26,6 +26,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -39,35 +40,20 @@
{
/** . */
- final Route parent;
+ private Route parent;
/** . */
boolean terminal;
/** . */
- final Map<String, List<SimpleRoute>> simpleRoutes;
+ private final Map<String, List<SimpleRoute>> simpleRoutes;
/** Actually here we allow to store several times the same pattern and routing could
be optimized instead. */
- final List<PatternRoute> patternRoutes;
+ private final List<PatternRoute> patternRoutes;
/** . */
- final Map<QualifiedName, String> routeParameters;
+ private final Map<QualifiedName, String> routeParameters;
- protected Route(Route parent)
- {
- if (parent == null)
- {
- throw new NullPointerException("No null parent");
- }
-
- //
- this.parent = parent;
- this.terminal = false;
- this.simpleRoutes = new LinkedHashMap<String, List<SimpleRoute>>();
- this.patternRoutes = new ArrayList<PatternRoute>();
- this.routeParameters = new HashMap<QualifiedName, String>();
- }
-
Route()
{
this.parent = null;
@@ -114,7 +100,7 @@
if (this instanceof SimpleRoute)
{
SimpleRoute sr = (SimpleRoute)this;
- sb.append('/').append(sr.value);
+ sb.append('/').append(sr.name);
}
else if (this instanceof PatternRoute)
{
@@ -345,6 +331,68 @@
return route;
}
+ <R extends Route> R add(R route)
+ {
+ if (route.parent != null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ if (route instanceof SimpleRoute)
+ {
+ SimpleRoute segment = (SimpleRoute)route;
+ List<SimpleRoute> routes = simpleRoutes.get(segment.name);
+ if (routes == null)
+ {
+ routes = new ArrayList<SimpleRoute>();
+ simpleRoutes.put(segment.name, routes);
+ }
+ routes.add(segment);
+ ((Route)segment).parent = this;
+ return (R)segment;
+ }
+ else if (route instanceof PatternRoute)
+ {
+ PatternRoute pattern = (PatternRoute)route;
+
+ patternRoutes.add(pattern);
+ route.parent = this;
+ return (R)pattern;
+ }
+ else
+ {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ Set<String> getSegmentNames()
+ {
+ return simpleRoutes.keySet();
+ }
+
+ int getSegmentSize(String segmentName)
+ {
+ List<SimpleRoute> routes = simpleRoutes.get(segmentName);
+ return routes != null ? routes.size() : 0;
+ }
+
+ SimpleRoute getSegment(String segmentName, int index)
+ {
+ List<SimpleRoute> routes = simpleRoutes.get(segmentName);
+ return routes != null ? routes.get(index) : null;
+ }
+
+ int getPatternSize()
+ {
+ return patternRoutes.size();
+ }
+
+ PatternRoute getPattern(int index)
+ {
+ return patternRoutes.get(index);
+ }
+
/**
* Append a path, creates the necessary routes and returns the last route added.
*
@@ -395,14 +443,8 @@
else
{
String segment = path.substring(0, pos);
- List<SimpleRoute> routes = simpleRoutes.get(segment);
- if (routes == null)
- {
- routes = new ArrayList<SimpleRoute>();
- simpleRoutes.put(segment, routes);
- }
- SimpleRoute route = new SimpleRoute(this, segment);
- routes.add(route);
+ SimpleRoute route = new SimpleRoute(segment);
+ add(route);
next = route;
}
}
@@ -463,8 +505,12 @@
// Julien : should the pattern end with a $ ?????? I don't see that for
now
// we need to figure out clearly
Pattern pattern = builder.build();
- PatternRoute route = new PatternRoute(this, pattern, parameterNames,
parameterPatterns, chunks);
- patternRoutes.add(route);
+ PatternRoute route = new PatternRoute(pattern, parameterNames,
parameterPatterns, chunks);
+
+ // Wire
+ add(route);
+
+ //
next = route;
}
else
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java 2010-09-12
21:53:26 UTC (rev 4157)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java 2010-09-12
22:54:07 UTC (rev 4158)
@@ -27,13 +27,10 @@
{
/** . */
- final String value;
+ final String name;
- SimpleRoute(Route parent, String value)
+ SimpleRoute(String name)
{
- super(parent);
-
- //
- this.value = value;
+ this.name = name;
}
}
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java 2010-09-12
21:53:26 UTC (rev 4157)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java 2010-09-12
22:54:07 UTC (rev 4158)
@@ -24,10 +24,7 @@
import org.exoplatform.web.controller.metadata.RouteDescriptor;
import org.exoplatform.web.controller.metadata.RouterDescriptor;
-import java.util.Arrays;
import java.util.Collections;
-import java.util.List;
-import java.util.Map;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
@@ -58,8 +55,7 @@
routerMD.addRoute(new RouteDescriptor(path));
Router router = new Router(routerMD);
Route expectedRoute = new Route();
- SimpleRoute a = new SimpleRoute(expectedRoute, "a");
- expectedRoute.simpleRoutes.put("a", Arrays.asList(a));
+ expectedRoute.add(new SimpleRoute("a"));
assertEquals(expectedRoute, router.root);
}
}
@@ -74,9 +70,9 @@
Router router = new Router(routerMD);
//
- assertEquals(0, router.root.simpleRoutes.size());
- assertEquals(1, router.root.patternRoutes.size());
- PatternRoute patternRoute = router.root.patternRoutes.get(0);
+ assertEquals(0, router.root.getSegmentNames().size());
+ assertEquals(1, router.root.getPatternSize());
+ PatternRoute patternRoute = router.root.getPattern(0);
assertEquals("^([^/]+)", patternRoute.pattern.toString());
assertEquals(Collections.singletonList(new QualifiedName("a")),
patternRoute.parameterNames);
assertEquals(1, patternRoute.parameterPatterns.size());
@@ -97,9 +93,9 @@
Router router = new Router(routerMD);
//
- assertEquals(0, router.root.simpleRoutes.size());
- assertEquals(1, router.root.patternRoutes.size());
- PatternRoute patternRoute = router.root.patternRoutes.get(0);
+ assertEquals(0, router.root.getSegmentNames().size());
+ assertEquals(1, router.root.getPatternSize());
+ PatternRoute patternRoute = router.root.getPattern(0);
assertEquals("^([^/]+)", patternRoute.pattern.toString());
assertEquals(Collections.singletonList(new QualifiedName("q",
"a")), patternRoute.parameterNames);
assertEquals(1, patternRoute.parameterPatterns.size());
@@ -120,9 +116,9 @@
Router router = new Router(routerMD);
//
- assertEquals(0, router.root.simpleRoutes.size());
- assertEquals(1, router.root.patternRoutes.size());
- PatternRoute patternRoute = router.root.patternRoutes.get(0);
+ assertEquals(0, router.root.getSegmentNames().size());
+ assertEquals(1, router.root.getPatternSize());
+ PatternRoute patternRoute = router.root.getPattern(0);
assertEquals("^(.*)", patternRoute.pattern.toString());
assertEquals(Collections.singletonList(new QualifiedName("a")),
patternRoute.parameterNames);
assertEquals(1, patternRoute.parameterPatterns.size());
@@ -141,25 +137,31 @@
//
Router router = new Router(routerMD);
- assertEquals(2, router.root.simpleRoutes.get("public").size());
- Route publicRoute1 = router.root.simpleRoutes.get("public").get(0);
- assertEquals(1, publicRoute1.simpleRoutes.get("foo").size());
- Route publicRoute2 = router.root.simpleRoutes.get("public").get(1);
- assertEquals(1, publicRoute2.simpleRoutes.get("bar").size());
+ assertEquals(2, router.root.getSegmentSize("public"));
+ Route publicRoute1 = router.root.getSegment("public", 0);
+ assertEquals(1, publicRoute1.getSegmentSize("foo"));
+ Route publicRoute2 = router.root.getSegment("public", 1);
+ assertEquals(1, publicRoute2.getSegmentSize("bar"));
}
private void assertEquals(Route expectedRoute, Route route)
{
assertEquals(expectedRoute.getClass(), route.getClass());
- assertEquals(expectedRoute.simpleRoutes.keySet(), route.simpleRoutes.keySet());
- for (Map.Entry<String, List<SimpleRoute>> entry :
expectedRoute.simpleRoutes.entrySet())
+ assertEquals(expectedRoute.getSegmentNames(), route.getSegmentNames());
+ for (String segmentName : expectedRoute.getSegmentNames())
{
- assertEquals(entry.getValue(), expectedRoute.simpleRoutes.get(entry.getKey()));
+ assertEquals(expectedRoute.getSegmentSize(segmentName),
route.getSegmentSize(segmentName));
+ for (int segmentIndex = 0;segmentIndex <
expectedRoute.getSegmentSize(segmentName);segmentIndex++)
+ {
+ SimpleRoute expectedSimpleRoute = expectedRoute.getSegment(segmentName,
segmentIndex);
+ SimpleRoute simpleRoute = route.getSegment(segmentName, segmentIndex);
+ assertEquals(expectedSimpleRoute, simpleRoute);
+ }
}
- assertEquals(expectedRoute.patternRoutes.size(), route.patternRoutes.size());
- for (int i = 0;i < expectedRoute.patternRoutes.size();i++)
+ assertEquals(expectedRoute.getPatternSize(), route.getPatternSize());
+ for (int i = 0;i < expectedRoute.getPatternSize();i++)
{
- assertEquals(expectedRoute.patternRoutes.get(i), route.patternRoutes.get(i));
+ assertEquals(expectedRoute.getPattern(i), route.getPattern(i));
}
if (route instanceof PatternRoute)
{
@@ -168,7 +170,7 @@
}
else if (route instanceof SimpleRoute)
{
- assertEquals(((SimpleRoute)expectedRoute).value, ((SimpleRoute)route).value);
+ assertEquals(((SimpleRoute)expectedRoute).name, ((SimpleRoute)route).name);
}
}
}