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

com.github.dreamhead.moco.handler.CollectionHandler 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 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);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy