@@ -674,7 +674,7 @@ public String toString() {
674674 return node .toString ();
675675 }
676676
677- Node insertRoute (String method , String pattern , Route route ) {
677+ Node insertRoute (String method , String pattern , Route route , boolean failOnDuplicateRoutes ) {
678678 Node n = this ;
679679 Node parent ;
680680 String search = pattern ;
@@ -683,7 +683,7 @@ Node insertRoute(String method, String pattern, Route route) {
683683 // Handle key exhaustion
684684 if (search .isEmpty ()) {
685685 // Insert or update the node's leaf handler
686- n .setEndpoint (method , route );
686+ n .setEndpoint (method , route , failOnDuplicateRoutes );
687687 return n ;
688688 }
689689
@@ -716,7 +716,7 @@ Node insertRoute(String method, String pattern, Route route) {
716716 if (n == null ) {
717717 Node child = new Node ().label (label ).tail (seg .tail ).prefix (search );
718718 Node hn = parent .addChild (child , search );
719- hn .setEndpoint (method , route );
719+ hn .setEndpoint (method , route , failOnDuplicateRoutes );
720720 return hn ;
721721 }
722722
@@ -752,14 +752,14 @@ Node insertRoute(String method, String pattern, Route route) {
752752 // If the new key is a subset, set the method/handler on this node and finish.
753753 search = search .substring (commonPrefix );
754754 if (search .isEmpty ()) {
755- child .setEndpoint (method , route );
755+ child .setEndpoint (method , route , failOnDuplicateRoutes );
756756 return child ;
757757 }
758758
759759 // Create a new edge for the node
760760 Node subchild = new Node ().typ (ntStatic ).label (search .charAt (0 )).prefix (search );
761761 Node hn = child .addChild (subchild , search );
762- hn .setEndpoint (method , route );
762+ hn .setEndpoint (method , route , failOnDuplicateRoutes );
763763 return hn ;
764764 }
765765 }
@@ -871,7 +871,7 @@ Node getEdge(int ntyp, char label, char tail, String prefix) {
871871 return null ;
872872 }
873873
874- void setEndpoint (String method , Route route ) {
874+ void setEndpoint (String method , Route route , boolean failOnDuplicateRoutes ) {
875875 Node n = this ;
876876 // Set the handler for the method type on the node
877877 if (n .endpoints == null ) {
@@ -893,6 +893,20 @@ void setEndpoint(String method, Route route) {
893893 // h.paramKeys = paramKeys;
894894 // }
895895 // } else {
896+ if (failOnDuplicateRoutes ) {
897+ var existing = n .endpoints .get (method );
898+ if (existing != null ) {
899+ throw new IllegalArgumentException (
900+ "Route already exists: "
901+ + method
902+ + " "
903+ + existing .getPattern ()
904+ + " at "
905+ + existing .getLocation ().filename ()
906+ + ":"
907+ + existing .getLocation ().line ());
908+ }
909+ }
896910 n .endpoints .put (method , route );
897911 // Endpoint h = n.endpoints.computeIfAbsent(method, k -> new Endpoint(handler));
898912 // h.handler = handler;
@@ -1193,6 +1207,12 @@ public void destroy() {
11931207
11941208 private StaticMap staticPaths = StaticMap .INIT ;
11951209
1210+ boolean failOnDuplicateRoutes ;
1211+
1212+ public Chi (boolean failOnDuplicateRoutes ) {
1213+ this .failOnDuplicateRoutes = failOnDuplicateRoutes ;
1214+ }
1215+
11961216 public void insert (String method , String pattern , Route route ) {
11971217 String baseCatchAll = baseCatchAll (pattern );
11981218 if (baseCatchAll .length () > 1 ) {
@@ -1209,7 +1229,7 @@ public void insert(String method, String pattern, Route route) {
12091229 staticPaths = staticPaths .put (pattern , staticRoute );
12101230 staticRoute .put (method , route );
12111231 }
1212- root .insertRoute (method , pattern , route );
1232+ root .insertRoute (method , pattern , route , failOnDuplicateRoutes );
12131233 }
12141234
12151235 private String baseCatchAll (String pattern ) {
0 commit comments