1 | package fr.sii.ogham.core.condition; | |
2 | ||
3 | import java.util.List; | |
4 | ||
5 | import fr.sii.ogham.core.util.StringUtils; | |
6 | ||
7 | /** | |
8 | * Condition that provides a logical AND operation on manipulated conditions. | |
9 | * | |
10 | * @author Aurélien Baudet | |
11 | * | |
12 | * @param <T> | |
13 | * the type of the object to test | |
14 | */ | |
15 | public class AndCondition<T> extends CompositeCondition<T> { | |
16 | ||
17 | /** | |
18 | * Initializes the {@code and} operator with none, one or several | |
19 | * sub-conditions. | |
20 | * | |
21 | * <pre> | |
22 | * Condition<String> condition1 = ... | |
23 | * Condition<String> condition2 = ... | |
24 | * AndCondition<String> myCondition = new AndCondition<>(condition1, condition2); | |
25 | * </pre> | |
26 | * | |
27 | * Has the same effect as: | |
28 | * | |
29 | * <pre> | |
30 | * Condition<String> condition1 = ... | |
31 | * Condition<String> condition2 = ... | |
32 | * AndCondition<String> myCondition = new AndCondition<>(); | |
33 | * myCondition.and(condition1); | |
34 | * myCondition.and(condition2); | |
35 | * </pre> | |
36 | * | |
37 | * @param conditions | |
38 | * the conditions to register (order is important) | |
39 | */ | |
40 | @SafeVarargs | |
41 | public AndCondition(Condition<T>... conditions) { | |
42 | super(conditions); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Initializes the {@code and} operator with none, one or several | |
47 | * sub-conditions. The list must not be null. | |
48 | * | |
49 | * @param conditions | |
50 | * the conditions to register (order is important) | |
51 | */ | |
52 | public AndCondition(List<Condition<T>> conditions) { | |
53 | super(conditions); | |
54 | } | |
55 | ||
56 | @Override | |
57 | public boolean accept(T obj) { | |
58 | for (Condition<T> condition : getConditions()) { | |
59 | // if the condition rejects the object => stop now | |
60 |
7
1. accept : negated conditional → NO_COVERAGE 2. accept : negated conditional → KILLED 3. accept : negated conditional → KILLED 4. accept : negated conditional → KILLED 5. accept : negated conditional → KILLED 6. accept : negated conditional → KILLED 7. accept : negated conditional → KILLED |
if (!condition.accept(obj)) { |
61 |
4
1. accept : replaced boolean return with true for fr/sii/ogham/core/condition/AndCondition::accept → NO_COVERAGE 2. accept : replaced boolean return with true for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 3. accept : replaced boolean return with true for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 4. accept : replaced boolean return with true for fr/sii/ogham/core/condition/AndCondition::accept → KILLED |
return false; |
62 | } | |
63 | } | |
64 | // none condition has rejected the object => it is accepted | |
65 |
7
1. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → NO_COVERAGE 2. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 3. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 4. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 5. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 6. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED 7. accept : replaced boolean return with false for fr/sii/ogham/core/condition/AndCondition::accept → KILLED |
return true; |
66 | } | |
67 | ||
68 | /** | |
69 | * Adds a condition to the current condition. For example: | |
70 | * | |
71 | * <pre> | |
72 | * AndCondition<String> myCondition = new AndCondition<>(); | |
73 | * myCondition.and(new FixedCondition<>(true)); | |
74 | * myCondition.apply("foo"); // will always return true | |
75 | * | |
76 | * myCondition.and(new FixedCondition<>(false)); | |
77 | * myCondition.apply("foo"); // will always return false | |
78 | * </pre> | |
79 | * | |
80 | * The returned instance is the same as {@code myCondition} so you can also | |
81 | * write this: | |
82 | * | |
83 | * <pre> | |
84 | * AndCondition<String> myCondition = new AndCondition<>(); | |
85 | * myCondition = myCondition.and(new FixedCondition<>(true)); | |
86 | * myCondition.apply("foo"); // will always return true | |
87 | * | |
88 | * myCondition = myCondition.and(new FixedCondition<>(false)); | |
89 | * myCondition.apply("foo"); // will always return false | |
90 | * </pre> | |
91 | * | |
92 | * | |
93 | * @param condition | |
94 | * the condition to add | |
95 | * @return this instance for fluent chaining | |
96 | */ | |
97 | public AndCondition<T> and(Condition<T> condition) { | |
98 | addCondition(condition); | |
99 |
3
1. and : replaced return value with null for fr/sii/ogham/core/condition/AndCondition::and → SURVIVED 2. and : replaced return value with null for fr/sii/ogham/core/condition/AndCondition::and → NO_COVERAGE 3. and : replaced return value with null for fr/sii/ogham/core/condition/AndCondition::and → KILLED |
return this; |
100 | } | |
101 | ||
102 | /** | |
103 | * Adds several conditions at once to the current condition. This can be | |
104 | * useful when you register each sub-condition into a list: | |
105 | * | |
106 | * <pre> | |
107 | * List<Condition<String>> conditions = new ArrayList<>(); | |
108 | * conditions.add(...); | |
109 | * conditions.add(...); | |
110 | * AndCondition<String> myCondition = new AndCondition<>(); | |
111 | * myCondition.and(conditions); | |
112 | * </pre> | |
113 | * | |
114 | * @param conditions | |
115 | * the list of conditions to register | |
116 | * @return this instance for fluent chaining | |
117 | */ | |
118 | public AndCondition<T> and(List<Condition<T>> conditions) { | |
119 | addConditions(conditions); | |
120 |
1
1. and : replaced return value with null for fr/sii/ogham/core/condition/AndCondition::and → NO_COVERAGE |
return this; |
121 | } | |
122 | ||
123 | @Override | |
124 | public String toString() { | |
125 |
1
1. toString : replaced return value with "" for fr/sii/ogham/core/condition/AndCondition::toString → NO_COVERAGE |
return "{" + StringUtils.join(conditions, "} and {") + "}"; |
126 | } | |
127 | } | |
Mutations | ||
60 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
61 |
1.1 2.2 3.3 4.4 |
|
65 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
99 |
1.1 2.2 3.3 |
|
120 |
1.1 |
|
125 |
1.1 |