Observables.java revision e919a48fb40b9d6c698a495acf40adbc0e320431
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.List; 24import java.util.concurrent.Executor; 25 26import javax.annotation.CheckReturnValue; 27import javax.annotation.Nonnull; 28import javax.annotation.Nullable; 29import javax.annotation.ParametersAreNonnullByDefault; 30 31/** 32 * Helper methods for {@link Observable}. 33 */ 34@ParametersAreNonnullByDefault 35public class Observables { 36 private static final SafeCloseable NOOP_CALLBACK_HANDLE = new SafeCloseable() { 37 @Override 38 public void close() { 39 // Do Nothing. 40 } 41 }; 42 43 private Observables() { 44 } 45 46 /** 47 * Transforms an observable with a function. 48 * 49 * @return The transformed observable. 50 */ 51 public static <F, T> Observable<T> transform(final Observable<F> input, 52 final Function<F, T> function) { 53 return new Observable<T>() { 54 @Nonnull 55 @Override 56 public T get() { 57 return function.apply(input.get()); 58 } 59 60 @CheckReturnValue 61 @Nonnull 62 @Override 63 public SafeCloseable addCallback(final Callback<T> callback, 64 Executor executor) { 65 return input.addCallback(new Callback<F>() { 66 @Override 67 public void onCallback(F result) { 68 callback.onCallback(function.apply(result)); 69 } 70 }, executor); 71 } 72 }; 73 } 74 75 /** 76 * Transforms a set of observables with a function. 77 * 78 * @return The transformed observable. 79 */ 80 public static <F, T> Observable<T> transform(final List<? extends Observable<F>> input, 81 Function<List<F>, T> function) { 82 return ObservableCombiner.transform(input, function); 83 } 84 85 /** 86 * @return An observable which has the given constant value. 87 */ 88 @Nonnull 89 public static <T> Observable<T> of(final @Nullable T constant) { 90 return new Observable<T>() { 91 @Nonnull 92 @Override 93 public T get() { 94 return constant; 95 } 96 97 @CheckReturnValue 98 @Nonnull 99 @Override 100 public SafeCloseable addCallback(Callback<T> callback, Executor executor) { 101 return NOOP_CALLBACK_HANDLE; 102 } 103 }; 104 } 105 106 @Nonnull 107 @CheckReturnValue 108 public static <T> SafeCloseable addThreadSafeCallback(Observable<T> observable, 109 final Updatable<T> callback) { 110 return observable.addCallback(new Callback<T>() { 111 @Override 112 public void onCallback(T result) { 113 callback.update(result); 114 } 115 }, MoreExecutors.sameThreadExecutor()); 116 } 117} 118