Observables.java revision 875d9fff24e283efa5d95ad75c3fab074e489fa4
1/*
2 * Copyright (C) 2015 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
17package com.android.camera.async;
18
19import com.android.camera.util.Callback;
20import com.google.common.base.Function;
21import com.google.common.util.concurrent.MoreExecutors;
22
23import java.util.concurrent.Executor;
24
25import javax.annotation.CheckReturnValue;
26import javax.annotation.Nonnull;
27import javax.annotation.Nullable;
28
29/**
30 * Helper methods for {@link Observable}.
31 */
32public class Observables {
33    private Observables() {
34    }
35
36    /**
37     * Transforms an observable with a function.
38     *
39     * @return The transformed observable.
40     */
41    public static <F, T> Observable<T> transform(final Observable<F> input,
42            final Function<F, T> function) {
43        return new Observable<T>() {
44            @Override
45            public T get() {
46                return function.apply(input.get());
47            }
48
49            @Override
50            public SafeCloseable addCallback(final Callback<T> callback, Executor executor) {
51                return input.addCallback(new Callback<F>() {
52                    @Override
53                    public void onCallback(F result) {
54                        callback.onCallback(function.apply(result));
55                    }
56                }, executor);
57            }
58        };
59    }
60
61    /**
62     * @return An observable which has the given constant value.
63     */
64    @Nonnull
65    public static <T> Observable<T> of(final @Nullable T constant) {
66        return new Observable<T>() {
67            @Override
68            public T get() {
69                return constant;
70            }
71
72            @Override
73            public SafeCloseable addCallback(Callback<T> callback, Executor executor) {
74                return new SafeCloseable() {
75                    @Override
76                    public void close() {
77                    }
78                };
79            }
80        };
81    }
82
83    @Nonnull
84    @CheckReturnValue
85    public static <T> SafeCloseable addThreadSafeCallback(
86            @Nonnull Observable<T> observable,
87            final @Nonnull Updatable<T> callback) {
88        return observable.addCallback(new Callback<T>() {
89            @Override
90            public void onCallback(T result) {
91                callback.update(result);
92            }
93        }, MoreExecutors.sameThreadExecutor());
94    }
95}
96