Matcher.java

1
package com.github.valid8j.pcond.core.fluent;
2
3
import com.github.valid8j.pcond.forms.Functions;
4
import com.github.valid8j.pcond.forms.Predicates;
5
import com.github.valid8j.pcond.internals.InternalChecks;
6
7
import java.util.ArrayList;
8
import java.util.LinkedList;
9
import java.util.List;
10
import java.util.Objects;
11
import java.util.function.Function;
12
import java.util.function.Predicate;
13
import java.util.function.Supplier;
14
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       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/valid8j/pcond/core/fluent/Matcher$Base::allOf → SURVIVED
      return junctionType(JunctionType.CONJUNCTION);
52
    }
53
54
    @Override
55
    public M anyOf() {
56 1 1. anyOf : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::anyOf → KILLED
      return junctionType(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/valid8j/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/valid8j/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/valid8j/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/valid8j/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/valid8j/pcond/core/fluent/Matcher$Base::hasNoChild → SURVIVED
2. hasNoChild : replaced boolean return with false for com/github/valid8j/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/valid8j/pcond/core/fluent/Matcher$Base::childPredicates → SURVIVED
      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/valid8j/pcond/core/fluent/Matcher$Base::me → KILLED
      return (M) this;
91
    }
92
93
    /**
94
     * This method is called when a matcher object is converted into a predicate.
95
     * Override this method so that it returns extending class.
96
     *
97
     * @return A rebased transformer.
98
     */
99
    protected abstract Matcher<?, R, R> rebase();
100
101
    /* protected */
102
    protected T baseValue() {
103 1 1. baseValue : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::baseValue → KILLED
      return this.baseValue.get();
104
    }
105
106
    private M junctionType(JunctionType junctionType) {
107 3 1. lambda$junctionType$0 : replaced boolean return with true for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → SURVIVED
2. lambda$junctionType$1 : replaced return value with "" for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$1 → NO_COVERAGE
3. lambda$junctionType$0 : replaced boolean return with false for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → KILLED
      InternalChecks.requireState(this, v -> childPredicates.isEmpty(), v -> "Child predicate(s) are already added.: <" + this + ">");
108
      this.junctionType = requireNonNull(junctionType);
109 1 1. junctionType : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::junctionType → KILLED
      return me();
110
    }
111
112
    @SuppressWarnings("unchecked")
113
    private Predicate<T> buildPredicate() {
114
      Predicate<R> ret;
115 3 1. lambda$buildPredicate$2 : replaced boolean return with true for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$2 → SURVIVED
2. lambda$buildPredicate$3 : replaced return value with "" for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$3 → NO_COVERAGE
3. lambda$buildPredicate$2 : negated conditional → KILLED
      InternalChecks.requireState(this, v -> !v.childPredicates.isEmpty(), (v) -> "No child has been added yet.: <" + v + ">");
116 1 1. buildPredicate : negated conditional → KILLED
      if (this.childPredicates.size() == 1)
117
        ret = childPredicates.get(0).apply(rebase());
118
      else {
119
        ret = this.junctionType.connect(
120
            new ArrayList<>(this.childPredicates)
121
                .stream()
122 1 1. lambda$buildPredicate$4 : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$4 → KILLED
                .map(each -> each.apply(rebase()))
123
                .collect(toList()));
124
      }
125 1 1. buildPredicate : negated conditional → KILLED
      if (Objects.equals(transformFunction, Functions.identity()))
126 1 1. buildPredicate : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED
        return (Predicate<T>) ret;
127 1 1. buildPredicate : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED
      return Predicates.transform(transformFunction).check("THEN", ret);
128
    }
129
  }
130
131
  enum JunctionType {
132
    CONJUNCTION {
133
      @SuppressWarnings("unchecked")
134
      @Override
135
      public <T> Predicate<T> connect(List<Predicate<T>> predicates) {
136 1 1. connect : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$JunctionType$1::connect → KILLED
        return Predicates.allOf(predicates.toArray(new Predicate[0]));
137
      }
138
    },
139
    DISJUNCTION {
140
      @SuppressWarnings("unchecked")
141
      @Override
142
      public <T> Predicate<T> connect(List<Predicate<T>> predicates) {
143 1 1. connect : replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$JunctionType$2::connect → KILLED
        return Predicates.anyOf(predicates.toArray(new Predicate[0]));
144
      }
145
    };
146
147
    public abstract <T> Predicate<T> connect(List<Predicate<T>> predicates);
148
  }
