com.github.dreamhead.moco.internal.BaseResponseSettingConfiguration 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.internal;
import com.github.dreamhead.moco.MocoEventTrigger;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.ResponseSetting;
import com.google.common.reflect.TypeToken;
import java.util.List;
import static com.github.dreamhead.moco.handler.AndResponseHandler.and;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
public abstract class BaseResponseSettingConfiguration>
extends AbstractResponseBase implements ResponseSetting {
protected ResponseHandler handler;
protected List eventTriggers = newArrayList();
private final Class clazz;
@SuppressWarnings("unchecked")
protected BaseResponseSettingConfiguration() {
this.clazz = (Class) new TypeToken(getClass()) {}.getRawType();
}
private T self() {
return clazz.cast(this);
}
@Override
public T response(final ResponseHandler handler, final ResponseHandler... handlers) {
ResponseHandler responseHandler = and(checkNotNull(handler, "Handler should not be null"),
checkNotNull(handlers, "Handlers should not be null"));
this.handler = targetHandler(responseHandler);
return self();
}
private ResponseHandler targetHandler(final ResponseHandler responseHandler) {
if (this.handler == null) {
return responseHandler;
}
return and(this.handler, responseHandler);
}
@Override
public T on(final MocoEventTrigger trigger) {
this.eventTriggers.add(checkNotNull(trigger, "Trigger should not be null"));
return self();
}
}