public final class

ConstantObservable<T>

extends java.lang.Object

implements Observable<java.lang.Object>

 java.lang.Object

↳androidx.camera.core.impl.ConstantObservable<T>

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

An Observable whose value is set at construction time and never changes.

Summary

Methods
public voidaddObserver(java.util.concurrent.Executor executor, Observable.Observer<java.lang.Object> observer)

public <any>fetchData()

public voidremoveObserver(Observable.Observer<java.lang.Object> observer)

public static Observable<java.lang.Object>withValue(java.lang.Object value)

Creates an Observable with constant given value.

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

Methods

public static Observable<java.lang.Object> withValue(java.lang.Object value)

Creates an Observable with constant given value.

Parameters:

value: The value which will immediately be sent to observers and is always returned by ConstantObservable.fetchData().

public <any> fetchData()

public void addObserver(java.util.concurrent.Executor executor, Observable.Observer<java.lang.Object> observer)

public void removeObserver(Observable.Observer<java.lang.Object> observer)

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.camera.core.impl;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.impl.utils.futures.Futures;

import com.google.common.util.concurrent.ListenableFuture;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

/**
 * An {@link Observable} whose value is set at construction time and never changes.
 * @param <T> The observed type.
 */
public final class ConstantObservable<T> implements Observable<T> {
    private static final ConstantObservable<Object> NULL_OBSERVABLE =
            new ConstantObservable<>(null);
    private static final String TAG = "ConstantObservable";

    private final ListenableFuture<T> mValueFuture;

    /**
     * Creates an {@link Observable} with constant given value.
     * @param value The value which will immediately be sent to observers and is always returned
     *              by {@link #fetchData()}.
     */
    @NonNull
    public static <U> Observable<U> withValue(@Nullable U value) {
        if (value == null) {
            @SuppressWarnings({"unchecked", "rawtypes"}) // Safe since null can be cast to any type
            Observable<U> typedNull = (Observable) NULL_OBSERVABLE;
            return typedNull;
        }
        return new ConstantObservable<>(value);
    }

    private ConstantObservable(@Nullable T value) {
        mValueFuture = Futures.immediateFuture(value);
    }

    @NonNull
    @Override
    public ListenableFuture<T> fetchData() {
        return mValueFuture;
    }

    @Override
    public void addObserver(@NonNull Executor executor, @NonNull Observer<? super T> observer) {
        // Since the Observable has a constant value, we only will have a one-shot call to the
        // observer, so we don't need to store the observer.
        // ImmediateFuture does not actually store listeners since it is already complete, so it
        // is safe to call addListener() here without leaking.
        mValueFuture.addListener(() -> {
            try {
                observer.onNewData(mValueFuture.get());
            } catch (ExecutionException | InterruptedException e) {
                // Note: this should not be possible as Futures.immediateFuture() should return a
                // future that is already complete and has not failed with an exception.
                observer.onError(e);
            }
        }, executor);
    }

    @Override
    public void removeObserver(@NonNull Observer<? super T> observer) {
        // no-op. addObserver() does not need to store observers.
    }
}