Created
April 3, 2018 15:08
-
-
Save adamw/e384415c23c683facdc9b19368de0c33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package test; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.function.Consumer; | |
public class Compare1 { | |
// implementations are omitted | |
class User { | |
String getProfileId() { return null; } | |
String getEmail() { return null; } | |
} | |
class Synchronous { | |
<T> T fetchFromDb(Class<T> entityClass, long id) { return null; } | |
String sendHttpGet(String url) { return null; } | |
void sendEmail(String to, String content) { } | |
void runBusinessProcess() { | |
User user = fetchFromDb(User.class, 42); | |
String profile = sendHttpGet("http://profile_service/get/" + user.getProfileId()); | |
sendEmail(user.getEmail(), "Your profile is: " + profile); | |
} | |
} | |
class Callbacks { | |
<T> void fetchFromDb(Class<T> entityClass, long id, Consumer<T> callback) { } | |
void sendHttpGet(String url, Consumer<String> callback) { } | |
void sendEmail(String to, String content, Runnable callback) { } | |
void runBusinessProcess(Runnable callback) { | |
fetchFromDb(User.class, 42, | |
user -> sendHttpGet("http://profile_service/get/" + user.getProfileId(), | |
profile -> sendEmail(user.getEmail(), "Your profile is: " + profile, | |
callback))); | |
} | |
} | |
class Wrappers { | |
<T> CompletableFuture<T> fetchFromDb(Class<T> entityClass, long id) { return null; } | |
CompletableFuture<String> sendHttpGet(String url) { return null; } | |
CompletableFuture<Void> sendEmail(String to, String content) { return null; } | |
CompletableFuture<Void> runBusinessProcess() { | |
return fetchFromDb(User.class, 42).thenCompose(user -> { | |
CompletableFuture<String> httpResult = | |
sendHttpGet("http://profile_service/get/" + user.getProfileId()); | |
return httpResult.thenCompose(profile -> | |
sendEmail(user.getEmail(), "Your profile is: " + profile)); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment