blob: efa9ac212e30cc124485e10426c541c4904dfb5f [file] [log] [blame]
Matthias Andreas Benkard36b0f042021-02-27 10:46:04 +01001package eu.mulk.demos.blog.comments;
2
3import static java.util.stream.Collectors.toMap;
4
5import java.util.Collection;
6import java.util.Map;
7import 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
14public 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}