Create a core-util module with some common utilities.

This commit is contained in:
Greyson Parrelli
2020-12-04 18:31:58 -05:00
parent 831cd2f297
commit 8e93bf9075
958 changed files with 1879 additions and 2035 deletions

View File

@@ -24,6 +24,6 @@ dependencyVerification {
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation project(':core-util')
testImplementation 'junit:junit:4.12'
}

View File

@@ -26,7 +26,6 @@ class DataStatus {
return new DataStatus(size, bitset);
}
private DataStatus(int size, @NonNull BitSet bitset) {
this.size = size;
this.state = bitset;

View File

@@ -1,16 +1,13 @@
package org.signal.paging;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import org.signal.paging.util.LinkedBlockingLifoQueue;
import org.signal.core.util.concurrent.SignalExecutors;
import org.signal.core.util.logging.Log;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* The workhorse of managing page requests.
@@ -24,7 +21,7 @@ class FixedSizePagingController<E> implements PagingController {
private static final String TAG = FixedSizePagingController.class.getSimpleName();
private static final Executor FETCH_EXECUTOR = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingLifoQueue<>(), r -> new Thread(r, "signal-FixedSizedPagingController"));
private static final Executor FETCH_EXECUTOR = SignalExecutors.newFixedLifoThreadExecutor("signal-FixedSizePagingController", 1, 1);
private static final boolean DEBUG = false;
private final PagedDataSource<E> dataSource;

View File

@@ -2,6 +2,8 @@ package org.signal.paging;
import androidx.annotation.NonNull;
import java.util.concurrent.Executor;
/**
* Describes various properties of how you'd like paging to be handled.
*/

View File

@@ -1,23 +0,0 @@
package org.signal.paging.util;
import java.util.concurrent.LinkedBlockingDeque;
public class LinkedBlockingLifoQueue<E> extends LinkedBlockingDeque<E> {
@Override
public void put(E runnable) throws InterruptedException {
super.putFirst(runnable);
}
@Override
public boolean add(E runnable) {
super.addFirst(runnable);
return true;
}
@Override
public boolean offer(E runnable) {
super.addFirst(runnable);
return true;
}
}

View File

@@ -1,55 +0,0 @@
package org.signal.paging.util;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import java.util.concurrent.CountDownLatch;
public final class Util {
private static volatile Handler handler;
private Util() {}
public static void runOnMain(final @NonNull Runnable runnable) {
if (isMainThread()) runnable.run();
else getHandler().post(runnable);
}
public static void runOnMainSync(final @NonNull Runnable runnable) {
if (isMainThread()) {
runnable.run();
} else {
final CountDownLatch sync = new CountDownLatch(1);
runOnMain(() -> {
try {
runnable.run();
} finally {
sync.countDown();
}
});
try {
sync.await();
} catch (InterruptedException ie) {
throw new AssertionError(ie);
}
}
}
public static boolean isMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
private static Handler getHandler() {
if (handler == null) {
synchronized (Util.class) {
if (handler == null) {
handler = new Handler(Looper.getMainLooper());
}
}
}
return handler;
}
}