Refactor so that mBridge can not be null and can be final

Reviewed By: astreet

Differential Revision: D2717983

fb-gh-sync-id: 969499eb062d54e0271f910c06c4539e2cb20030
This commit is contained in:
Alexander Blom
2015-12-12 14:19:16 -08:00
committed by facebook-github-bot-7
parent f2d84456e5
commit 8de172e92d
4 changed files with 122 additions and 56 deletions

View File

@@ -12,29 +12,61 @@ package com.facebook.react.common.futures;
import javax.annotation.Nullable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A super simple Future-like class that can safely notify another Thread when a value is ready.
* Does not support setting errors or canceling.
* Does not support canceling.
*/
public class SimpleSettableFuture<T> {
public class SimpleSettableFuture<T> implements Future<T> {
private final CountDownLatch mReadyLatch = new CountDownLatch(1);
private volatile @Nullable T mResult;
private @Nullable T mResult;
private @Nullable Exception mException;
/**
* Sets the result. If another thread has called {@link #get}, they will immediately receive the
* value. Must only be called once.
* value. set or setException must only be called once.
*/
public void set(T result) {
if (mReadyLatch.getCount() == 0) {
throw new RuntimeException("Result has already been set!");
}
checkNotSet();
mResult = result;
mReadyLatch.countDown();
}
/**
* Sets the eception. If another thread has called {@link #get}, they will immediately receive the
* exception. set or setException must only be called once.
*/
public void setException(Exception exception) {
checkNotSet();
mException = exception;
mReadyLatch.countDown();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
throw new UnsupportedOperationException();
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return mReadyLatch.getCount() == 0;
}
@Deprecated
@Override
public T get() throws InterruptedException, ExecutionException {
throw new UnsupportedOperationException("Must use a timeout");
}
/**
* Wait up to the timeout time for another Thread to set a value on this future. If a value has
* already been set, this method will return immediately.
@@ -42,21 +74,26 @@ public class SimpleSettableFuture<T> {
* NB: For simplicity, we catch and wrap InterruptedException. Do NOT use this class if you
* are in the 1% of cases where you actually want to handle that.
*/
public @Nullable T get(long timeoutMS) {
@Override
public @Nullable T get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException {
try {
if (!mReadyLatch.await(timeoutMS, TimeUnit.MILLISECONDS)) {
throw new TimeoutException();
if (!mReadyLatch.await(timeout, unit)) {
throw new TimeoutException("Timed out waiting for result");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (mException != null) {
throw new ExecutionException(mException);
}
return mResult;
}
public static class TimeoutException extends RuntimeException {
public TimeoutException() {
super("Timed out waiting for future");
private void checkNotSet() {
if (mReadyLatch.getCount() == 0) {
throw new RuntimeException("Result has already been set!");
}
}
}