Design a route handler that stores route patterns and matches incoming URLs in the same way as a basic web framework.
A route consists of path segments separated by /. A segment enclosed in braces is a parameter. For example, the route /products/{productId}/reviews/{reviewId} contains the parameters productId and reviewId.
A parameter matches exactly one non-empty URL segment. Static segments must match exactly and comparisons are case-sensitive. A route and a URL match only when they contain the same number of segments.
If multiple routes match a URL, choose the route containing the greatest number of static segments. If there is still a tie, choose the route that was registered earlier.
RouteHandler()
Initialize an empty route handler.
void registerHandler(String route)
route: The route pattern to register.Store the given route pattern so that it can match future URLs.
String getRouteHandler(String url)
url: The URL to match against the registered routes.Return the best matching registered route. Return empty string "" if no route matches the URL.
List<String> getRouteHandlerWithParameters(String url)
url: The URL to match against the registered routes.Return a list containing the matched route followed by its extracted parameters in name=value format. Parameters must appear in their left-to-right order in the route.
Return an empty list if no route matches.
/.{itemId} matches one non-empty segment.2 ≤ route.length(), url.length() ≤ 1,000/./ characters.10,000 routes are registered.100,000 method calls are made.RouteHandler routeHandler = new RouteHandler()
routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )
routeHandler.getRouteHandler( url = "/stores/42/items/900" )
Output: "/stores/{storeId}/items/{itemId}"
Both parameter segments match the corresponding URL segments.
RouteHandler routeHandler = new RouteHandler()
routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )
routeHandler.getRouteHandlerWithParameters( url = "/stores/42/items/900" )
Output: ["/stores/{storeId}/items/{itemId}", "storeId=42", "itemId=900"]
The matched route is returned first, followed by the extracted parameters in route order.
RouteHandler routeHandler = new RouteHandler()
routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )
routeHandler.registerHandler( route = "/stores/sale/items/{itemId}" )
routeHandler.getRouteHandler( url = "/stores/sale/items/28" )
Output: "/stores/sale/items/{itemId}"
Both routes match, but the selected route contains more static segments.
RouteHandler routeHandler = new RouteHandler()
routeHandler.registerHandler( route = "/teams/{teamId}/members/{memberId}" )
routeHandler.getRouteHandler( url = "/teams/15/settings" )
Output: ""
The registered route and the URL have different numbers of segments, so they do not match.
RouteHandler routeHandler = new RouteHandler()
routeHandler.registerHandler( route = "/articles/{articleId}" )
routeHandler.getRouteHandlerWithParameters( url = "/authors/73" )
Output: []
The static segment articles does not match authors.