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 17 package com.android.camera.async; 18 19 import com.android.camera.util.Callback; 20 import com.google.common.base.Function; 21 import com.google.common.base.Supplier; 22 import com.google.common.util.concurrent.MoreExecutors; 23 24 import java.util.List; 25 import java.util.concurrent.Executor; 26 27 import javax.annotation.CheckReturnValue; 28 import javax.annotation.Nonnull; 29 import javax.annotation.ParametersAreNonnullByDefault; 30 31 /** 32 * Helper methods for {@link Observable}. 33 */ 34 @ParametersAreNonnullByDefault 35 public class Observables { 36 private static final SafeCloseable NOOP_CALLBACK_HANDLE = new SafeCloseable() { 37 @Override 38 public void close() { 39 // Do Nothing. 40 } 41 }; 42 Observables()43 private Observables() { 44 } 45 46 /** 47 * Transforms an observable with a function. 48 * 49 * @return The transformed observable. 50 */ transform(final Observable<F> input, final Function<F, T> function)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(Runnable callback, Executor executor) { 64 return input.addCallback(callback, executor); 65 } 66 }; 67 } 68 69 /** 70 * @return An observable which recomputes its output every time an input changes. 71 */ 72 public static <T> Observable<T> transform(final List<? extends Observable<?>> inputs, 73 final Supplier<T> output) { 74 return ObservableCombiner.transform(inputs, output); 75 } 76 77 /** 78 * Transforms a set of observables with a function. 79 * 80 * @return The transformed observable. 81 */ 82 public static <F, T> Observable<T> transform(final List<? extends Observable<F>> input, 83 Function<List<F>, T> function) { 84 return ObservableCombiner.transform(input, function); 85 } 86 87 /** 88 * @return An observable which has the given constant value. 89 */ 90 @Nonnull 91 public static <T> Observable<T> of(final @Nonnull T constant) { 92 return new Observable<T>() { 93 @Nonnull 94 @Override 95 public T get() { 96 return constant; 97 } 98 99 @CheckReturnValue 100 @Nonnull 101 @Override 102 public SafeCloseable addCallback(Runnable callback, Executor executor) { 103 return NOOP_CALLBACK_HANDLE; 104 } 105 }; 106 } 107 108 @Nonnull 109 @CheckReturnValue 110 public static <T> SafeCloseable addThreadSafeCallback(final Observable<T> observable, 111 final Updatable<T> callback) { 112 return observable.addCallback(new Runnable() { 113 @Override 114 public void run() { 115 callback.update(observable.get()); 116 } 117 }, MoreExecutors.sameThreadExecutor()); 118 } 119 } 120