1414import java .util .HashMap ;
1515import java .util .List ;
1616import java .util .Map ;
17- import java .util .Objects ;
1817import java .util .regex .Pattern ;
1918
2019class $Chi implements RadixTree {
@@ -23,30 +22,6 @@ class $Chi implements RadixTree {
2322 private static final int ntParam = 2 ; // /{user}
2423 private static final int ntCatchAll = 3 ; // /api/v1/*
2524
26- private static class StaticKey {
27- private String method ;
28- private String pattern ;
29-
30- public StaticKey (String method , String pattern ) {
31- this .method = method ;
32- this .pattern = pattern ;
33- }
34-
35- @ Override public boolean equals (Object that ) {
36- if (this == that ) {
37- return true ;
38- } else {
39- StaticKey staticKey = (StaticKey ) that ;
40- return method .equals (staticKey .method ) &&
41- pattern .equals (staticKey .pattern );
42- }
43- }
44-
45- @ Override public int hashCode () {
46- return Objects .hash (method , pattern );
47- }
48- }
49-
5025 static class Segment {
5126 int nodeType ;
5227 String key = "" ;
@@ -628,7 +603,7 @@ public void destroy() {
628603 private Node root = new Node ();
629604
630605 /** Not need to use a concurrent map, due we don't allow to add routes after application started. */
631- private Map <StaticKey , Route > staticPaths = new HashMap <>();
606+ private Map <Object , Route > staticPaths = new HashMap <>();
632607
633608 public void insert (String method , String pattern , Route route ) {
634609 String baseCatchAll = baseCatchAll (pattern );
@@ -642,7 +617,7 @@ public void insert(String method, String pattern, Route route) {
642617 pattern = "/*" ;
643618 }
644619 if (Router .pathKeys (pattern ).isEmpty ()) {
645- staticPaths .put (new StaticKey ( method , pattern ) , route );
620+ staticPaths .put (method + pattern , route );
646621 }
647622 root .insertRoute (method , pattern , route );
648623 }
@@ -663,28 +638,32 @@ public void insert(Route route) {
663638 root .destroy ();
664639 }
665640
641+ public RouterMatch find (Context context , MessageEncoder encoder ,
642+ List <RadixTree > more ) {
643+ return find (context , context .pathString (), encoder , more );
644+ }
645+
666646 public RouterMatch find (Context context , String path , MessageEncoder encoder ,
667647 List <RadixTree > more ) {
668648 String method = context .getMethod ();
669649 RouterMatch result = new RouterMatch ();
670- Route route = staticPaths .get (new StaticKey (method , path ));
671- if (route != null ) {
672- return result .found (route );
673- }
674- // use radix tree
675- route = root .findRoute (result , method , path );
676- if (route != null ) {
677- return result .found (route );
678- }
679- if (more != null ) {
680- // expand search
681- for (RadixTree tree : more ) {
682- RouterMatch match = tree .find (context , path , encoder , null );
683- if (match .matches ) {
684- return match ;
650+ Route route = staticPaths .get (method + path );
651+ if (route == null ) {
652+ // use radix tree
653+ route = root .findRoute (result , method , path );
654+ if (route == null ) {
655+ if (more != null ) {
656+ // expand search
657+ for (RadixTree tree : more ) {
658+ RouterMatch match = tree .find (context , path , encoder , null );
659+ if (match .matches ) {
660+ return match ;
661+ }
662+ }
685663 }
664+ return result .missing (method , path , encoder );
686665 }
687666 }
688- return result .missing ( method , path , encoder );
667+ return result .found ( route );
689668 }
690669}
0 commit comments