CleanableRegistry.java

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 1 1. register : negated conditional → RUN_ERROR
		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 1 1. clean : negated conditional → RUN_ERROR
		while (!cleanables.isEmpty()) {
51 1 1. clean : removed call to fr/sii/ogham/core/builder/registry/CleanableRegistry::clean → RUN_ERROR
			clean(cleanables.pop(), failures);
52
		}
53 1 1. clean : negated conditional → RUN_ERROR
		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 1 1. clean : removed call to fr/sii/ogham/core/clean/Cleanable::clean → RUN_ERROR
			cleanable.clean();
61
		} catch (CleanException e) {
62
			failures.add(new CleanableException(e, cleanable));
63
		}
64
	}
65
}

Mutations

42

1.1
Location : register
Killed by :
negated conditional → RUN_ERROR

50

1.1
Location : clean
Killed by :
negated conditional → RUN_ERROR

51

1.1
Location : clean
Killed by :
removed call to fr/sii/ogham/core/builder/registry/CleanableRegistry::clean → RUN_ERROR

53

1.1
Location : clean
Killed by :
negated conditional → RUN_ERROR

60

1.1
Location : clean
Killed by :
removed call to fr/sii/ogham/core/clean/Cleanable::clean → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1