1 | package fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.handler; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | import java.util.function.Predicate; | |
6 | ||
7 | import org.slf4j.Logger; | |
8 | import org.slf4j.LoggerFactory; | |
9 | ||
10 | import fr.sii.ogham.core.message.content.Content; | |
11 | import fr.sii.ogham.core.util.PriorityComparator; | |
12 | import fr.sii.ogham.core.util.PriorizedMatchingHandler; | |
13 | import fr.sii.ogham.email.exception.handler.ContentHandlerException; | |
14 | import fr.sii.ogham.email.message.Email; | |
15 | import fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.compat.MailCompat; | |
16 | ||
17 | /** | |
18 | * Implementation of {@link SendGridContentHandler} that delegates content | |
19 | * handling to specialized instances, if one matching the actual content type | |
20 | * has been declared. | |
21 | */ | |
22 | public final class PriorizedContentHandler implements SendGridContentHandler { | |
23 | ||
24 | private static final Logger LOG = LoggerFactory.getLogger(PriorizedContentHandler.class); | |
25 | ||
26 | /** | |
27 | * The list of content handlers with corresponding matcher and priority | |
28 | */ | |
29 | private List<PriorizedMatchingHandler<SendGridContentHandler>> matchingHandlers; | |
30 | ||
31 | /** | |
32 | * Initialize with the list of content handlers with matching predicate and | |
33 | * priority. | |
34 | * | |
35 | * Higher priority means that handler will be used first. | |
36 | * | |
37 | * @param matchingHandlers | |
38 | * the list of content handlers with the associated content | |
39 | * matcher and priority | |
40 | */ | |
41 | public PriorizedContentHandler(List<PriorizedMatchingHandler<SendGridContentHandler>> matchingHandlers) { | |
42 | super(); | |
43 | this.matchingHandlers = matchingHandlers; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Initialize with an empty list | |
48 | */ | |
49 | public PriorizedContentHandler() { | |
50 | this(new ArrayList<>()); | |
51 | } | |
52 | ||
53 | ||
54 | /** | |
55 | * Register a new content handler using the provided content matcher. | |
56 | * | |
57 | * @param contentMatcher | |
58 | * the class of the content to match | |
59 | * @param handler | |
60 | * the content handler to use if the class matches | |
61 | * @param priority | |
62 | * the priority order (means that matching handler with higher | |
63 | * priority is used) | |
64 | */ | |
65 | public void register(Predicate<Content> contentMatcher, SendGridContentHandler handler, int priority) { | |
66 | matchingHandlers.add(new PriorizedMatchingHandler<>(contentMatcher, handler, priority)); | |
67 |
2
1. register : removed call to java/util/List::sort → NO_COVERAGE 2. register : removed call to java/util/List::sort → SURVIVED |
matchingHandlers.sort(new PriorityComparator<>(PriorizedMatchingHandler::getPriority)); |
68 | } | |
69 | ||
70 | /** | |
71 | * Register a new content handler using the provided content matcher. | |
72 | * | |
73 | * The priority is the registration order (first registered has higher | |
74 | * priority). | |
75 | * | |
76 | * @param contentMatcher | |
77 | * the class of the content | |
78 | * @param handler | |
79 | * the content handler | |
80 | */ | |
81 | public void register(Predicate<Content> contentMatcher, SendGridContentHandler handler) { | |
82 |
2
1. register : removed negation → NO_COVERAGE 2. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE |
register(contentMatcher, handler, -matchingHandlers.size()); |
83 | } | |
84 | ||
85 | /** | |
86 | * Register a new content handler. The matching predicate is only based on | |
87 | * the class. It means that if the class of the content is a sub-class of | |
88 | * clazz parameter, then the associated handler is used. | |
89 | * | |
90 | * @param clazz | |
91 | * the class of the content to match | |
92 | * @param handler | |
93 | * the content handler to use if the class matches | |
94 | * @param priority | |
95 | * the priority order (means that matching handler with higher | |
96 | * priority is used) | |
97 | */ | |
98 | public void register(Class<? extends Content> clazz, SendGridContentHandler handler, int priority) { | |
99 |
4
1. register : negated conditional → NO_COVERAGE 2. register : negated conditional → KILLED 3. register : negated conditional → KILLED 4. register : negated conditional → KILLED |
if (handler == null) { |
100 | throw new IllegalArgumentException("[handler] cannot be null"); | |
101 | } | |
102 |
4
1. register : negated conditional → NO_COVERAGE 2. register : negated conditional → KILLED 3. register : negated conditional → KILLED 4. register : negated conditional → KILLED |
if (clazz == null) { |
103 | throw new IllegalArgumentException("[clazz] cannot be null"); | |
104 | } | |
105 | | |
106 |
7
1. lambda$register$0 : replaced boolean return with false for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::lambda$register$0 → NO_COVERAGE 2. lambda$register$0 : replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::lambda$register$0 → NO_COVERAGE 3. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE 4. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → SURVIVED 5. lambda$register$0 : replaced boolean return with false for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::lambda$register$0 → KILLED 6. lambda$register$0 : replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::lambda$register$0 → KILLED 7. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → KILLED |
register(c -> clazz.isAssignableFrom(c.getClass()), handler, priority); |
107 | } | |
108 | ||
109 | /** | |
110 | * Register a new content handler. The matching predicate is only based on | |
111 | * the class. It means that if the class of the content is a sub-class of | |
112 | * clazz parameter, then the associated handler is used. | |
113 | * | |
114 | * The priority is the registration order (first registered has higher | |
115 | * priority). | |
116 | * | |
117 | * @param clazz | |
118 | * the class of the content | |
119 | * @param handler | |
120 | * the content handler | |
121 | */ | |
122 | public void register(Class<? extends Content> clazz, SendGridContentHandler handler) { | |
123 |
5
1. register : removed negation → SURVIVED 2. register : removed negation → NO_COVERAGE 3. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE 4. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → SURVIVED 5. register : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::register → KILLED |
register(clazz, handler, -matchingHandlers.size()); |
124 | } | |
125 | ||
126 | @Override | |
127 | public void setContent(final Email original, final MailCompat email, final Content content) throws ContentHandlerException { | |
128 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (email == null) { |
129 | throw new IllegalArgumentException("[email] cannot be null"); | |
130 | } | |
131 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (content == null) { |
132 | throw new IllegalArgumentException("[content] cannot be null"); | |
133 | } | |
134 | ||
135 | final Class<?> clazz = content.getClass(); | |
136 | LOG.debug("Getting content handler for type {}", clazz); | |
137 | final SendGridContentHandler handler = findHandler(content); | |
138 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (handler == null) { |
139 | LOG.warn("No content handler found for requested type {}", clazz); | |
140 | throw new ContentHandlerException("No content handler found for content type " + clazz.getSimpleName(), content); | |
141 | } else { | |
142 |
2
1. setContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/SendGridContentHandler::setContent → NO_COVERAGE 2. setContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/SendGridContentHandler::setContent → KILLED |
handler.setContent(original, email, content); |
143 | } | |
144 | } | |
145 | ||
146 | private SendGridContentHandler findHandler(final Content content) { | |
147 | for (PriorizedMatchingHandler<SendGridContentHandler> entry : matchingHandlers) { | |
148 |
2
1. findHandler : negated conditional → NO_COVERAGE 2. findHandler : negated conditional → KILLED |
if (entry.matches(content)) { |
149 |
2
1. findHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::findHandler → NO_COVERAGE 2. findHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/PriorizedContentHandler::findHandler → KILLED |
return entry.getHandler(); |
150 | } | |
151 | } | |
152 | return null; | |
153 | } | |
154 | ||
155 | } | |
Mutations | ||
67 |
1.1 2.2 |
|
82 |
1.1 2.2 |
|
99 |
1.1 2.2 3.3 4.4 |
|
102 |
1.1 2.2 3.3 4.4 |
|
106 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
123 |
1.1 2.2 3.3 4.4 5.5 |
|
128 |
1.1 2.2 |
|
131 |
1.1 2.2 |
|
138 |
1.1 2.2 |
|
142 |
1.1 2.2 |
|
148 |
1.1 2.2 |
|
149 |
1.1 2.2 |