com.github.dreamhead.moco.handler.CollectionHandler 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 com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.ImmutableList.copyOf;
public abstract class CollectionHandler extends AbstractResponseHandler {
private final ImmutableList handlers;
private int index;
protected CollectionHandler(final Iterable handlers) {
this.handlers = copyOf(handlers);
}
@Override
public final void writeToResponse(final SessionContext context) {
int current = index;
this.index = next(index, this.handlers.size());
handlers.get(current).writeToResponse(context);
}
@Override
public final ResponseHandler doApply(final MocoConfig config) {
FluentIterable transformedResources = from(copyOf(handlers)).transform(applyConfig(config));
return newCollectionHandler(transformedResources);
}
private Function applyConfig(final MocoConfig config) {
return new Function() {
@Override
public ResponseHandler apply(final ResponseHandler input) {
return input.apply(config);
}
};
}
protected abstract int next(int index, int size);
protected abstract ResponseHandler newCollectionHandler(Iterable handlers);
}