Skip to content

Commit d506bfe

Browse files
minbiKarthikeyan
authored andcommitted
[Core] Add callback and internal returning runnable (#701)
to support sync and async methods
1 parent 420069f commit d506bfe

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.amazonaws.async;
19+
20+
/**
21+
* Callback for async operations.
22+
* @param <R> the type of the result
23+
*/
24+
public interface Callback<R> {
25+
26+
/**
27+
* If the underlying operation succeeds this method will be called with the result.
28+
*
29+
* @param result the result
30+
*/
31+
void onResult(R result);
32+
33+
/**
34+
* If the underlying operation fails this method will be called with the exception.
35+
*
36+
* @param e the error
37+
*/
38+
void onError(Exception e);
39+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2019-2019 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.amazonaws.internal;
19+
20+
import com.amazonaws.async.Callback;
21+
22+
/**
23+
* Internal class to allow easy surfacing of synchronous and asynchronous operations.
24+
* This class may change without notice and may not be reflected in the SDK versioning scheme.
25+
*
26+
* @param <R> the type of the result/return
27+
*/
28+
public abstract class ReturningRunnable<R> {
29+
30+
private final String operationDescription;
31+
32+
/**
33+
* Default constructor. When an exception is thrown, it will not be wrapped with another
34+
* exception.
35+
*/
36+
public ReturningRunnable() {
37+
this.operationDescription = null;
38+
}
39+
40+
/**
41+
* When an exception is thrown, it will be wrapped with another exception with a message
42+
* specified by operationDescription.
43+
*
44+
* @param operationDescription The message of the exception that wraps the underlying exception.
45+
*/
46+
public ReturningRunnable(final String operationDescription) {
47+
this.operationDescription = operationDescription;
48+
}
49+
50+
/**
51+
* This method contains the underlying operation.
52+
*
53+
* @return Any result object of type R
54+
* @throws Exception Any error from performing the underlying operation
55+
*/
56+
public abstract R run() throws Exception;
57+
58+
/**
59+
* Runs the code inside {@link #run()} synchronously on the same thread this method is called.
60+
*
61+
* @return the result from the {@link #run()} method
62+
* @throws Exception error from the operation
63+
*/
64+
public R await() throws Exception {
65+
return this.run();
66+
}
67+
68+
/**
69+
* Runs the code inside {@link #run()} on a new Thread asynchronously and returns immediately.
70+
*
71+
* @param callback receives the result or error from the operation
72+
*/
73+
public void async(final Callback<R> callback) {
74+
new Thread(new Runnable() {
75+
@Override
76+
public void run() {
77+
try {
78+
callback.onResult(ReturningRunnable.this.run());
79+
} catch (final Exception e) {
80+
if (operationDescription == null) {
81+
callback.onError(e);
82+
} else {
83+
callback.onError(new Exception(operationDescription, e));
84+
}
85+
}
86+
}
87+
}).start();
88+
}
89+
}

0 commit comments

Comments
 (0)