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 OR 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 OrCondition<T> extends CompositeCondition<T> { | |
16 | ||
17 | /** | |
18 | * Initializes the {@code or} operator with none, one or several | |
19 | * sub-conditions. | |
20 | * | |
21 | * <pre> | |
22 | * Condition<String> condition1 = ... | |
23 | * Condition<String> condition2 = ... | |
24 | * OrCondition<String> myCondition = new OrCondition<>(condition1, condition2); | |
25 | * </pre> | |
26 | * | |
27 | * Has the same effect as: | |
28 | * | |
29 | * <pre> | |
30 | * Condition<String> condition1 = ... | |
31 | * Condition<String> condition2 = ... | |
32 | * OrCondition<String> myCondition = new OrCondition<>(); | |
33 | * myCondition.or(condition1); | |
34 | * myCondition.or(condition2); | |
35 | * </pre> | |
36 | * | |
37 | * @param conditions | |
38 | * the conditions to register (order is important) | |
39 | */ | |
40 | @SafeVarargs | |
41 | public OrCondition(Condition<T>... conditions) { | |
42 | super(conditions); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Initializes the {@code or} 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 OrCondition(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 accepts the object => stop now | |
60 |
5
1. accept : negated conditional → NO_COVERAGE 2. accept : negated conditional → TIMED_OUT 3. accept : negated conditional → KILLED 4. accept : negated conditional → KILLED 5. accept : negated conditional → KILLED |
if (condition.accept(obj)) { |
61 |
5
1. accept : replaced boolean return with false for fr/sii/ogham/core/condition/OrCondition::accept → NO_COVERAGE 2. accept : replaced boolean return with false for fr/sii/ogham/core/condition/OrCondition::accept → TIMED_OUT 3. accept : replaced boolean return with false for fr/sii/ogham/core/condition/OrCondition::accept → KILLED 4. accept : replaced boolean return with false for fr/sii/ogham/core/condition/OrCondition::accept → KILLED 5. accept : replaced boolean return with false for fr/sii/ogham/core/condition/OrCondition::accept → KILLED |
return true; |
62 | } | |
63 | } | |
64 | // none condition has accepted the object => it is rejected | |
65 |
4
1. accept : replaced boolean return with true for fr/sii/ogham/core/condition/OrCondition::accept → NO_COVERAGE 2. accept : replaced boolean return with true for fr/sii/ogham/core/condition/OrCondition::accept → KILLED 3. accept : replaced boolean return with true for fr/sii/ogham/core/condition/OrCondition::accept → KILLED 4. accept : replaced boolean return with true for fr/sii/ogham/core/condition/OrCondition::accept → KILLED |
return false; |
66 | } | |
67 | ||
68 | /** | |
69 | * Adds a condition to the current condition. For example: | |
70 | * | |
71 | * <pre> | |
72 | * OrCondition<String> myCondition = new OrCondition<>(); | |
73 | * myCondition.or(new FixedCondition<>(true)); | |
74 | * myCondition.apply("foo"); // will always return true | |
75 | * | |
76 | * myCondition.or(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 | * OrCondition<String> myCondition = new OrCondition<>(); | |
85 | * myCondition = myCondition.or(new FixedCondition<>(true)); | |
86 | * myCondition.apply("foo"); // will always return true | |
87 | * | |
88 | * myCondition = myCondition.or(new FixedCondition<>(false)); | |
89 | * myCondition.apply("foo"); // will always return true because first | |
90 | * // condition always returns true | |
91 | * </pre> | |
92 | * | |
93 | * | |
94 | * @param condition | |
95 | * the condition to add | |
96 | * @return this instance for fluent chaining | |
97 | */ | |
98 | public OrCondition<T> or(Condition<T> condition) { | |
99 | addCondition(condition); | |
100 |
1
1. or : replaced return value with null for fr/sii/ogham/core/condition/OrCondition::or → NO_COVERAGE |
return this; |
101 | } | |
102 | ||
103 | /** | |
104 | * Adds several conditions at once to the current condition. This can be | |
105 | * useful when you register each sub-condition into a list: | |
106 | * | |
107 | * <pre> | |
108 | * List<Condition<String>> conditions = new ArrayList<>(); | |
109 | * conditions.add(...); | |
110 | * conditions.add(...); | |
111 | * OrCondition<String> myCondition = new OrCondition<>(); | |
112 | * myCondition.or(conditions); | |
113 | * </pre> | |
114 | * | |
115 | * @param conditions | |
116 | * the list of conditions to register | |
117 | * @return this instance for fluent chaining | |
118 | */ | |
119 | public OrCondition<T> or(List<Condition<T>> conditions) { | |
120 | addConditions(conditions); | |
121 |
1
1. or : replaced return value with null for fr/sii/ogham/core/condition/OrCondition::or → NO_COVERAGE |
return this; |
122 | } | |
123 | ||
124 | @Override | |
125 | public String toString() { | |
126 |
1
1. toString : replaced return value with "" for fr/sii/ogham/core/condition/OrCondition::toString → NO_COVERAGE |
return "{" + StringUtils.join(conditions, " or ") + "}"; |
127 | } | |
128 | } | |
Mutations | ||
60 |
1.1 2.2 3.3 4.4 5.5 |
|
61 |
1.1 2.2 3.3 4.4 5.5 |
|
65 |
1.1 2.2 3.3 4.4 |
|
100 |
1.1 |
|
121 |
1.1 |
|
126 |
1.1 |