public final class

BatchResultCallbackAdapter<K, PlatformValue, JetpackValue>

extends java.lang.Object

 java.lang.Object

↳androidx.appsearch.platformstorage.util.BatchResultCallbackAdapter<K, PlatformValue, JetpackValue>

Gradle dependencies

compile group: 'androidx.appsearch', name: 'appsearch-platform-storage', version: '1.0.0-alpha04'

  • groupId: androidx.appsearch
  • artifactId: appsearch-platform-storage
  • version: 1.0.0-alpha04

Artifact androidx.appsearch:appsearch-platform-storage:1.0.0-alpha04 it located at Google repository (https://maven.google.com/)

Overview

An implementation of the framework API's which return the result as a .

Summary

Constructors
publicBatchResultCallbackAdapter(ResolvableFuture<AppSearchBatchResult> future, java.util.function.Function<java.lang.Object, java.lang.Object> valueMapper)

Methods
public static BatchResultCallbackAdapter<java.lang.Object, java.lang.Object, java.lang.Object>forSameValueType(ResolvableFuture<AppSearchBatchResult> future)

Returns a BatchResultCallbackAdapter where the Platform value is identical to the Jetpack value, needing no transformation.

public voidonResult(<any> platformResult)

public voidonSystemError(java.lang.Throwable t)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public BatchResultCallbackAdapter(ResolvableFuture<AppSearchBatchResult> future, java.util.function.Function<java.lang.Object, java.lang.Object> valueMapper)

Methods

public void onResult(<any> platformResult)

public void onSystemError(java.lang.Throwable t)

public static BatchResultCallbackAdapter<java.lang.Object, java.lang.Object, java.lang.Object> forSameValueType(ResolvableFuture<AppSearchBatchResult> future)

Returns a BatchResultCallbackAdapter where the Platform value is identical to the Jetpack value, needing no transformation.

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * 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 androidx.appsearch.platformstorage.util;

import android.app.appsearch.BatchResultCallback;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.appsearch.app.AppSearchBatchResult;
import androidx.appsearch.platformstorage.converter.AppSearchResultToPlatformConverter;
import androidx.concurrent.futures.ResolvableFuture;
import androidx.core.util.Preconditions;

import java.util.function.Function;

/**
 * An implementation of the framework API's {@link android.app.appsearch.BatchResultCallback} which
 * return the result as a {@link com.google.common.util.concurrent.ListenableFuture}.
 *
 * @param <K>             The type of key in the batch result (both Framework and Jetpack)
 * @param <PlatformValue> The type of value in the Framework's
 *                        {@link android.app.appsearch.AppSearchBatchResult}.
 * @param <JetpackValue>  The type of value in Jetpack's {@link AppSearchBatchResult}.
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@RequiresApi(Build.VERSION_CODES.S)
public final class BatchResultCallbackAdapter<K, PlatformValue, JetpackValue>
        implements BatchResultCallback<K, PlatformValue> {
    private final ResolvableFuture<AppSearchBatchResult<K, JetpackValue>> mFuture;
    private final Function<PlatformValue, JetpackValue> mValueMapper;

    public BatchResultCallbackAdapter(
            @NonNull ResolvableFuture<AppSearchBatchResult<K, JetpackValue>> future,
            @NonNull Function<PlatformValue, JetpackValue> valueMapper) {
        mFuture = Preconditions.checkNotNull(future);
        mValueMapper = Preconditions.checkNotNull(valueMapper);
    }

    @Override
    public void onResult(
            @NonNull android.app.appsearch.AppSearchBatchResult<K, PlatformValue> platformResult) {
        AppSearchBatchResult<K, JetpackValue> jetpackResult =
                AppSearchResultToPlatformConverter.platformAppSearchBatchResultToJetpack(
                        platformResult, mValueMapper);
        mFuture.set(jetpackResult);
    }

    @Override
    public void onSystemError(@Nullable Throwable t) {
        mFuture.setException(t);
    }

    /**
     * Returns a {@link androidx.appsearch.platformstorage.util.BatchResultCallbackAdapter} where
     * the Platform value is identical to the Jetpack value, needing no transformation.
     */
    @NonNull
    public static <K, V> BatchResultCallbackAdapter<K, V, V> forSameValueType(
            @NonNull ResolvableFuture<AppSearchBatchResult<K, V>> future) {
        return new BatchResultCallbackAdapter<>(future, Function.identity());
    }
}