com.gooddata.sdk.service.AbstractPollHandlerBase Maven / Gradle / Ivy
/*
* Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.service;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
import static com.gooddata.sdk.common.util.Validate.notNull;
/**
* For internal use by services employing polling.
*
* @param
polling type
* @param result type
*
* @see FutureResult
*/
public abstract class AbstractPollHandlerBase implements PollHandler
{
protected final Class
pollClass;
protected final Class resultClass;
private boolean done = false;
private R result;
protected AbstractPollHandlerBase(Class pollClass, Class resultClass) {
this.pollClass = notNull(pollClass, "pollClass");
this.resultClass = notNull(resultClass, "resultClass");
}
@Override
public final Class getResultClass() {
return resultClass;
}
@Override
public final Class getPollClass() {
return pollClass;
}
protected PollHandler
setResult(R result) {
this.result = result;
this.done = true;
onFinish();
return this;
}
@Override
public final boolean isDone() {
return done;
}
@Override
public final R getResult() {
return result;
}
@Override
public boolean isFinished(final ClientHttpResponse response) throws IOException {
return HttpStatus.OK.equals(response.getStatusCode());
}
/**
* Method called after polling is successfully finished (default no-op)
*/
protected void onFinish() {
}
}