From 37bc5f9f96888899dceae0e8e8102a1764f78967 Mon Sep 17 00:00:00 2001 From: Duncan Proctor Date: Thu, 11 Aug 2022 20:23:03 -0400 Subject: [PATCH] remove summation and prefixing --- .idea/workspace.xml | 36 +++++++----------- README.md | 24 +----------- build.gradle.kts | 2 +- .../com/duncpro/jroute/router/TreeRouter.java | 31 ---------------- .../duncpro/jroute/router/TreeRouterTest.java | 37 ------------------- 5 files changed, 16 insertions(+), 114 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0ea1d12..338bd64 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,8 +6,10 @@ + - + + + + @@ -77,24 +82,12 @@ - - - - - - - - - - - - - - - - - - + @@ -235,7 +228,6 @@ - diff --git a/README.md b/README.md index 1188baf..91e162a 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Notably `TreeRouter` is not thread safe for mutations. Consider wrapping it in a to add routes concurrently. In practice route registration typically only happens at startup and the process is quick enough that multi-threading is unnecessary. -## Router Inspection and Summation +## Router Inspection In some cases, like when generating API documentation, an application might need to inspect the state of a router at runtime. This can be accomplished using the `Router.getAllEndpoints()` method. @@ -61,27 +61,5 @@ final Router routers = /* ... */; final Set> positionedEndpoints = router.getAllEndpoints(Route.ROOT); ``` -### Summing Multiple Routers -For large applications consisting of many modules it can be useful to assign -each module its own `Router`. The following example demonstrates summing two routers. - -Assume that we are using some library which generates API documentation for a -given `Router` and returns a new `Router` which holds that documentation... -````java -final Router appRouter = /* ... */; -final Router docRouter = generateApiDocs(appRouter); -final TreeRouter masterRouter = TreeRouter.sum(Set.of(appRouter, docRouter)); -```` -Now there is a single Router which contains the routes for our application and our application's documentation. - -### Prefixing Routers -`TreeRouter.prefix` produces a copy of the original router except all routes begin with an arbitrary prefix `Route`. - -Our original documentation example can be extended by storing all API docs under a specific route. -```java -final Router docRouter = TreeRouter.prefix(generateApiDocs(appRouter), - new Route("/docs")); -``` - ## More Docs There is a Javadoc for this library [here](https://duncpro.github.io/JRoute). diff --git a/build.gradle.kts b/build.gradle.kts index 3bf0f4d..552f857 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "com.duncpro" -version = "1.0-SNAPSHOT-4" +version = "1.0-SNAPSHOT-5" repositories { mavenCentral() diff --git a/src/main/java/com/duncpro/jroute/router/TreeRouter.java b/src/main/java/com/duncpro/jroute/router/TreeRouter.java index c8d4ff6..1a70f66 100644 --- a/src/main/java/com/duncpro/jroute/router/TreeRouter.java +++ b/src/main/java/com/duncpro/jroute/router/TreeRouter.java @@ -67,35 +67,4 @@ protected static RouteTreeNode findOrCreateNode(RouteTreeNode routeTre final var child = routeTree.getOrCreateChildRoute(route.getElements().get(0)); return findOrCreateNode(child, route.withoutLeadingElement()); } - - /** - * Creates a new {@link TreeRouter} containing the sum of all routes in the given sub routers - * at the time of invocation. - * @throws RouteConflictException if the given routers cannot be summed because they - * contain routes which conflict with one another. - */ - public static TreeRouter sum(Collection> subRouters) { - final var masterRouter = new TreeRouter(); - - subRouters.stream() - .flatMap(subRouter -> subRouter.getAllEndpoints(Route.ROOT).stream()) - .forEach(positionedEndpoint -> masterRouter.addRoute(positionedEndpoint.method, - positionedEndpoint.route, positionedEndpoint.endpoint)); - - return masterRouter; - } - - /** - * Returns a new {@link TreeRouter} which is identical to the given {@link Router} except - * all routes have been prefixed with the given {@link Route}. The returned router will not - * reflect any changes made to the original router. - */ - public static TreeRouter prefix(Router router, Route prefix) { - final var prefixedRouter = new TreeRouter(); - for (final var positionedEndpoint : router.getAllEndpoints(Route.ROOT)) { - final var prefixedRoute = Route.concat(prefix, positionedEndpoint.route); - prefixedRouter.addRoute(positionedEndpoint.method, prefixedRoute, positionedEndpoint.endpoint); - } - return prefixedRouter; - } } diff --git a/src/test/java/com/duncpro/jroute/router/TreeRouterTest.java b/src/test/java/com/duncpro/jroute/router/TreeRouterTest.java index 67f3bfb..12c4d77 100644 --- a/src/test/java/com/duncpro/jroute/router/TreeRouterTest.java +++ b/src/test/java/com/duncpro/jroute/router/TreeRouterTest.java @@ -136,41 +136,4 @@ void getAllEndpoints() { assertEquals(2, resolvedEndpoints.size()); } } - - @Test - void sum() { - final Router router1 = new TreeRouter<>(); - final var endpoint1 = new PositionedEndpoint<>(new Route("/hello/world"), HttpMethod.GET, 1); - router1.addRoute(endpoint1); - - final Router router2 = new TreeRouter<>(); - final var endpoint2 = new PositionedEndpoint<>(new Route("/hello"), HttpMethod.GET, 1); - router2.addRoute(endpoint2); - - final var composedRouter = TreeRouter.sum(Set.of(router1, router2)); - - { - final var resolvedEndpoints = composedRouter.getAllEndpoints(new Route("/hello")); - assertEquals(2, resolvedEndpoints.size()); - assertTrue(resolvedEndpoints.stream() - .anyMatch(endpoint -> endpoint.equals(endpoint1))); - assertTrue(resolvedEndpoints.stream() - .anyMatch(endpoint -> endpoint.equals(endpoint2))); - } - } - - @Test - void prefix() { - final Router originalRouter = new TreeRouter<>(); - final var endpoint = new Object(); - - originalRouter.addRoute(HttpMethod.GET, "world", endpoint); - - final var prefixedRouter = TreeRouter.prefix(originalRouter, new Route("hello")); - final var resolvedEndpoints = prefixedRouter.getAllEndpoints(Route.ROOT); - - assertEquals(1, resolvedEndpoints.size()); - final var loneRoute = resolvedEndpoints.stream().findFirst().orElseThrow(); - assertEquals(new PositionedEndpoint<>(new Route("hello/world"), HttpMethod.GET, endpoint), loneRoute); - } }