mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 10:02:23 +00:00
Refactor recipient access.
1) Refactor recipient class to support asynchronous loading operations. 2) Refactor recipient factory to simplify recipient access. 3) Consoliate everything into one recipient provider that is capable of doing async lookups and intelligent caching.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
public interface FutureTaskListener<V> {
|
||||
public void onSuccess(V result);
|
||||
public void onFailure(Throwable error);
|
||||
}
|
||||
18
src/org/thoughtcrime/securesms/util/LRUCache.java
Normal file
18
src/org/thoughtcrime/securesms/util/LRUCache.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LRUCache<K,V> extends LinkedHashMap<K,V> {
|
||||
|
||||
private final int maxSize;
|
||||
|
||||
public LRUCache(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry (Map.Entry<K,V> eldest) {
|
||||
return size() > maxSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class ListenableFutureTask<V> extends FutureTask<V> {
|
||||
|
||||
private FutureTaskListener<V> listener;
|
||||
|
||||
public ListenableFutureTask(Callable<V> callable, FutureTaskListener<V> listener) {
|
||||
super(callable);
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public synchronized void setListener(FutureTaskListener<V> listener) {
|
||||
this.listener = listener;
|
||||
if (this.isDone()) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void done() {
|
||||
callback();
|
||||
}
|
||||
|
||||
private void callback() {
|
||||
if (this.listener != null) {
|
||||
try {
|
||||
this.listener.onSuccess(get());
|
||||
} catch (ExecutionException ee) {
|
||||
this.listener.onFailure(ee);
|
||||
} catch (InterruptedException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user