Skip to content

Instantly share code, notes, and snippets.

public interface IAccessProfileInfo {
Future<Iterable<ProfileId>> friendsOf(ProfileId profileId);
Future<Set<Topic>> favoriteTopicsOf(ProfileId profileId);
Future<Iterable<PostSummary>> lastPostsOf(ProfileId profileId);
}
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
try {
Future<Iterable<ProfileId>> friendIds = friendsOf(profileId);
Set<Topic> topics = favoriteTopicsOf(profileId).get();
Stream<PostSummary> interestingPosts = stream(friendIds.get())
.parallel() // Stream parallelization
.flatMap(friendId -> lastPostsOf(friendId))
.filter(post -> post.isAbout(topics));
return mostLiked(3, interestingPosts);
} catch (Exception e) { // InterruptedException and ExecutionException
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
Iterable<ProfileId> friendIds = friendsOf(profileId);
Set<Topic> topics = favoriteTopicsOf(profileId);
Stream<PostSummary> interestingPosts = stream(friendIds)
.flatMap(friendId -> lastPostsOf(friendId))
.filter(post -> post.isAbout(topics));
return mostLiked(3, interestingPosts);
}
class RestInfrastructure {
@Bean
@Autowired
public static IAccessProfileInfo getProfileInfoAccess(/* ... */) {
return new /*...*/;
}
}
class SuggestPopularFriendPost {
private final IAccessProfileInfo profileInfo; // Injected with constructor
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
// Same code as before, calling `friendsOf`, `favoriteTopicsOf` and `lastPostsOf`
}
private Iterable<ProfileId> friendsOf(ProfileId profileId) {
return profileInfo.friendsOf(profileId); // Rerouting to `IAccessProfileInfo`
}
public interface ISuggestPosts {
Iterable<PostSummary> suggestedPostsFor(ProfileId profileId);
void resetSuggestionOf(ProfileId profileId);
}
public interface ICacheSuggestions {
void saveSuggestions(ProfileId profileId, Iterable<PostSummary> suggestions);
Iterable<PostSummary> loadSuggestions(ProfileId profileId);
}
public static ISuggestPosts suggestPosts(IAccessProfileInfo profileInfo, ICacheSuggestions cache) {
return new PostSuggestions(cache, new SuggestPopularFriendPost(profileInfo));
}
public interface IAccessProfileInfo {
Iterable<ProfileId> friendsOf(ProfileId profileId);
Set<Topic> favoriteTopicsOf(ProfileId profileId);
Iterable<PostSummary> lastPostsOf(ProfileId profileId);
}
public interface ISuggestPosts {
Iterable<PostSummary> suggestedPostsFor(ProfileId profileId);
}