Android Open Source - Buddy-Android-SDK Buddy Future






From Project

Back to project page Buddy-Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Buddy-Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.buddy.sdk;
/*  ww w  .  ja v a  2 s .c  om*/
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by shawn on 7/7/14.
 */
class BuddyFuture<V> implements Future<V> {

    private boolean done;
    private V value;
    private CountDownLatch waitHandle = new CountDownLatch(1);

    private Object SyncObj = new Object();

    private class CallbackWrapper {
        public BuddyFutureCallback callback;
        public BuddyFuture<V> handle;
    }

    private List<CallbackWrapper> callbacks;

    public BuddyFuture() {

    }

    public BuddyFuture<V> continueWith(final BuddyFutureCallback<V> callback) {

        if (!done) {
            if (callbacks == null) {
                callbacks = new ArrayList<CallbackWrapper>();
            }

            CallbackWrapper wrapper = new CallbackWrapper();
            wrapper.callback = callback;
            wrapper.handle = new BuddyFuture<V>();
            callbacks.add(wrapper);
            return wrapper.handle;

        } else {
            callback.completed(this);
        }
        return this;
    }



    public void setValue(V val) {
        synchronized (SyncObj) {
            value = val;

            if (!done) {
                done = true;
                if (callbacks != null) {
                    for (CallbackWrapper cb : callbacks) {
                        cb.handle.setValue(val);
                    }
                }
                SyncObj.notify();
            }
        }

    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return done;
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {
        synchronized (SyncObj) {
            if (!done) {
                SyncObj.wait();
            }
        }
        return value;
    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        if (!done) {
            synchronized (SyncObj) {
                SyncObj.wait(TimeUnit.MILLISECONDS.convert(timeout, unit));
            }
        }
        return value;
    }
}




Java Source Code List

chat.sample.buddy.com.buddychat.ApplicationTest.java
chat.sample.buddy.com.buddychat.BuddyChatApplication.java
chat.sample.buddy.com.buddychat.Chat.java
chat.sample.buddy.com.buddychat.GcmBroadcastReceiver.java
chat.sample.buddy.com.buddychat.GcmIntentService.java
chat.sample.buddy.com.buddychat.GetCurrentUserCallback.java
chat.sample.buddy.com.buddychat.Loading.java
chat.sample.buddy.com.buddychat.Login.java
chat.sample.buddy.com.buddychat.MainScreen.java
chat.sample.buddy.com.buddychat.Signup.java
chat.sample.buddy.com.buddychat.SimpleAdapter.java
com.buddy.sample.buddychat.LoginActivity.java
com.buddy.sdk.AccessTokenCallback.java
com.buddy.sdk.BuddyCallback.java
com.buddy.sdk.BuddyClientImpl.java
com.buddy.sdk.BuddyClientOptions.java
com.buddy.sdk.BuddyClient.java
com.buddy.sdk.BuddyDateDeserializer.java
com.buddy.sdk.BuddyFile.java
com.buddy.sdk.BuddyFutureCallback.java
com.buddy.sdk.BuddyFuture.java
com.buddy.sdk.BuddyLocationDeserializer.java
com.buddy.sdk.BuddyLocationRangeSerializer.java
com.buddy.sdk.BuddyLocationSerializer.java
com.buddy.sdk.BuddyResultBase.java
com.buddy.sdk.BuddyResult.java
com.buddy.sdk.BuddyServiceClient.java
com.buddy.sdk.Buddy.java
com.buddy.sdk.DateRangeSerializer.java
com.buddy.sdk.DateRange.java
com.buddy.sdk.JsonEnvelopeBase.java
com.buddy.sdk.JsonEnvelopeDeserializer.java
com.buddy.sdk.JsonEnvelope.java
com.buddy.sdk.UserAuthenticationRequiredCallback.java
com.buddy.sdk.models.AlbumItem.java
com.buddy.sdk.models.Album.java
com.buddy.sdk.models.BinaryModelBase.java
com.buddy.sdk.models.Blob.java
com.buddy.sdk.models.BuddyLocation.java
com.buddy.sdk.models.Checkin.java
com.buddy.sdk.models.Identity.java
com.buddy.sdk.models.LocationRange.java
com.buddy.sdk.models.Message.java
com.buddy.sdk.models.MetadataItem.java
com.buddy.sdk.models.ModelBase.java
com.buddy.sdk.models.NotificationResult.java
com.buddy.sdk.models.PagedResult.java
com.buddy.sdk.models.Picture.java
com.buddy.sdk.models.SocialNetworkUser.java
com.buddy.sdk.models.TimedMetric.java
com.buddy.sdk.models.UserListItem.java
com.buddy.sdk.models.UserList.java
com.buddy.sdk.models.User.java
com.buddy.sdk.models.Video.java