1 | package com.github.dakusui.pcond.core.fluent; | |
2 | ||
3 | import com.github.dakusui.pcond.forms.Functions; | |
4 | import com.github.dakusui.pcond.forms.Predicates; | |
5 | ||
6 | import java.util.ArrayList; | |
7 | import java.util.LinkedList; | |
8 | import java.util.List; | |
9 | import java.util.Objects; | |
10 | import java.util.function.Function; | |
11 | import java.util.function.Predicate; | |
12 | import java.util.function.Supplier; | |
13 | ||
14 | import static com.github.dakusui.pcond.internals.InternalChecks.requireState; | |
15 | import static java.util.Collections.unmodifiableList; | |
16 | import static java.util.Objects.requireNonNull; | |
17 | import static java.util.stream.Collectors.toList; | |
18 | ||
19 | public interface Matcher< | |
20 | M extends Matcher<M, T, R>, | |
21 | T, | |
22 | R> { | |
23 | M allOf(); | |
24 | ||
25 | M anyOf(); | |
26 | ||
27 | Predicate<T> toPredicate(); | |
28 | ||
29 | Function<T, R> transformFunction(); | |
30 | ||
31 | abstract class Base< | |
32 | M extends Matcher<M, T, R>, | |
33 | T, | |
34 | R> implements Matcher<M, T, R> { | |
35 | private final Function<T, R> transformFunction; | |
36 | private final Supplier<T> baseValue; | |
37 | ||
38 | private final List<Function<Matcher<?, R, R>, Predicate<R>>> childPredicates = new LinkedList<>(); | |
39 | private Matcher.JunctionType junctionType; | |
40 | ||
41 | private Predicate<T> builtPredicate; | |
42 | ||
43 | protected Base(Supplier<T> baseValue, Function<T, R> transformFunction) { | |
44 | this.transformFunction = requireNonNull(transformFunction); | |
45 | this.baseValue = requireNonNull(baseValue); | |
46 | this.allOf(); | |
47 | } | |
48 | ||
49 | @Override | |
50 | public M allOf() { | |
51 |
1
1. allOf : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::allOf → SURVIVED |
return junctionType(Matcher.JunctionType.CONJUNCTION); |
52 | } | |
53 | ||
54 | @Override | |
55 | public M anyOf() { | |
56 |
1
1. anyOf : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::anyOf → NO_COVERAGE |
return junctionType(Matcher.JunctionType.DISJUNCTION); |
57 | } | |
58 | ||
59 | @Override | |
60 | public Predicate<T> toPredicate() { | |
61 |
1
1. toPredicate : negated conditional → KILLED |
if (this.builtPredicate == null) |
62 | this.builtPredicate = buildPredicate(); | |
63 |
1
1. toPredicate : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::toPredicate → KILLED |
return this.builtPredicate; |
64 | } | |
65 | ||
66 | protected M addPredicate(Function<Matcher<?, R, R>, Predicate<R>> clause) { | |
67 | this.childPredicates.add(requireNonNull(clause)); | |
68 |
1
1. addPredicate : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::addPredicate → KILLED |
return me(); |
69 | } | |
70 | ||
71 | public R value() { | |
72 |
1
1. value : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::value → KILLED |
return this.transformFunction.apply(this.baseValue()); |
73 | } | |
74 | ||
75 | /* protected */ | |
76 | public Function<T, R> transformFunction() { | |
77 |
1
1. transformFunction : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::transformFunction → KILLED |
return this.transformFunction; |
78 | } | |
79 | ||
80 | protected boolean hasNoChild() { | |
81 |
2
1. hasNoChild : replaced boolean return with true for com/github/dakusui/pcond/core/fluent/Matcher$Base::hasNoChild → SURVIVED 2. hasNoChild : replaced boolean return with false for com/github/dakusui/pcond/core/fluent/Matcher$Base::hasNoChild → KILLED |
return this.childPredicates.isEmpty(); |
82 | } | |
83 | ||
84 | protected List<Function<Matcher<?, R, R>, Predicate<R>>> childPredicates() { | |
85 |
1
1. childPredicates : replaced return value with Collections.emptyList for com/github/dakusui/pcond/core/fluent/Matcher$Base::childPredicates → NO_COVERAGE |
return unmodifiableList(this.childPredicates); |
86 | } | |
87 | ||
88 | @SuppressWarnings("unchecked") | |
89 | protected M me() { | |
90 |
1
1. me : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::me → KILLED |
return (M) this; |
91 | } | |
92 | ||
93 | /** | |
94 | * Override this method so that it returns extending class. | |
95 | * | |
96 | * @return A rebased transformer. | |
97 | */ | |
98 | protected abstract Matcher<?, R, R> rebase(); | |
99 | ||
100 | /* protected */ | |
101 | protected T baseValue() { | |
102 |
1
1. baseValue : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::baseValue → KILLED |
return this.baseValue.get(); |
103 | } | |
104 | ||
105 | private M junctionType(Matcher.JunctionType junctionType) { | |
106 |
3
1. lambda$junctionType$0 : replaced boolean return with true for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → SURVIVED 2. lambda$junctionType$1 : replaced return value with "" for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$junctionType$1 → NO_COVERAGE 3. lambda$junctionType$0 : replaced boolean return with false for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → KILLED |
requireState(this, v -> childPredicates.isEmpty(), v -> "Child predicate(s) are already added.: <" + this + ">"); |
107 | this.junctionType = requireNonNull(junctionType); | |
108 |
1
1. junctionType : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::junctionType → SURVIVED |
return me(); |
109 | } | |
110 | ||
111 | @SuppressWarnings("unchecked") | |
112 | private Predicate<T> buildPredicate() { | |
113 | Predicate<R> ret; | |
114 |
3
1. lambda$buildPredicate$2 : replaced boolean return with true for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$2 → SURVIVED 2. lambda$buildPredicate$3 : replaced return value with "" for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$3 → NO_COVERAGE 3. lambda$buildPredicate$2 : negated conditional → KILLED |
requireState(this, v -> !v.childPredicates.isEmpty(), (v) -> "No child has been added yet.: <" + v + ">"); |
115 |
1
1. buildPredicate : negated conditional → KILLED |
if (this.childPredicates.size() == 1) |
116 | ret = childPredicates.get(0).apply(rebase()); | |
117 | else { | |
118 | ret = this.junctionType.connect( | |
119 | new ArrayList<>(this.childPredicates) | |
120 | .stream() | |
121 |
1
1. lambda$buildPredicate$4 : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$4 → KILLED |
.map(each -> each.apply(rebase())) |
122 | .collect(toList())); | |
123 | } | |
124 |
1
1. buildPredicate : negated conditional → KILLED |
if (Objects.equals(transformFunction, Functions.identity())) |
125 |
1
1. buildPredicate : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED |
return (Predicate<T>) ret; |
126 |
1
1. buildPredicate : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED |
return Predicates.transform(transformFunction).check("THEN", ret); |
127 | } | |
128 | } | |
129 | ||
130 | enum JunctionType { | |
131 | CONJUNCTION { | |
132 | @SuppressWarnings("unchecked") | |
133 | @Override | |
134 | public <T> Predicate<T> connect(List<Predicate<T>> predicates) { | |
135 |
1
1. connect : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$JunctionType$1::connect → KILLED |
return Predicates.allOf(predicates.toArray(new Predicate[0])); |
136 | } | |
137 | }, | |
138 | DISJUNCTION { | |
139 | @SuppressWarnings("unchecked") | |
140 | @Override | |
141 | public <T> Predicate<T> connect(List<Predicate<T>> predicates) { | |
142 |
1
1. connect : replaced return value with null for com/github/dakusui/pcond/core/fluent/Matcher$JunctionType$2::connect → NO_COVERAGE |
return Predicates.anyOf(predicates.toArray(new Predicate[0])); |
143 | } | |
144 | }; | |
145 | ||
146 | public abstract <T> Predicate<T> connect(List<Predicate<T>> predicates); | |
147 | } | |
148 | } | |
Mutations | ||
51 |
1.1 |
|
56 |
1.1 |
|
61 |
1.1 |
|
63 |
1.1 |
|
68 |
1.1 |
|
72 |
1.1 |
|
77 |
1.1 |
|
81 |
1.1 2.2 |
|
85 |
1.1 |
|
90 |
1.1 |
|
102 |
1.1 |
|
106 |
1.1 2.2 3.3 |
|
108 |
1.1 |
|
114 |
1.1 2.2 3.3 |
|
115 |
1.1 |
|
121 |
1.1 |
|
124 |
1.1 |
|
125 |
1.1 |
|
126 |
1.1 |
|
135 |
1.1 |
|
142 |
1.1 |