Nothing Special   »   [go: up one dir, main page]

Skip to content

A technology agnostic routing framework for anything that requires routing using several attributes of the source

License

Notifications You must be signed in to change notification settings

mP1/walkingkooka-route

Repository files navigation

Build Status Coverage Status License Language grade: Java Total alerts J2CL compatible

Router

The following example builds a very simple, totally useless router that dispatches urls. This examples constructs a router where all some components of a URL have been decomposed into components, and several paths have all their required components and predicates defined. The router will then return the target value only if only a single rule is entirely matched.

final RouteMappings<String, Integer> mappings = RouteMappings.<String, Integer>empty()
        .add(Maps.of("protocol", Predicates.is("http"), "host", Predicate.isEqual("example.com")), 12)
        .add(Maps.of("protocol", Predicates.is("http"), "host", Predicate.isEqual("example2.com")), 34)
        .add(Maps.of("protocol", Predicates.is("https")), 56);
final Router<String, Integer> router = mappings.router();

final Optional<Integer> https = router.route(Maps.of("protocol", "https")); // should return 56
final Optional<Integer> http = router.route(Maps.of("protocol", "http")); // 12 and 34 both match returns nothing.
final Optional<Integer> httpExample = router.route(Maps.of("protocol", "http", "host", "example.com")); // should return 12

assertEquals(Optional.of(56), https);
assertEquals(Optional.empty(), http); // matches two rules, there no value
assertEquals(Optional.of(12), httpExample);

This projects takes this project to the next level, supporting expressing rules about all components within a http request.

About

A technology agnostic routing framework for anything that requires routing using several attributes of the source

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages