4141import java .util .ArrayList ;
4242import java .util .Collections ;
4343import java .util .HashMap ;
44+ import java .util .LinkedHashMap ;
4445import java .util .LinkedList ;
4546import java .util .List ;
4647import java .util .Map ;
@@ -123,7 +124,7 @@ public Stack executor(Executor executor) {
123124
124125 private String basePath ;
125126
126- private List < RadixTree > trees ;
127+ private Map < Predicate < Context >, RadixTree > predicateMap ;
127128
128129 private Executor worker = new ForwardingExecutor ();
129130
@@ -194,11 +195,11 @@ public RouterImpl(ClassLoader loader) {
194195
195196 @ Nonnull @ Override
196197 public Router use (@ Nonnull Predicate <Context > predicate , @ Nonnull Router router ) {
197- RadixTree tree = new $Chi (). with ( predicate ) ;
198- if (trees == null ) {
199- trees = new ArrayList <>();
198+ RadixTree tree = new $Chi ();
199+ if (predicateMap == null ) {
200+ predicateMap = new LinkedHashMap <>();
200201 }
201- trees . add ( tree );
202+ predicateMap . put ( predicate , tree );
202203 for (Route route : router .getRoutes ()) {
203204 Route newRoute = defineRoute (route .getMethod (), route .getPattern (), route .getHandler (), tree );
204205 copy (route , newRoute );
@@ -339,7 +340,8 @@ public Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder en
339340 return beanConverters ;
340341 }
341342
342- @ Nonnull @ Override public Route ws (@ Nonnull String pattern , @ Nonnull WebSocket .Initializer handler ) {
343+ @ Nonnull @ Override
344+ public Route ws (@ Nonnull String pattern , @ Nonnull WebSocket .Initializer handler ) {
343345 return route (WS , pattern , new WebSocketHandler (handler )).setHandle (handler );
344346 }
345347
@@ -464,7 +466,8 @@ private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
464466 }
465467 } else {
466468 /** Consumes && Produces (only for HTTP routes (not web socket) */
467- route .setBefore (prependMediaType (route .getConsumes (), route .getBefore (), Route .SUPPORT_MEDIA_TYPE ));
469+ route .setBefore (
470+ prependMediaType (route .getConsumes (), route .getBefore (), Route .SUPPORT_MEDIA_TYPE ));
468471 route .setBefore (prependMediaType (route .getProduces (), route .getBefore (), Route .ACCEPT ));
469472 }
470473 /** Response handler: */
@@ -517,10 +520,10 @@ public void destroy() {
517520 errorCodes .clear ();
518521 errorCodes = null ;
519522 }
520- if (this .trees != null ) {
521- this .trees .forEach (RadixTree ::destroy );
522- this .trees .clear ();
523- this .trees = null ;
523+ if (this .predicateMap != null ) {
524+ this .predicateMap . values () .forEach (RadixTree ::destroy );
525+ this .predicateMap .clear ();
526+ this .predicateMap = null ;
524527 }
525528 }
526529
@@ -529,7 +532,17 @@ public void destroy() {
529532 }
530533
531534 @ Nonnull @ Override public Match match (@ Nonnull Context ctx ) {
532- return chi .find (ctx , ctx .pathString (), encoder , trees );
535+ if (predicateMap != null ) {
536+ for (Map .Entry <Predicate <Context >, RadixTree > e : predicateMap .entrySet ()) {
537+ if (e .getKey ().test (ctx )) {
538+ RouterMatch match = e .getValue ().find (ctx , ctx .pathString (), encoder );
539+ if (match .matches ) {
540+ return match ;
541+ }
542+ }
543+ }
544+ }
545+ return chi .find (ctx , ctx .pathString (), encoder );
533546 }
534547
535548 @ Override public boolean match (@ Nonnull String pattern , @ Nonnull String path ) {
0 commit comments