Android: Re-implement a subset of CompletableFuture

Imagine if Android phones actually got updates
This commit is contained in:
JosJuice 2021-01-18 13:06:40 +01:00
parent 347551a01d
commit 12aa1071cb
2 changed files with 96 additions and 4 deletions

View File

@ -19,12 +19,12 @@ import org.dolphinemu.dolphinemu.model.GameFileCache;
import org.dolphinemu.dolphinemu.services.GameFileCacheService;
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner;
import org.dolphinemu.dolphinemu.utils.BooleanSupplier;
import org.dolphinemu.dolphinemu.utils.CompletableFuture;
import org.dolphinemu.dolphinemu.utils.ContentHandler;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.WiiUtils;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
@ -171,9 +171,6 @@ public final class MainPresenter
public void importWiiSave(String path)
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N)
return; // TODO
final Activity mainPresenterActivity = (Activity) mContext;
CompletableFuture<Boolean> canOverwriteFuture = new CompletableFuture<>();

View File

@ -0,0 +1,95 @@
package org.dolphinemu.dolphinemu.utils;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Simplified re-implementation of a subset of {@link java.util.concurrent.CompletableFuture}.
* Replace this class with that class once we have full Java 8 support (once we require API 24).
*/
public class CompletableFuture<T> implements Future<T>
{
private final Lock lock = new ReentrantLock();
private final Condition done = lock.newCondition();
private boolean isDone = false;
private T result = null;
@Override
public boolean cancel(boolean mayInterruptIfRunning)
{
throw new UnsupportedOperationException();
}
@Override
public boolean isCancelled()
{
return false;
}
@Override
public boolean isDone()
{
return isDone;
}
@Override
public T get() throws ExecutionException, InterruptedException
{
lock.lock();
try
{
while (!isDone)
done.await();
return result;
}
finally
{
lock.unlock();
}
}
@Override
public T get(long timeout, TimeUnit unit)
throws ExecutionException, InterruptedException, TimeoutException
{
lock.lock();
try
{
while (!isDone)
{
if (!done.await(timeout, unit))
throw new TimeoutException();
}
return result;
}
finally
{
lock.unlock();
}
}
public boolean complete(T value)
{
lock.lock();
try
{
boolean wasDone = isDone;
result = value;
isDone = true;
done.signalAll();
return !wasDone;
}
finally
{
lock.unlock();
}
}
}