Skip to content

Instantly share code, notes, and snippets.

@adamw
Created April 3, 2018 15:08
Show Gist options
  • Save adamw/e384415c23c683facdc9b19368de0c33 to your computer and use it in GitHub Desktop.
Save adamw/e384415c23c683facdc9b19368de0c33 to your computer and use it in GitHub Desktop.
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