1 | package fr.sii.ogham.core.builder.registry; | |
2 | ||
3 | import java.util.ArrayDeque; | |
4 | import java.util.ArrayList; | |
5 | import java.util.Deque; | |
6 | import java.util.List; | |
7 | ||
8 | import fr.sii.ogham.core.clean.Cleanable; | |
9 | import fr.sii.ogham.core.exception.clean.CleanException; | |
10 | import fr.sii.ogham.core.exception.clean.CleanableException; | |
11 | import fr.sii.ogham.core.exception.clean.MultipleCleanException; | |
12 | ||
13 | /** | |
14 | * Registry that tracks instances that implements {@link Cleanable}. Other | |
15 | * instances are skipped. | |
16 | * | |
17 | * <p> | |
18 | * The registry is also a {@link Cleanable} to relay cleanup request to | |
19 | * registered instances when calling {@link #clean()} method. | |
20 | * | |
21 | * If an instance failed during its cleanup (exception is thrown), the failure | |
22 | * is registered. The next {@link Cleanable} instance is tried and so on until | |
23 | * all registered instances are cleaned. At the end a | |
24 | * {@link MultipleCleanException} is thrown with all registered failures. | |
25 | * | |
26 | * @author Aurélien Baudet | |
27 | * | |
28 | */ | |
29 | public class CleanableRegistry implements Registry<Object>, Cleanable { | |
30 | private final Deque<Cleanable> cleanables; | |
31 | ||
32 | /** | |
33 | * Initializes an empty registry | |
34 | */ | |
35 | public CleanableRegistry() { | |
36 | super(); | |
37 | this.cleanables = new ArrayDeque<>(); | |
38 | } | |
39 | ||
40 | @Override | |
41 | public void register(Object obj) { | |
42 |
8
1. register : negated conditional → NO_COVERAGE 2. register : negated conditional → KILLED 3. register : negated conditional → KILLED 4. register : negated conditional → KILLED 5. register : negated conditional → KILLED 6. register : negated conditional → KILLED 7. register : negated conditional → KILLED 8. register : negated conditional → KILLED |
if (obj instanceof Cleanable) { |
43 | cleanables.add((Cleanable) obj); | |
44 | } | |
45 | } | |
46 | ||
47 | @Override | |
48 | public void clean() throws CleanException { | |
49 | List<CleanException> failures = new ArrayList<>(); | |
50 |
3
1. clean : negated conditional → SURVIVED 2. clean : negated conditional → NO_COVERAGE 3. clean : negated conditional → KILLED |
while (!cleanables.isEmpty()) { |
51 |
3
1. clean : removed call to fr/sii/ogham/core/builder/registry/CleanableRegistry::clean → SURVIVED 2. clean : removed call to fr/sii/ogham/core/builder/registry/CleanableRegistry::clean → NO_COVERAGE 3. clean : removed call to fr/sii/ogham/core/builder/registry/CleanableRegistry::clean → KILLED |
clean(cleanables.pop(), failures); |
52 | } | |
53 |
3
1. clean : negated conditional → SURVIVED 2. clean : negated conditional → NO_COVERAGE 3. clean : negated conditional → KILLED |
if (!failures.isEmpty()) { |
54 | throw new MultipleCleanException("Failed to cleanup several resources", failures); | |
55 | } | |
56 | } | |
57 | ||
58 | private static void clean(Cleanable cleanable, List<CleanException> failures) { | |
59 | try { | |
60 |
3
1. clean : removed call to fr/sii/ogham/core/clean/Cleanable::clean → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/core/clean/Cleanable::clean → SURVIVED 3. clean : removed call to fr/sii/ogham/core/clean/Cleanable::clean → KILLED |
cleanable.clean(); |
61 | } catch (CleanException e) { | |
62 | failures.add(new CleanableException(e, cleanable)); | |
63 | } | |
64 | } | |
65 | } | |
Mutations | ||
42 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
50 |
1.1 2.2 3.3 |
|
51 |
1.1 2.2 3.3 |
|
53 |
1.1 2.2 3.3 |
|
60 |
1.1 2.2 3.3 |