1 /* 2 * Copyright 2021 Google LLC 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 * https://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 com.google.android.enterprise.connectedapps.testapp.wrappers; 17 18 import com.google.android.enterprise.connectedapps.FutureWrapper; 19 import com.google.android.enterprise.connectedapps.Profile; 20 import com.google.android.enterprise.connectedapps.internal.Bundler; 21 import com.google.android.enterprise.connectedapps.internal.BundlerType; 22 import com.google.android.enterprise.connectedapps.internal.CrossProfileCallbackMultiMerger; 23 import com.google.android.enterprise.connectedapps.internal.FutureResultWriter; 24 import com.google.android.enterprise.connectedapps.testapp.SimpleFuture; 25 import java.util.Map; 26 27 /** Wrapper for adding support for {@link SimpleFuture} to the Connected Apps SDK. */ 28 @com.google.android.enterprise.connectedapps.annotations.CustomFutureWrapper( 29 originalType = SimpleFuture.class) 30 public final class SimpleFutureWrapper<E> extends FutureWrapper<E> { 31 32 private final SimpleFuture<E> future = new SimpleFuture<>(); 33 create(Bundler bundler, BundlerType bundlerType)34 public static <E> SimpleFutureWrapper<E> create(Bundler bundler, BundlerType bundlerType) { 35 return new SimpleFutureWrapper<>(bundler, bundlerType); 36 } 37 immediateFailedFuture(Throwable t)38 public static <E> SimpleFuture<E> immediateFailedFuture(Throwable t) { 39 SimpleFuture<E> future = new SimpleFuture<>(); 40 future.setException(t); 41 return future; 42 } 43 SimpleFutureWrapper(Bundler bundler, BundlerType bundlerType)44 private SimpleFutureWrapper(Bundler bundler, BundlerType bundlerType) { 45 super(bundler, bundlerType); 46 } 47 getFuture()48 public SimpleFuture<E> getFuture() { 49 return future; 50 } 51 52 @Override onResult(E result)53 public void onResult(E result) { 54 future.set(result); 55 } 56 57 @Override onException(Throwable throwable)58 public void onException(Throwable throwable) { 59 future.setException(throwable); 60 } 61 writeFutureResult( SimpleFuture<E> future, FutureResultWriter<E> resultWriter)62 public static <E> void writeFutureResult( 63 SimpleFuture<E> future, FutureResultWriter<E> resultWriter) { 64 65 future.setCallback(resultWriter::onSuccess, resultWriter::onFailure); 66 } 67 groupResults( Map<Profile, SimpleFuture<E>> results)68 public static <E> SimpleFuture<Map<Profile, E>> groupResults( 69 Map<Profile, SimpleFuture<E>> results) { 70 SimpleFuture<Map<Profile, E>> m = new SimpleFuture<>(); 71 72 CrossProfileCallbackMultiMerger<E> merger = 73 new CrossProfileCallbackMultiMerger<>(results.size(), m::set); 74 for (Map.Entry<Profile, SimpleFuture<E>> result : results.entrySet()) { 75 result 76 .getValue() 77 .setCallback( 78 (value) -> merger.onResult(result.getKey(), value), 79 (throwable) -> merger.missingResult(result.getKey())); 80 } 81 return m; 82 } 83 } 84