Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.koushikdutta.ion;
 
 import  com.koushikdutta.async.future.*;
 
Created by koush on 5/25/13.
 
class ParentFuture<T> extends SimpleFuture<T> {
    FutureCallback<T> callbackFan = new FutureCallback<T>() {
        @Override
        public void onCompleted(Exception e, T result) {
            ArrayList<SimpleFuture<T>> futures;
            synchronized (ParentFuture.this) {
                futures = ParentFuture.this.;
                ParentFuture.this. = null;
            }
            if (futures == null)
                return;
            for (SimpleFuture<T> futurefutures) {
                if (e != null)
                    future.setComplete(e);
                else
                    future.setComplete(result);
            }
        }
    };
    private <F extends SimpleFuture<T>> F setupChild(F future) {
        future.setParent(this);
        synchronized (this) {
            if ( == null)
                 = new ArrayList<SimpleFuture<T>>();
            .add(future);
        }
        // invoke the callbacks if necessary
        super.setCallback();
        return future;
    }
    ArrayList<SimpleFuture<T>> callbacks;
    public ParentFuture<T> addChildFuture(SimpleFuture<T> future) {
        setupChild(future);
        return this;
    }
    @Override
    public boolean cancel() {
        synchronized (this) {
            // dont allow cancelling if there are still dependents
            if (hasLiveFutures())
                return false;
            // cancel is possible, so remove the callbacks and call the underlying cancel implementation.
             = null;
            return super.cancel();
        }
    }
    // check the list of child callbacks to see if any exist.
    // remove cancelled ones.
    boolean hasLiveFutures() {
        if ( == null)
            return false;
        ArrayList<SimpleFuture<T>> futures = this.;
        this. = null;
        for (SimpleFuture<T> futurefutures) {
            if (!future.isCancelled()) {
                if (this. == null)
                    this. = new ArrayList<SimpleFuture<T>>();
                this..add(future);
            }
        }
        return this. != null;
    }
    @Override
    public ParentFuture<T> setCallback(FutureCallback <T> callback) {
        throw new IllegalArgumentException("use getChildFuture");
    }
New to GrepCode? Check out our FAQ X