com.github.dreamhead.moco.matcher.CompositeRequestMatcher 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.matcher;
import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.RequestMatcher;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import static com.google.common.collect.FluentIterable.from;
public abstract class CompositeRequestMatcher extends AbstractRequestMatcher {
protected abstract RequestMatcher newMatcher(Iterable matchers);
private final Iterable matchers;
protected CompositeRequestMatcher(final Iterable matchers) {
this.matchers = matchers;
}
private Iterable applyToMatchers(final MocoConfig config) {
FluentIterable appliedMatchers = from(matchers).transform(applyConfig(config));
if (matchers.equals(appliedMatchers)) {
return this.matchers;
}
return appliedMatchers;
}
private Function applyConfig(final MocoConfig config) {
return new Function() {
@Override
public RequestMatcher apply(final RequestMatcher matcher) {
return matcher.apply(config);
}
};
}
@Override
public final RequestMatcher doApply(final MocoConfig config) {
Iterable appliedMatchers = applyToMatchers(config);
if (appliedMatchers == this.matchers) {
return this;
}
return newMatcher(appliedMatchers);
}
@Override
public final boolean match(final Request request) {
return doMatch(request, matchers);
}
protected abstract boolean doMatch(Request request, Iterable matchers);
}