ConcurrentState.java revision 9ad0984f36ff9cd133c61c4e979032988b77a995
1/* 2 * Copyright (C) 2014 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 java.util.ArrayList; 20import java.util.HashSet; 21import java.util.List; 22import java.util.Set; 23import java.util.concurrent.Executor; 24 25import com.android.camera.util.Callback; 26 27/** 28 * Generic asynchronous state wrapper which supports two methods of interaction: 29 * polling for the latest value and listening for updates. 30 * <p> 31 * Note that this class only supports polling and using listeners. If 32 * synchronous consumption of state changes is required, see 33 * {@link FutureResult} or {@link BufferQueue} and its implementations. 34 * </p> 35 */ 36public class ConcurrentState<T> implements Updatable<T>, Observable<T> { 37 38 private static class ExecutorListenerPair<T> { 39 private final Executor mExecutor; 40 private final Callback<T> mListener; 41 42 public ExecutorListenerPair(Executor executor, Callback<T> listener) { 43 mExecutor = executor; 44 mListener = listener; 45 } 46 47 /** 48 * Runs the callback on the executor with the given value. 49 */ 50 public void run(final T t) { 51 mExecutor.execute(new Runnable() { 52 public void run() { 53 mListener.onCallback(t); 54 } 55 }); 56 } 57 } 58 59 private final Object mLock; 60 private final Set<ExecutorListenerPair<T>> mListeners; 61 private T mValue; 62 63 public ConcurrentState(T initialValue) { 64 mLock = new Object(); 65 mListeners = new HashSet<>(); 66 mValue = initialValue; 67 } 68 69 /** 70 * Updates the state to the latest value, notifying all listeners. 71 */ 72 @Override 73 public void update(T newValue) { 74 List<ExecutorListenerPair<T>> listeners = new ArrayList<>(); 75 synchronized (mLock) { 76 mValue = newValue; 77 // Copy listeners out here so we can iterate over the list outside 78 // the critical section. 79 listeners.addAll(mListeners); 80 } 81 for (ExecutorListenerPair<T> pair : listeners) { 82 pair.run(newValue); 83 } 84 } 85 86 @Override 87 public SafeCloseable addCallback(Callback callback, Executor executor) { 88 synchronized (mLock) { 89 final ExecutorListenerPair<T> pair = new ExecutorListenerPair<>(executor, callback); 90 mListeners.add(pair); 91 92 return new SafeCloseable() { 93 @Override 94 public void close() { 95 synchronized (mLock) { 96 mListeners.remove(pair); 97 } 98 } 99 }; 100 } 101 } 102 103 /** 104 * Polls for the latest value. 105 * 106 * @return The latest state. 107 */ 108 @Override 109 public T get() { 110 synchronized (mLock) { 111 return mValue; 112 } 113 } 114} 115