Home | History | Annotate | Download | only in concurrent
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.dialer.common.concurrent;
     18 
     19 import android.os.AsyncTask;
     20 import android.support.annotation.NonNull;
     21 import android.support.annotation.Nullable;
     22 import com.android.dialer.common.concurrent.FallibleAsyncTask.FallibleTaskResult;
     23 import com.google.auto.value.AutoValue;
     24 
     25 /**
     26  * A task that runs work in the background, passing Throwables from {@link
     27  * #doInBackground(Object[])} to {@link #onPostExecute(Object)} through a {@link
     28  * FallibleTaskResult}.
     29  *
     30  * @param  the type of the parameters sent to the task upon execution
     31  * @param  the type of the progress units published during the background computation
     32  * @param  the type of the result of the background computation
     33  */
     34 public abstract class FallibleAsyncTask<ParamsT, ProgressT, ResultT>
     35     extends AsyncTask<ParamsT, ProgressT, FallibleTaskResult<ResultT>> {
     36 
     37   @Override
     38   protected final FallibleTaskResult<ResultT> doInBackground(ParamsT... params) {
     39     try {
     40       return FallibleTaskResult.createSuccessResult(doInBackgroundFallible(params));
     41     } catch (Throwable t) {
     42       return FallibleTaskResult.createFailureResult(t);
     43     }
     44   }
     45 
     46   /** Performs background work that may result in a Throwable. */
     47   @Nullable
     48   protected abstract ResultT doInBackgroundFallible(ParamsT... params) throws Throwable;
     49 
     50   /**
     51    * Holds the result of processing from {@link #doInBackground(Object[])}.
     52    *
     53    * @param  the type of the result of the background computation
     54    */
     55   @AutoValue
     56   public abstract static class FallibleTaskResult<ResultT> {
     57 
     58     /** Creates an instance of FallibleTaskResult for the given throwable. */
     59     private static <ResultT> FallibleTaskResult<ResultT> createFailureResult(@NonNull Throwable t) {
     60       return new AutoValue_FallibleAsyncTask_FallibleTaskResult<>(t, null);
     61     }
     62 
     63     /** Creates an instance of FallibleTaskResult for the given result. */
     64     private static <ResultT> FallibleTaskResult<ResultT> createSuccessResult(
     65         @Nullable ResultT result) {
     66       return new AutoValue_FallibleAsyncTask_FallibleTaskResult<>(null, result);
     67     }
     68 
     69     /**
     70      * Returns the Throwable thrown in {@link #doInBackground(Object[])}, or {@code null} if
     71      * background work completed without throwing.
     72      */
     73     @Nullable
     74     public abstract Throwable getThrowable();
     75 
     76     /**
     77      * Returns the result of {@link #doInBackground(Object[])}, which may be {@code null}, or {@code
     78      * null} if the background work threw a Throwable.
     79      *
     80      * <p>Use {@link #isFailure()} to determine if a {@code null} return is the result of a
     81      * Throwable from the background work.
     82      */
     83     @Nullable
     84     public abstract ResultT getResult();
     85 
     86     /**
     87      * Returns {@code true} if this object is the result of background work that threw a Throwable.
     88      */
     89     public boolean isFailure() {
     90       //noinspection ThrowableResultOfMethodCallIgnored
     91       return getThrowable() != null;
     92     }
     93   }
     94 }
     95