Android Open Source - BusWear Async Executor






From Project

Back to project page BusWear.

License

The source code is released under:

Apache License

If you think the Android project BusWear 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

/*
 * Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
 *// w  w  w  .ja v a 2s .  c  o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.tajchert.buswear.util;

import android.app.Activity;
import android.util.Log;

import java.lang.reflect.Constructor;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import pl.tajchert.buswear.EventBus;


/**
 * Executes an {@link pl.tajchert.buswear.util.AsyncExecutor.RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events of any
 * given type (default is {@link ThrowableFailureEvent}).
 * 
 * @author Markus
 */
public class AsyncExecutor {

    public static class Builder {
        private Executor threadPool;
        private Class<?> failureEventType;
        private EventBus eventBus;

        private Builder() {
        }

        public Builder threadPool(Executor threadPool) {
            this.threadPool = threadPool;
            return this;
        }

        public Builder failureEventType(Class<?> failureEventType) {
            this.failureEventType = failureEventType;
            return this;
        }

        public Builder eventBus(EventBus eventBus) {
            this.eventBus = eventBus;
            return this;
        }

        public AsyncExecutor build() {
            return buildForScope(null);
        }

        public AsyncExecutor buildForActivityScope(Activity activity) {
            return buildForScope(activity.getClass());
        }
        
        public AsyncExecutor buildForScope(Object executionContext) {
            if (eventBus == null) {
                eventBus = EventBus.getDefault();
            }
            if (threadPool == null) {
                threadPool = Executors.newCachedThreadPool();
            }
            if (failureEventType == null) {
                failureEventType = ThrowableFailureEvent.class;
            }
            return new AsyncExecutor(threadPool, eventBus, failureEventType, executionContext);
        }
    }

    /** Like {@link Runnable}, but the run method may throw an exception. */
    public interface RunnableEx {
        void run() throws Exception;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static AsyncExecutor create() {
        return new Builder().build();
    }

    private final Executor threadPool;
    private final Constructor<?> failureEventConstructor;
    private final EventBus eventBus;
    private final Object scope;

    private AsyncExecutor(Executor threadPool, EventBus eventBus, Class<?> failureEventType, Object scope) {
        this.threadPool = threadPool;
        this.eventBus = eventBus;
        this.scope = scope;
        try {
            failureEventConstructor = failureEventType.getConstructor(Throwable.class);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(
                    "Failure event class must have a constructor with one parameter of type Throwable", e);
        }
    }

    /** Posts an failure event if the given {@link pl.tajchert.buswear.util.AsyncExecutor.RunnableEx} throws an Exception. */
    public void execute(final RunnableEx runnable) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (Exception e) {
                    Object event;
                    try {
                        event = failureEventConstructor.newInstance(e);
                    } catch (Exception e1) {
                        Log.e(EventBus.TAG, "Original exception:", e);
                        throw new RuntimeException("Could not create failure event", e1);
                    }
                    if (event instanceof HasExecutionScope) {
                        ((HasExecutionScope) event).setExecutionScope(scope);
                    }
                    eventBus.postLocal(event);
                }
            }
        });
    }

}




Java Source Code List

pl.tajchert.buswear.AsyncPoster.java
pl.tajchert.buswear.BackgroundPoster.java
pl.tajchert.buswear.EventBusBuilder.java
pl.tajchert.buswear.EventBusException.java
pl.tajchert.buswear.EventBus.java
pl.tajchert.buswear.HandlerPoster.java
pl.tajchert.buswear.NoSubscriberEvent.java
pl.tajchert.buswear.PendingPostQueue.java
pl.tajchert.buswear.PendingPost.java
pl.tajchert.buswear.SubscriberExceptionEvent.java
pl.tajchert.buswear.SubscriberMethodFinder.java
pl.tajchert.buswear.SubscriberMethod.java
pl.tajchert.buswear.Subscription.java
pl.tajchert.buswear.ThreadMode.java
pl.tajchert.buswear.sample.CustomObject.java
pl.tajchert.buswear.sample.CustomObject.java
pl.tajchert.buswear.sample.MainMobileActivity.java
pl.tajchert.buswear.sample.MainWearActivity.java
pl.tajchert.buswear.util.AsyncExecutor.java
pl.tajchert.buswear.util.ErrorDialogConfig.java
pl.tajchert.buswear.util.ErrorDialogFragmentFactory.java
pl.tajchert.buswear.util.ErrorDialogFragments.java
pl.tajchert.buswear.util.ErrorDialogManager.java
pl.tajchert.buswear.util.ExceptionToResourceMapping.java
pl.tajchert.buswear.util.HasExecutionScope.java
pl.tajchert.buswear.util.ThrowableFailureEvent.java
pl.tajchert.buswear.wear.EventCatcher.java
pl.tajchert.buswear.wear.SendByteArrayToNode.java
pl.tajchert.buswear.wear.SendCommandToNode.java
pl.tajchert.buswear.wear.SendWearManager.java
pl.tajchert.buswear.wear.WearBusTools.java