From ca99abe614c8582b734866ee0950eb80df41e2f5 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Tue, 26 Dec 2023 17:35:01 +0100 Subject: [PATCH] route to record class ~5 min work --- src/at/nanopenguin/mtcg/http/Route.java | 9 +-------- src/at/nanopenguin/mtcg/http/Router.java | 12 ++++++------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/at/nanopenguin/mtcg/http/Route.java b/src/at/nanopenguin/mtcg/http/Route.java index d396e50..b8f8c90 100644 --- a/src/at/nanopenguin/mtcg/http/Route.java +++ b/src/at/nanopenguin/mtcg/http/Route.java @@ -2,12 +2,5 @@ package at.nanopenguin.mtcg.http; import at.nanopenguin.mtcg.application.Service; -public class Route { - public final Service service; - public final boolean hasPathVariable; - - public Route(Service service, boolean hasPathVariable) { - this.service = service; - this.hasPathVariable = hasPathVariable; - } +public record Route(Service service, boolean hasPathVariable) { } diff --git a/src/at/nanopenguin/mtcg/http/Router.java b/src/at/nanopenguin/mtcg/http/Router.java index 4c454f5..597a793 100644 --- a/src/at/nanopenguin/mtcg/http/Router.java +++ b/src/at/nanopenguin/mtcg/http/Router.java @@ -5,7 +5,7 @@ import at.nanopenguin.mtcg.application.Service; import java.util.*; public class Router { - private Map> routeMap = new HashMap<>(); + private final Map> routeMap = new HashMap<>(); public void addRoute(final HttpMethod method, final String route, final Service service, final int pathVarPos) { Map map = this.routeMap.get(method); @@ -13,7 +13,7 @@ public class Router { this.routeMap.put(method, (map = new HashMap<>())); } - List routeComponents = new ArrayList(Arrays.asList(route.split("/"))); + List routeComponents = new ArrayList<>(Arrays.asList(route.split("/"))); if (pathVarPos > 0) { routeComponents.set(pathVarPos, "{var}"); } @@ -35,8 +35,8 @@ public class Router { int i = 0; Route component = this.routeMap.get(method).get(routeComponents[i]); System.out.println(routeComponents[i]); - for (String search = routeComponents[i]; component != null && component.service == null; component = this.routeMap.get(method).get(search = String.join("/", search, routeComponents[++i]))) { - if (component.hasPathVariable) { + for (String search = routeComponents[i]; component != null && component.service() == null; component = this.routeMap.get(method).get(search = String.join("/", search, routeComponents[++i]))) { + if (component.hasPathVariable()) { pathVariable = routeComponents[++i]; search = String.join("/", search, "{var}"); } @@ -46,8 +46,8 @@ public class Router { return null; } - component.service.setPathVariable(pathVariable); - return component.service; + component.service().setPathVariable(pathVariable); + return component.service(); } }