Skip to content

Commit 66e14eb

Browse files
committed
router: improve routeSet API
- remove mvc() from router - make all multiple import of routes a routeSet (unify API)
1 parent dadaca4 commit 66e14eb

7 files changed

Lines changed: 55 additions & 118 deletions

File tree

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,8 @@ public Jooby setTrustProxy(boolean trustProxy) {
469469
}
470470

471471
@NonNull @Override
472-
public Router domain(@NonNull String domain, @NonNull Router subrouter) {
473-
this.router.domain(domain, subrouter);
474-
return this;
472+
public RouteSet domain(@NonNull String domain, @NonNull Router subrouter) {
473+
return this.router.domain(domain, subrouter);
475474
}
476475

477476
@NonNull @Override
@@ -485,31 +484,36 @@ public RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Runnable b
485484
}
486485

487486
@NonNull @Override
488-
public Jooby mount(@NonNull Predicate<Context> predicate, @NonNull Router subrouter) {
489-
this.router.mount(predicate, subrouter);
490-
return this;
487+
public RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Router subrouter) {
488+
return this.router.mount(predicate, subrouter);
491489
}
492490

493491
@NonNull @Override
494-
public Jooby mount(@NonNull String path, @NonNull Router router) {
495-
this.router.mount(path, router);
492+
public RouteSet mount(@NonNull String path, @NonNull Router router) {
493+
var rs = this.router.mount(path, router);
496494
if (router instanceof Jooby) {
497495
Jooby child = (Jooby) router;
498496
child.registry = this.registry;
499497
}
500-
return this;
498+
return rs;
501499
}
502500

503501
@NonNull @Override
504-
public Jooby mount(@NonNull Router router) {
502+
public RouteSet mount(@NonNull Router router) {
505503
return mount("/", router);
506504
}
507505

508-
@NonNull @Override
509-
public Jooby mvc(@NonNull Extension router) {
506+
/**
507+
* Add controller routes.
508+
*
509+
* @param router Mvc extension.
510+
* @return Route set.
511+
*/
512+
@NonNull public RouteSet mvc(@NonNull Extension router) {
510513
try {
514+
int start = this.router.getRoutes().size();
511515
router.install(this);
512-
return this;
516+
return new RouteSet(this.router.getRoutes().subList(start, this.router.getRoutes().size()));
513517
} catch (Exception cause) {
514518
throw SneakyThrows.propagate(cause);
515519
}

jooby/src/main/java/io/jooby/RouteSet.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.util.*;
1111
import java.util.concurrent.Executor;
12+
import java.util.function.Consumer;
13+
import java.util.function.Predicate;
1214

1315
import edu.umd.cs.findbugs.annotations.NonNull;
1416
import edu.umd.cs.findbugs.annotations.Nullable;
@@ -20,7 +22,7 @@
2022
* @author edgar
2123
* @since 2.7.3
2224
*/
23-
public class RouteSet {
25+
public class RouteSet implements Iterable<Route> {
2426

2527
private List<Route> routes;
2628

@@ -30,6 +32,10 @@ public class RouteSet {
3032

3133
private String description;
3234

35+
public RouteSet(List<Route> routes) {
36+
this.routes = routes;
37+
}
38+
3339
/**
3440
* Sub-routes. Always empty except when used it from {@link Router#path(String, Runnable)} or
3541
* {@link Router#routes(Runnable)}.
@@ -233,4 +239,13 @@ public class RouteSet {
233239
public @NonNull RouteSet description(@Nullable String description) {
234240
return setDescription(description);
235241
}
242+
243+
public void forEach(Predicate<Route> predicate, Consumer<? super Route> action) {
244+
routes.stream().filter(predicate).forEach(action);
245+
}
246+
247+
@Override
248+
public Iterator<Route> iterator() {
249+
return routes.iterator();
250+
}
236251
}

jooby/src/main/java/io/jooby/Router.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ default Object execute(@NonNull Context context) {
312312
*
313313
* @param domain Predicate
314314
* @param subrouter Subrouter.
315-
* @return This router.
315+
* @return Created routes.
316316
*/
317-
@NonNull Router domain(@NonNull String domain, @NonNull Router subrouter);
317+
@NonNull RouteSet domain(@NonNull String domain, @NonNull Router subrouter);
318318

319319
/**
320320
* Enabled routes for specific domain. Domain matching is done using the <code>host</code> header.
@@ -334,7 +334,7 @@ default Object execute(@NonNull Context context) {
334334
*
335335
* @param domain Predicate
336336
* @param body Route action.
337-
* @return This router.
337+
* @return Created routes.
338338
*/
339339
@NonNull RouteSet domain(@NonNull String domain, @NonNull Runnable body);
340340

@@ -360,9 +360,9 @@ default Object execute(@NonNull Context context) {
360360
*
361361
* @param predicate Context predicate.
362362
* @param router Router to import.
363-
* @return This router.
363+
* @return Created routes.
364364
*/
365-
@NonNull Router mount(@NonNull Predicate<Context> predicate, @NonNull Router router);
365+
@NonNull RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Router router);
366366

367367
/**
368368
* Import routes from given action. Predicate works like a filter and only when predicate pass the
@@ -387,7 +387,7 @@ default Object execute(@NonNull Context context) {
387387
*
388388
* @param predicate Context predicate.
389389
* @param body Route action.
390-
* @return This router.
390+
* @return Created routes.
391391
*/
392392
@NonNull RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Runnable body);
393393

@@ -398,33 +398,25 @@ default Object execute(@NonNull Context context) {
398398
*
399399
* @param path Prefix path.
400400
* @param router Router to import.
401-
* @return This router.
401+
* @return Created routes.
402402
*/
403-
@NonNull Router mount(@NonNull String path, @NonNull Router router);
403+
@NonNull RouteSet mount(@NonNull String path, @NonNull Router router);
404404

405405
/**
406406
* Import all routes from the given router.
407407
*
408408
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
409409
*
410410
* @param router Router to import.
411-
* @return This router.
411+
* @return Created routes.
412412
*/
413-
@NonNull Router mount(@NonNull Router router);
413+
@NonNull RouteSet mount(@NonNull Router router);
414414

415415
/* ***********************************************************************************************
416416
* Mvc
417417
* ***********************************************************************************************
418418
*/
419419

420-
/**
421-
* Import all routes from the given controller class.
422-
*
423-
* @param router Router extension.
424-
* @return This router.
425-
*/
426-
@NonNull Router mvc(@NonNull Extension router);
427-
428420
/**
429421
* Add a websocket handler.
430422
*

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Stack executor(Executor executor) {
136136

137137
private List<Route> routes = new ArrayList<>();
138138

139-
private HttpMessageEncoder encoder = new HttpMessageEncoder();
139+
private final HttpMessageEncoder encoder = new HttpMessageEncoder();
140140

141141
private String basePath;
142142

@@ -265,19 +265,17 @@ public RouteSet domain(@NonNull String domain, @NonNull Runnable body) {
265265
}
266266

267267
@NonNull @Override
268-
public Router domain(@NonNull String domain, @NonNull Router subrouter) {
268+
public RouteSet domain(@NonNull String domain, @NonNull Router subrouter) {
269269
return mount(domainPredicate(domain), subrouter);
270270
}
271271

272272
@NonNull @Override
273273
public RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Runnable body) {
274-
var routeSet = new RouteSet();
275274
var tree = new Chi();
276275
putPredicate(predicate, tree);
277276
int start = this.routes.size();
278277
newStack(tree, "/", body);
279-
routeSet.setRoutes(this.routes.subList(start, this.routes.size()));
280-
return routeSet;
278+
return new RouteSet(this.routes.subList(start, this.routes.size()));
281279
}
282280

283281
public Router install(
@@ -297,42 +295,37 @@ public Router install(
297295
}
298296

299297
@NonNull @Override
300-
public Router mount(@NonNull Predicate<Context> predicate, @NonNull Router subrouter) {
301-
/** Override services: */
298+
public RouteSet mount(@NonNull Predicate<Context> predicate, @NonNull Router subrouter) {
299+
/* Override services: */
302300
overrideAll(this, subrouter);
303-
/** Routes: */
304-
mount(
301+
/* Routes: */
302+
return mount(
305303
predicate,
306304
() -> {
307305
for (Route route : subrouter.getRoutes()) {
308306
Route newRoute = newRoute(route.getMethod(), route.getPattern(), route.getHandler());
309307
copy(route, newRoute);
310308
}
311309
});
312-
return this;
313310
}
314311

315312
@NonNull @Override
316-
public Router mount(@NonNull String path, @NonNull Router router) {
313+
public RouteSet mount(@NonNull String path, @NonNull Router router) {
314+
int start = this.routes.size();
317315
/** Override services: */
318316
overrideAll(this, router);
319317
/** Merge error handler: */
320318
mergeErrorHandler(router);
321319
/** Routes: */
322320
copyRoutes(path, router);
323-
return this;
321+
return new RouteSet(this.routes.subList(start, this.routes.size()));
324322
}
325323

326324
@NonNull @Override
327-
public Router mount(@NonNull Router router) {
325+
public RouteSet mount(@NonNull Router router) {
328326
return mount("/", router);
329327
}
330328

331-
@NonNull @Override
332-
public Router mvc(@NonNull Extension router) {
333-
throw new UnsupportedOperationException();
334-
}
335-
336329
@NonNull @Override
337330
public Router encoder(@NonNull MessageEncoder encoder) {
338331
this.encoder.add(MediaType.all, encoder);
@@ -413,11 +406,9 @@ public RouteSet routes(@NonNull Runnable action) {
413406

414407
@Override
415408
@NonNull public RouteSet path(@NonNull String pattern, @NonNull Runnable action) {
416-
RouteSet routeSet = new RouteSet();
417409
int start = this.routes.size();
418410
newStack(chi, pattern, action);
419-
routeSet.setRoutes(this.routes.subList(start, this.routes.size()));
420-
return routeSet;
411+
return new RouteSet(this.routes.subList(start, this.routes.size()));
421412
}
422413

423414
@NonNull @Override
@@ -898,7 +889,6 @@ private void copy(Route src, Route it) {
898889
it.setFilter(filter);
899890
it.setAfter(after);
900891
it.setEncoder(src.getEncoder());
901-
// it.setReturnType(src.getReturnType());
902892
it.setHandle(src.getHandle());
903893
it.setProduces(src.getProduces());
904894
it.setConsumes(src.getConsumes());

modules/jooby-apt/src/test/java/tests/i3490/Box3490.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

modules/jooby-apt/src/test/java/tests/i3490/C3490.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

modules/jooby-apt/src/test/java/tests/i3490/Issue3490.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)