forked from aws-amplify/aws-sdk-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Add callback and internal returning runnable (aws-amplify#701)
to support sync and async methods
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
aws-android-sdk-core/src/main/java/com/amazonaws/async/Callback.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,39 @@ | ||
/* | ||
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates. | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.async; | ||
|
||
/** | ||
* Callback for async operations. | ||
* @param <R> the type of the result | ||
*/ | ||
public interface Callback<R> { | ||
|
||
/** | ||
* If the underlying operation succeeds this method will be called with the result. | ||
* | ||
* @param result the result | ||
*/ | ||
void onResult(R result); | ||
|
||
/** | ||
* If the underlying operation fails this method will be called with the exception. | ||
* | ||
* @param e the error | ||
*/ | ||
void onError(Exception e); | ||
} |
89 changes: 89 additions & 0 deletions
89
aws-android-sdk-core/src/main/java/com/amazonaws/internal/ReturningRunnable.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,89 @@ | ||
/* | ||
* Copyright 2019-2019 Amazon.com, Inc. or its affiliates. | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.internal; | ||
|
||
import com.amazonaws.async.Callback; | ||
|
||
/** | ||
* Internal class to allow easy surfacing of synchronous and asynchronous operations. | ||
* This class may change without notice and may not be reflected in the SDK versioning scheme. | ||
* | ||
* @param <R> the type of the result/return | ||
*/ | ||
public abstract class ReturningRunnable<R> { | ||
|
||
private final String operationDescription; | ||
|
||
/** | ||
* Default constructor. When an exception is thrown, it will not be wrapped with another | ||
* exception. | ||
*/ | ||
public ReturningRunnable() { | ||
this.operationDescription = null; | ||
} | ||
|
||
/** | ||
* When an exception is thrown, it will be wrapped with another exception with a message | ||
* specified by operationDescription. | ||
* | ||
* @param operationDescription The message of the exception that wraps the underlying exception. | ||
*/ | ||
public ReturningRunnable(final String operationDescription) { | ||
this.operationDescription = operationDescription; | ||
} | ||
|
||
/** | ||
* This method contains the underlying operation. | ||
* | ||
* @return Any result object of type R | ||
* @throws Exception Any error from performing the underlying operation | ||
*/ | ||
public abstract R run() throws Exception; | ||
|
||
/** | ||
* Runs the code inside {@link #run()} synchronously on the same thread this method is called. | ||
* | ||
* @return the result from the {@link #run()} method | ||
* @throws Exception error from the operation | ||
*/ | ||
public R await() throws Exception { | ||
return this.run(); | ||
} | ||
|
||
/** | ||
* Runs the code inside {@link #run()} on a new Thread asynchronously and returns immediately. | ||
* | ||
* @param callback receives the result or error from the operation | ||
*/ | ||
public void async(final Callback<R> callback) { | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
callback.onResult(ReturningRunnable.this.run()); | ||
} catch (final Exception e) { | ||
if (operationDescription == null) { | ||
callback.onError(e); | ||
} else { | ||
callback.onError(new Exception(operationDescription, e)); | ||
} | ||
} | ||
} | ||
}).start(); | ||
} | ||
} |