Commit 84e13e3b by ibaker Committed by Oliver Woodman

Replace media2 SettableFuture with Guava version

PiperOrigin-RevId: 322530026
parent 3191afe8
......@@ -24,6 +24,7 @@ import androidx.media2.common.SessionPlayer;
import androidx.media2.common.SessionPlayer.PlayerResult;
import com.google.android.exoplayer2.util.Log;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......
......@@ -35,6 +35,7 @@ import com.google.android.exoplayer2.PlaybackPreparer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.util.Assertions;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.ext.media2;
import androidx.concurrent.futures.CallbackToFutureAdapter;
import com.google.android.exoplayer2.util.Assertions;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
/**
* A replacement of com.google.common.util.concurrent.SettableFuture with CallbackToFutureAdapter to
* avoid the dependency on Guava.
*/
@SuppressWarnings("ShouldNotSubclass")
/* package */ class SettableFuture<V> implements ListenableFuture<V> {
static <V> SettableFuture<V> create() {
return new SettableFuture<>();
}
private final ListenableFuture<V> future;
private final CallbackToFutureAdapter.Completer<V> completer;
SettableFuture() {
AtomicReference<CallbackToFutureAdapter.Completer<V>> completerRef = new AtomicReference<>();
future =
CallbackToFutureAdapter.getFuture(
completer -> {
completerRef.set(completer);
return null;
});
completer = Assertions.checkNotNull(completerRef.get());
}
@Override
public void addListener(Runnable listener, Executor executor) {
Assertions.checkNotNull(listener);
Assertions.checkNotNull(executor);
future.addListener(listener, executor);
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return future.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return future.isCancelled();
}
@Override
public boolean isDone() {
return future.isDone();
}
@Override
public V get() throws ExecutionException, InterruptedException {
return future.get();
}
@Override
public V get(long timeout, TimeUnit unit)
throws ExecutionException, InterruptedException, TimeoutException {
return future.get(timeout, unit);
}
void set(V value) {
completer.set(value);
}
void setException(Throwable throwable) {
completer.setException(throwable);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment