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.google.common.collect.FluentIterable.from;
import static com.google.common.collect.ImmutableList.copyOf;

public class AndResponseHandler extends AbstractResponseHandler {
    private final Iterable handlers;

    public 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 AndResponseHandler and(final Iterable handlers) {
        return new AndResponseHandler(handlers);
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy