Package io.conducive.client.http.glue

Source Code of io.conducive.client.http.glue.HttpCallback

package io.conducive.client.http.glue;

import com.ekuefler.supereventbus.EventBus;
import io.conducive.client.ui.events.FailureEvent;

import java.util.List;

/**
* Callback which is informed of results from a REST API call via the onSuccess/onFailure methods.
*
* At least one of the onSuccess methods should be overridden. By default, onFailure will post a FailureEvent
* to the event bus, but custom behaviour can be added by overriding one or both onFailure methods.
*
* @author Reuben Firmin
*/
public class HttpCallback<T> {

    private Class<T> prototype;
    private EventBus eventBus;

    /**
     * Create callback with a prototype of the class it'll be returning.
     */
    public HttpCallback(EventBus eventBus, Class<T> prototype) {
        this.prototype = prototype;
        this.eventBus = eventBus;
    }

    /**
     * Called when an asynchronous call completes successfully.
     *
     * @param result the value returned; will be null if no result
     */
    public void onSuccess(T result) {
        // no-op
    }

    /**
     * Default implementation will call onSuccess multiple times.
     *
     * @param result Will be empty list if no result
     */
    public void onSuccess(List<T> result) {
        if (result.size() == 0) {
            onSuccess((T) null);
        }
        for (T res : result) {
            onSuccess(res);
        }
    }

    public void onFailure(String reason) {
        eventBus.post(new FailureEvent(reason));
    }

    public void onFailure(Throwable e) {
        onFailure(e.getMessage());
    }

    protected Class<T> prototype() {
        return prototype;
    }
}
TOP

Related Classes of io.conducive.client.http.glue.HttpCallback

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.