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:
Moxie Marlinspike
2012-12-24 08:40:37 -08:00
parent f685cb550b
commit 9939830551
27 changed files with 521 additions and 284 deletions

View File

@@ -0,0 +1,6 @@
package org.thoughtcrime.securesms.util;
public interface FutureTaskListener<V> {
public void onSuccess(V result);
public void onFailure(Throwable error);
}

View 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;
}
}

View File

@@ -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);
}
}
}
}