Matthias Andreas Benkard | 36b0f04 | 2021-02-27 10:46:04 +0100 | [diff] [blame^] | 1 | package eu.mulk.demos.blog.comments; |
| 2 | |
| 3 | import static java.util.stream.Collectors.toMap; |
| 4 | |
| 5 | import java.util.Collection; |
| 6 | import java.util.Map; |
| 7 | import javax.enterprise.context.ApplicationScoped; |
| 8 | |
| 9 | /** |
| 10 | * Simulates a remote service that classifies {@link Comment}s as either {@link SpamStatus#SPAM} or |
| 11 | * {@link SpamStatus#HAM}. |
| 12 | */ |
| 13 | @ApplicationScoped |
| 14 | public class SpamAssessmentService { |
| 15 | |
| 16 | /** |
| 17 | * Classifies a list of {@link Comment}s as either {@link SpamStatus#SPAM} or * {@link |
| 18 | * SpamStatus#HAM}. |
| 19 | * |
| 20 | * @return a map mapping {@link Comment#id}s to {@link SpamStatus}es. |
| 21 | */ |
| 22 | public Map<Long, SpamStatus> assess(Collection<Comment> comments) { |
| 23 | return comments.stream().collect(toMap(x -> x.id, this::assessOne)); |
| 24 | } |
| 25 | |
| 26 | private SpamStatus assessOne(Comment c) { |
| 27 | if (c.authorName.startsWith("Anonymous")) { |
| 28 | return SpamStatus.SPAM; |
| 29 | } |
| 30 | |
| 31 | return SpamStatus.HAM; |
| 32 | } |
| 33 | } |