149
}

Mutations

51

1.1
Location : allOf
Killed by : none
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::allOf → SURVIVED

56

1.1
Location : anyOf
Killed by : com.github.valid8j.examples.test.ExamplesTest.testClassicExample(com.github.valid8j.examples.test.ExamplesTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::anyOf → KILLED

61

1.1
Location : toPredicate
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
negated conditional → KILLED

63

1.1
Location : toPredicate
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::toPredicate → KILLED

68

1.1
Location : addPredicate
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::addPredicate → KILLED

72

1.1
Location : value
Killed by : com.github.valid8j.it.SmokeTest.givenBook_whenCheckTitleAndAbstract_thenTheyAreNotNullAndAppropriateLength_2(com.github.valid8j.it.SmokeTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::value → KILLED

77

1.1
Location : transformFunction
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::transformFunction → KILLED

81

1.1
Location : hasNoChild
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced boolean return with false for com/github/valid8j/pcond/core/fluent/Matcher$Base::hasNoChild → KILLED

2.2
Location : hasNoChild
Killed by : none
replaced boolean return with true for com/github/valid8j/pcond/core/fluent/Matcher$Base::hasNoChild → SURVIVED

85

1.1
Location : childPredicates
Killed by : none
replaced return value with Collections.emptyList for com/github/valid8j/pcond/core/fluent/Matcher$Base::childPredicates → SURVIVED

90

1.1
Location : me
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::me → KILLED

103

1.1
Location : baseValue
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::baseValue → KILLED

107

1.1
Location : lambda$junctionType$0
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced boolean return with false for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → KILLED

2.2
Location : lambda$junctionType$0
Killed by : none
replaced boolean return with true for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$0 → SURVIVED

3.3
Location : lambda$junctionType$1
Killed by : none
replaced return value with "" for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$junctionType$1 → NO_COVERAGE

109

1.1
Location : junctionType
Killed by : com.github.valid8j.examples.test.ExamplesTest.testClassicExample(com.github.valid8j.examples.test.ExamplesTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::junctionType → KILLED

115

1.1
Location : lambda$buildPredicate$2
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
negated conditional → KILLED

2.2
Location : lambda$buildPredicate$2
Killed by : none
replaced boolean return with true for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$2 → SURVIVED

3.3
Location : lambda$buildPredicate$3
Killed by : none
replaced return value with "" for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$3 → NO_COVERAGE

116

1.1
Location : buildPredicate
Killed by : com.github.valid8j.ut.styles.FluentStyleTestAssertionTest$ForTestAssertionsTest.string_assertThatTest_failed(com.github.valid8j.ut.styles.FluentStyleTestAssertionTest$ForTestAssertionsTest)
negated conditional → KILLED

122

1.1
Location : lambda$buildPredicate$4
Killed by : com.github.valid8j.ut.utilstest.ReportDetailTest.givenLongString_whenCheckEqualnessWithSlightlyDifferentString_thenFailWithDetailsArePrinted(com.github.valid8j.ut.utilstest.ReportDetailTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::lambda$buildPredicate$4 → KILLED

125

1.1
Location : buildPredicate
Killed by : com.github.valid8j.ut.styles.MoreFluentListTest.test_isEmpty(com.github.valid8j.ut.styles.MoreFluentListTest)
negated conditional → KILLED

126

1.1
Location : buildPredicate
Killed by : com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest.test_postcondition(com.github.valid8j.ut.styles.FluentStyleDbCTest$ForEnsuresTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED

127

1.1
Location : buildPredicate
Killed by : com.github.valid8j.ut.styles.MoreFluentListTest.test_isEmpty(com.github.valid8j.ut.styles.MoreFluentListTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$Base::buildPredicate → KILLED

136

1.1
Location : connect
Killed by : com.github.valid8j.ut.utilstest.ReportDetailTest.givenLongString_whenCheckEqualnessWithSlightlyDifferentString_thenFailWithDetailsArePrinted(com.github.valid8j.ut.utilstest.ReportDetailTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$JunctionType$1::connect → KILLED

143

1.1
Location : connect
Killed by : com.github.valid8j.examples.test.ExamplesTest.testClassicExample(com.github.valid8j.examples.test.ExamplesTest)
replaced return value with null for com/github/valid8j/pcond/core/fluent/Matcher$JunctionType$2::connect → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3