Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various API consolodations/simplifications
- Loading branch information
1 parent
63acb91
commit af9a92b
Showing
15 changed files
with
436 additions
and
165 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package net.jodah.recurrent; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import net.jodah.recurrent.event.CompletionListener; | ||
|
||
/** | ||
* A callable that performs async callbacks. | ||
* | ||
* @author Jonathan Halterman | ||
* @param <T> result type | ||
*/ | ||
abstract class AsyncCallable<T> implements Callable<T> { | ||
protected Invocation invocation; | ||
protected CompletionListener<T> listener; | ||
|
||
static <T> AsyncCallable<T> of(final Callable<T> callable) { | ||
return new AsyncCallable<T>() { | ||
@Override | ||
public T call() throws Exception { | ||
try { | ||
T result = callable.call(); | ||
listener.onCompletion(result, null); | ||
return result; | ||
} catch (Exception e) { | ||
listener.onCompletion(null, e); | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
static <T> AsyncCallable<T> of(final RetryableCallable<T> callable) { | ||
return new AsyncCallable<T>() { | ||
@Override | ||
public T call() throws Exception { | ||
try { | ||
T result = callable.call(invocation); | ||
listener.onCompletion(result, null); | ||
return result; | ||
} catch (Exception e) { | ||
listener.onCompletion(null, e); | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
static AsyncCallable<?> of(final RetryableRunnable runnable) { | ||
return new AsyncCallable<Object>() { | ||
@Override | ||
public Void call() throws Exception { | ||
try { | ||
runnable.run(invocation); | ||
listener.onCompletion(null, null); | ||
} catch (Exception e) { | ||
listener.onCompletion(null, e); | ||
} | ||
|
||
return null; | ||
} | ||
}; | ||
} | ||
|
||
static AsyncCallable<?> of(final Runnable runnable) { | ||
return new AsyncCallable<Object>() { | ||
@Override | ||
public Void call() throws Exception { | ||
try { | ||
runnable.run(); | ||
listener.onCompletion(null, null); | ||
} catch (Exception e) { | ||
listener.onCompletion(null, e); | ||
} | ||
|
||
return null; | ||
} | ||
}; | ||
} | ||
|
||
void initialize(Invocation invocation, CompletionListener<T> listener) { | ||
this.invocation = invocation; | ||
this.listener = listener; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.