com.github.dreamhead.moco.handler.AndResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
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));
}
}