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

com.github.dreamhead.moco.handler.AndResponseHandler Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.handler;

import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.internal.SessionContext;
import com.google.common.base.Function;

import static com.github.dreamhead.moco.util.Iterables.asIterable;
import static com.google.common.collect.FluentIterable.from;

public final class AndResponseHandler extends AbstractResponseHandler {
    private final Iterable handlers;

    private AndResponseHandler(final Iterable handlers) {
        this.handlers = handlers;
    }

    @Override
    public void writeToResponse(final SessionContext context) {
        for (ResponseHandler handler : handlers) {
            handler.writeToResponse(context);
        }
    }

    @Override
    public ResponseHandler apply(final MocoConfig config) {
        if (config.isFor(MocoConfig.RESPONSE_ID)) {
            return super.apply(config);
        }

        return and(from(handlers).transform(applyConfig(config)));
    }

    private Function applyConfig(final MocoConfig config) {
        return new Function() {
            @Override
            public ResponseHandler apply(final ResponseHandler handler) {
                return handler.apply(config);
            }
        };
    }

    public static ResponseHandler and(final Iterable handlers) {
        return new AndResponseHandler(handlers);
    }

    public static ResponseHandler and(final ResponseHandler handler, final ResponseHandler... handlers) {
        if (handlers.length == 0) {
            return handler;
        }

        return new AndResponseHandler(asIterable(handler, handlers));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy