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

brooklyn.event.feed.FeedConfig Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.event.feed;

import static com.google.common.base.Preconditions.checkNotNull;
import brooklyn.event.AttributeSensor;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;

/**
 * Configuration for a poll, or a subscription etc, that is being added to a feed.
 * 
 * @author aled
 */
public class FeedConfig> {

    /** The onSuccess or onError functions can return this value to indicate that the sensor should not change. */
    public static final Object UNSET = new Object();
    
    private final AttributeSensor sensor;
    private Function onsuccess;
    private Function onfailure;
    private Function onexception;
    private Predicate checkSuccess;

    public FeedConfig(AttributeSensor sensor) {
        this.sensor = checkNotNull(sensor, "sensor");
    }

    public FeedConfig(FeedConfig other) {
        this.sensor = other.sensor;
        this.onsuccess = other.onsuccess;
        this.onfailure = other.onfailure;
        this.onexception = other.onexception;
        this.checkSuccess = other.checkSuccess;
    }

    @SuppressWarnings("unchecked")
    protected This self() {
        return (This) this;
    }
    
    public AttributeSensor getSensor() {
        return sensor;
    }

    public Predicate getCheckSuccess() {
        return checkSuccess;
    }
    
    public Function getOnSuccess() {
        return onsuccess;
    }

    public Function getOnFailure() {
        return onfailure;
    }
    
    /** @deprecated since 0.6; use {@link #getOnException()}) */
    public Function getOnError() {
        return getOnException();
    }

    public Function getOnException() {
        return onexception;
    }

    public This checkSuccess(Predicate val) {
        this.checkSuccess = checkNotNull(val, "checkSuccess");
        return self();
    }

    public This onSuccess(Function val) {
        this.onsuccess = checkNotNull(val, "onSuccess");
        return self();
    }
    
    public This setOnSuccess(T val) {
        return onSuccess(Functions.constant(val));
    }
    
    /** a failure is when the connection is fine (no exception) but the other end returns a result object V 
     * which the feed can tell indicates a failure (e.g. HTTP code 404) */
    public This onFailure(Function val) {
        this.onfailure = checkNotNull(val, "onFailure");
        return self();
    }

    public This setOnFailure(T val) {
        return onFailure(Functions.constant(val));
    }

    /** registers a callback to be used {@link #onSuccess(Function)} and {@link #onFailure(Function)}, 
     * i.e. whenever a result comes back, but not in case of exceptions being thrown (ie problems communicating) */
    public This onResult(Function val) {
        onSuccess(val);
        return onFailure(val);
    }

    public This setOnResult(T val) {
        return onResult(Functions.constant(val));
    }

    /** @deprecated since 0.6; use {@link #onException(Function)} */
    public This onError(Function val) {
        return onException(val);
    }

    /** an exception is when there is an error in the communication */
    public This onException(Function val) {
        this.onexception = checkNotNull(val, "onException");
        return self();
    }
    
    public This setOnException(T val) {
        return onException(Functions.constant(val));
    }

    /** convenience for indicating a behaviour to occur for both
     * {@link #onException(Function)}
     * (error connecting) and 
     * {@link #onFailure(Function)} 
     * (successful communication but failure report from remote end) */
    public This onFailureOrException(Function val) {
        onFailure(val);
        return onException(val);
    }
    
    public This setOnFailureOrException(T val) {
        return onFailureOrException(Functions.constant(val));
    }

    public boolean hasSuccessHandler() {
        return this.onsuccess != null;
    }

    public boolean hasFailureHandler() {
        return this.onfailure != null;
    }

    public boolean hasExceptionHandler() {
        return this.onexception != null;
    }

    public boolean hasCheckSuccessHandler() {
        return this.checkSuccess != null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy