-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ListenableFutureTask up to parent.
- Loading branch information
Showing
9 changed files
with
97 additions
and
69 deletions.
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
libtextsecure/src/main/java/org/whispersystems/textsecure/util/FutureTaskListener.java
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
libtextsecure/src/main/java/org/whispersystems/textsecure/util/ListenableFutureTask.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/org/thoughtcrime/securesms/util/FutureTaskListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (C) 2014 Open Whisper Systems | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
public interface FutureTaskListener<V> { | ||
public void onSuccess(V result); | ||
public void onFailure(Throwable error); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/org/thoughtcrime/securesms/util/ListenableFutureTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (C) 2014 Open Whisper Systems | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.FutureTask; | ||
|
||
public class ListenableFutureTask<V> extends FutureTask<V> { | ||
|
||
private final List<FutureTaskListener<V>> listeners = new LinkedList<>(); | ||
|
||
public ListenableFutureTask(Callable<V> callable) { | ||
super(callable); | ||
} | ||
|
||
public synchronized void addListener(FutureTaskListener<V> listener) { | ||
if (this.isDone()) { | ||
callback(listener); | ||
} else { | ||
this.listeners.add(listener); | ||
} | ||
} | ||
|
||
public synchronized void removeListener(FutureTaskListener<V> listener) { | ||
this.listeners.remove(listener); | ||
} | ||
|
||
@Override | ||
protected synchronized void done() { | ||
callback(); | ||
} | ||
|
||
private void callback() { | ||
for (FutureTaskListener<V> listener : listeners) { | ||
callback(listener); | ||
} | ||
} | ||
|
||
private void callback(FutureTaskListener<V> listener) { | ||
if (listener != null) { | ||
try { | ||
listener.onSuccess(get()); | ||
} catch (InterruptedException e) { | ||
throw new AssertionError(e); | ||
} catch (ExecutionException e) { | ||
listener.onFailure(e); | ||
} | ||
} | ||
} | ||
} |