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