Home | History | Annotate | Download | only in guava
      1 /*
      2  * Copyright 2018 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 package androidx.room.guava;
     17 
     18 import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
     19 
     20 import androidx.annotation.RestrictTo;
     21 import androidx.arch.core.executor.ArchTaskExecutor;
     22 import androidx.room.RoomSQLiteQuery;
     23 
     24 import com.google.common.util.concurrent.FutureCallback;
     25 import com.google.common.util.concurrent.Futures;
     26 import com.google.common.util.concurrent.ListenableFuture;
     27 import com.google.common.util.concurrent.ListenableFutureTask;
     28 
     29 import java.util.concurrent.Callable;
     30 
     31 /**
     32  * A class to hold static methods used by code generation in Room's Guava compatibility library.
     33  *
     34  * @hide
     35  */
     36 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     37 @SuppressWarnings("unused") // Used in GuavaListenableFutureQueryResultBinder code generation.
     38 public class GuavaRoom {
     39 
     40     private GuavaRoom() {}
     41 
     42     /**
     43      * Returns a {@link ListenableFuture<T>} created by submitting the input {@code callable} to
     44      * {@link ArchTaskExecutor}'s background-threaded Executor.
     45      */
     46     public static <T> ListenableFuture<T> createListenableFuture(
     47             final Callable<T> callable,
     48             final RoomSQLiteQuery query,
     49             final boolean releaseQuery) {
     50         ListenableFutureTask<T> listenableFutureTask = ListenableFutureTask.create(callable);
     51         ArchTaskExecutor.getInstance().executeOnDiskIO(listenableFutureTask);
     52 
     53         if (releaseQuery) {
     54             Futures.addCallback(
     55                     listenableFutureTask,
     56                     new FutureCallback<T>() {
     57                         @Override
     58                         public void onSuccess(T t) {
     59                             query.release();
     60                         }
     61 
     62                         @Override
     63                         public void onFailure(Throwable throwable) {
     64                             query.release();
     65                         }
     66                     },
     67                     directExecutor());
     68         }
     69 
     70         return listenableFutureTask;
     71     }
     72 }
     73