1 | package fr.sii.ogham.sms.sender.impl.cloudhopper.encoder; | |
2 | ||
3 | import java.util.List; | |
4 | ||
5 | import org.slf4j.Logger; | |
6 | import org.slf4j.LoggerFactory; | |
7 | ||
8 | import fr.sii.ogham.sms.encoder.Encoded; | |
9 | import fr.sii.ogham.sms.encoder.Encoder; | |
10 | import fr.sii.ogham.sms.encoder.SupportingEncoder; | |
11 | import fr.sii.ogham.sms.exception.message.EncodingException; | |
12 | import fr.sii.ogham.sms.sender.impl.cloudhopper.exception.GuessEncodingException; | |
13 | ||
14 | /** | |
15 | * Automatically determine best {@link Encoder} to use for encoding the message. | |
16 | * | |
17 | * <p> | |
18 | * The guessing is really simple: | |
19 | * <ol> | |
20 | * <li>The first {@link Encoder} is tested to check if message can be encoded | |
21 | * with that encoder: | |
22 | * <ul> | |
23 | * <li>If the {@link Encoder} implements {@link SupportingEncoder}, the | |
24 | * {@link SupportingEncoder#canEncode(String)} is called to check if the | |
25 | * {@link Encoder} is able to encode the message. If false is returned, the | |
26 | * {@link Encoder} is skipped (not executed).</li> | |
27 | * <li>If the {@link Encoder} doesn't implement {@link SupportingEncoder}, the | |
28 | * {@link Encoder} is executed. The {@link Encoder} may throw an | |
29 | * {@link EncodingException} if it can't encode correctly the message. If that | |
30 | * exception is raised, it is caught and the {@link Encoder} is skipped.</li> | |
31 | * <li>If the {@link Encoder} is not skipped (it could encode the message | |
32 | * correctly), its result is directly returned.</li> | |
33 | * </ul> | |
34 | * <li>The next {@link Encoder} is tested and so on until one {@link Encoder} | |
35 | * returns the encoded message</li> | |
36 | * </ol> | |
37 | * | |
38 | * <p> | |
39 | * If none of the possible encoders could encode the message, the | |
40 | * {@link GuessEncodingException} is raised. | |
41 | * | |
42 | * | |
43 | * @author Aurélien Baudet | |
44 | * | |
45 | */ | |
46 | public class GuessEncodingEncoder implements SupportingEncoder { | |
47 | private static final Logger LOG = LoggerFactory.getLogger(GuessEncodingEncoder.class); | |
48 | ||
49 | private final List<Encoder> possibleEncoders; | |
50 | ||
51 | /** | |
52 | * Initializes with the list of possible encoders to test. If an encoder | |
53 | * implements {@link SupportingEncoder}, the | |
54 | * {@link SupportingEncoder#canEncode(String)} is used to check if the text | |
55 | * message can be encoded using that {@link Encoder}. Otherwise, the | |
56 | * {@link Encoder} is executed to try encoding the message. If the message | |
57 | * can't be encoded, an {@link EncodingException} is raised. The exception | |
58 | * is caught and the next {@link Encoder} is executed. | |
59 | * | |
60 | * @param possibleEncoders | |
61 | * the possible encoders used to encode the message | |
62 | */ | |
63 | public GuessEncodingEncoder(List<Encoder> possibleEncoders) { | |
64 | super(); | |
65 | this.possibleEncoders = possibleEncoders; | |
66 | } | |
67 | ||
68 | @Override | |
69 | public boolean canEncode(String message) { | |
70 | for (Encoder encoder : possibleEncoders) { | |
71 |
2
1. canEncode : negated conditional → NO_COVERAGE 2. canEncode : negated conditional → KILLED |
if (canEncode(encoder, message)) { |
72 |
2
1. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → NO_COVERAGE 2. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → KILLED |
return true; |
73 | } | |
74 | } | |
75 |
2
1. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → NO_COVERAGE 2. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → KILLED |
return false; |
76 | } | |
77 | ||
78 | @Override | |
79 | public Encoded encode(String message) throws EncodingException { | |
80 | for (Encoder encoder : possibleEncoders) { | |
81 |
2
1. encode : negated conditional → NO_COVERAGE 2. encode : negated conditional → KILLED |
if (!canEncode(encoder, message)) { |
82 | continue; | |
83 | } | |
84 | try { | |
85 |
2
1. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::encode → NO_COVERAGE 2. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::encode → KILLED |
return encoder.encode(message); |
86 | } catch (EncodingException e) { | |
87 | LOG.debug("Encoder failed to encode => try next one", e); | |
88 | } | |
89 | } | |
90 | throw new GuessEncodingException("Failed to guess encoding for message", message); | |
91 | } | |
92 | ||
93 | private static boolean canEncode(Encoder encoder, String message) { | |
94 |
2
1. canEncode : negated conditional → NO_COVERAGE 2. canEncode : negated conditional → KILLED |
if (encoder instanceof SupportingEncoder) { |
95 |
4
1. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → NO_COVERAGE 2. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → NO_COVERAGE 3. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → KILLED 4. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → KILLED |
return ((SupportingEncoder) encoder).canEncode(message); |
96 | } | |
97 |
2
1. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → NO_COVERAGE 2. canEncode : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/GuessEncodingEncoder::canEncode → KILLED |
return true; |
98 | } | |
99 | } | |
Mutations | ||
71 |
1.1 2.2 |
|
72 |
1.1 2.2 |
|
75 |
1.1 2.2 |
|
81 |
1.1 2.2 |
|
85 |
1.1 2.2 |
|
94 |
1.1 2.2 |
|
95 |
1.1 2.2 3.3 4.4 |
|
97 |
1.1 2.2 |