Skip to content

Commit dba0e43

Browse files
committed
SimpleFuture: Add silent cancellation (no callback).
This will unblock any calls to .get() FileCache: key enumeration.
1 parent 63f931e commit dba0e43

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

AndroidAsync/src/com/koushikdutta/async/future/SimpleFuture.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
import java.util.concurrent.TimeoutException;
99

1010
public class SimpleFuture<T> extends SimpleCancellable implements DependentFuture<T> {
11+
AsyncSemaphore waiter;
12+
Exception exception;
13+
T result;
14+
boolean silent;
15+
FutureCallback<T> callback;
16+
1117
@Override
1218
public boolean cancel(boolean mayInterruptIfRunning) {
1319
return cancel();
1420
}
1521

16-
@Override
17-
public boolean cancel() {
22+
private boolean cancelInternal(boolean silent) {
1823
if (!super.cancel())
1924
return false;
2025
// still need to release any pending waiters
@@ -23,12 +28,21 @@ public boolean cancel() {
2328
exception = new CancellationException();
2429
releaseWaiterLocked();
2530
callback = handleCompleteLocked();
31+
this.silent = silent;
2632
}
2733
handleCallbackUnlocked(callback);
2834
return true;
2935
}
3036

31-
AsyncSemaphore waiter;
37+
public boolean cancelSilently() {
38+
return cancelInternal(true);
39+
}
40+
41+
@Override
42+
public boolean cancel() {
43+
return cancelInternal(silent);
44+
}
45+
3246
@Override
3347
public T get() throws InterruptedException, ExecutionException {
3448
AsyncSemaphore waiter;
@@ -40,7 +54,7 @@ public T get() throws InterruptedException, ExecutionException {
4054
waiter.acquire();
4155
return getResult();
4256
}
43-
57+
4458
private T getResult() throws ExecutionException {
4559
if (exception != null)
4660
throw new ExecutionException(exception);
@@ -59,7 +73,7 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution
5973
throw new TimeoutException();
6074
return getResult();
6175
}
62-
76+
6377
@Override
6478
public boolean setComplete() {
6579
return setComplete((T)null);
@@ -78,7 +92,7 @@ private FutureCallback<T> handleCompleteLocked() {
7892
}
7993

8094
private void handleCallbackUnlocked(FutureCallback<T> callback) {
81-
if (callback != null)
95+
if (callback != null && !silent)
8296
callback.onCompleted(exception, result);
8397
}
8498

@@ -95,12 +109,10 @@ AsyncSemaphore ensureWaiterLocked() {
95109
return waiter;
96110
}
97111

98-
Exception exception;
99112
public boolean setComplete(Exception e) {
100113
return setComplete(e, null);
101114
}
102115

103-
T result;
104116
public boolean setComplete(T value) {
105117
return setComplete(null, value);
106118
}
@@ -134,7 +146,10 @@ public SimpleFuture<T> setComplete(Future<T> future) {
134146
return this;
135147
}
136148

137-
FutureCallback<T> callback;
149+
// TEST USE ONLY!
150+
public FutureCallback<T> getCallback() {
151+
return callback;
152+
}
138153

139154
@Override
140155
public SimpleFuture<T> setCallback(FutureCallback<T> callback) {
@@ -150,11 +165,6 @@ public SimpleFuture<T> setCallback(FutureCallback<T> callback) {
150165
return this;
151166
}
152167

153-
// TEST USE ONLY!
154-
public FutureCallback<T> getCallback() {
155-
return callback;
156-
}
157-
158168
@Override
159169
public <C extends FutureCallback<T>> C then(C callback) {
160170
if (callback instanceof DependentCancellable)
@@ -180,6 +190,7 @@ public SimpleFuture<T> reset() {
180190
exception = null;
181191
waiter = null;
182192
callback = null;
193+
silent = false;
183194

184195
return this;
185196
}

AndroidAsync/src/com/koushikdutta/async/util/FileCache.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.Comparator;
12+
import java.util.HashSet;
1213
import java.util.Random;
14+
import java.util.Set;
1315

1416
/**
1517
* Created by koush on 4/12/14.
@@ -249,6 +251,20 @@ public void clear() {
249251
cache.evictAll();
250252
}
251253

254+
public Set<String> keySet() {
255+
HashSet<String> ret = new HashSet<String>();
256+
File[] files = directory.listFiles();
257+
if (files == null)
258+
return ret;
259+
for (File file: files) {
260+
String name = file.getName();
261+
int last = name.lastIndexOf('.');
262+
if (last != -1)
263+
ret.add(name.substring(0, last));
264+
}
265+
return ret;
266+
}
267+
252268
public void setMaxSize(long maxSize) {
253269
cache.setMaxSize(maxSize);
254270
}

0 commit comments

Comments
 (0)