All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.riptide.RoutingTree Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package org.zalando.riptide;

import org.apiguardian.api.API;
import org.springframework.http.client.ClientHttpResponse;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.apiguardian.api.API.Status.STABLE;

/**
 * A routing tree is a nested {@link Route route} that consists of a {@link Navigator navigator} and a set of
 * {@link Binding bindings}. When being {@link Route#execute(ClientHttpResponse, MessageReader) executed} the navigator
 * will select the attribute value of the returned {@link ClientHttpResponse response}, find the correct binding
 * and execute {@link Binding#getRoute() it's route}. Since a routing tree is a route itself they can be nested
 * recursively inside each other to produce complex graphs.
 *
 * @param  generic attribute type
 * @see Route
 */
@API(status = STABLE)
public interface RoutingTree extends Route {

    Navigator getNavigator();

    Set keySet();

    Optional get(final A attribute);

    Optional getWildcard();

    /**
     * @throws NoWildcardException if no route, not even a wildcard, exists for the given response
     */
    @Override
    void execute(final ClientHttpResponse response, final MessageReader reader) throws Exception;

    @API(status = EXPERIMENTAL)
    default RoutingTree merge(final Binding binding) {
        return merge(Collections.singletonList(binding));
    }

    @API(status = EXPERIMENTAL)
    default RoutingTree merge(final List> bindings) {
        return merge(dispatch(getNavigator(), bindings));
    }

    @Override
    default Route merge(final Route route) {
        if (route instanceof RoutingTree) {
            final RoutingTree other = (RoutingTree) route;
            if (getNavigator().equals(other.getNavigator())) {
                @SuppressWarnings("unchecked")
                final RoutingTree tree = other;
                return merge(tree);
            }
        }

        return Route.super.merge(route);
    }

    @API(status = EXPERIMENTAL)
    default RoutingTree merge(final RoutingTree other) {
        final Map bindings = new LinkedHashMap<>(keySet().size() + other.keySet().size());

        keySet().forEach(attribute ->
                bindings.merge(attribute, get(attribute)
                        .orElseThrow(IllegalStateException::new), Route::merge));

        getWildcard().ifPresent(route ->
                bindings.merge(null, route, Route::merge));

        other.keySet().forEach(attribute ->
                bindings.merge(attribute, other.get(attribute)
                        .orElseThrow(IllegalStateException::new), Route::merge));

        other.getWildcard().ifPresent(route ->
                bindings.merge(null, route, Route::merge));

        return dispatch(getNavigator(), bindings.entrySet().stream()
                .map(e -> Binding.create(e.getKey(), e.getValue()))
                .collect(toList()));
    }

    @SafeVarargs
    static  RoutingTree dispatch(final Navigator navigator, final Binding... bindings) {
        return dispatch(navigator, asList(bindings));
    }

    static  RoutingTree dispatch(final Navigator navigator, final List> bindings) {
        return new DefaultRoutingTree<>(navigator, bindings);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy